Skip to content

Commit 31fb6ff

Browse files
committed
feat: added totalColumns and totalRows to Twig and GraphQL
1 parent 1354259 commit 31fb6ff

File tree

7 files changed

+68
-17
lines changed

7 files changed

+68
-17
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,17 @@ All notable changes to this project will be documented in this file.
44

55
The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).
66

7-
## 1.3.0 - 2022-12-04
7+
## 2.0.0 - 2022-12-05
88
### Added
99
- Added field setting to add icons to Little Layout field boxes.
1010
- Added field setting to set the height and width of Little Layout field boxes, using CSS values.
11-
- Updated doc blocks and added return types to all PHP files.
11+
- Added ability to get column and row settings from a field in your Twig templates and GraphQL queries.
1212

1313
### Changed
1414
- Improved performance by refactoring Little Layout field from a Vue app to a Web Component.
1515
- Refactored field settings from a Vue app to a Web Component.
1616
- Changed layout boxes in field from clickable div elements to `button` elements.
17+
- Updated doc blocks and added return types to all PHP files.
1718
- Updated JS bundler to Vite 5.x.
1819
- Bumped NodeJS to Node 20.x.
1920

README.md

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,20 @@ Use the Empty Layout setting to determine if authors can clear out the layouts t
4444

4545
The following data can be retrieved in your Twig- or GraphQL-based templates:
4646

47-
| Property | Description |
48-
|---|---|
49-
| **empty** | Determine if field has a value. |
50-
| **gridColumnStart** | Value for CSS Grid’s `grid-column-start` and `grid-column` properties. |
51-
| **gridColumnEnd** | Value for CSS Grid’s `grid-column-end` and `grid-column` properties. |
52-
| **gridRowStart** | Value for CSS Grid’s `grid-row-start` and `grid-row` properties. |
53-
| **gridRowEnd** | Value for CSS Grid’s `grid-row-end` and `grid-row` properties. |
54-
| **gridColumnSpan** | Value for CSS Grid’s `grid-column` property. |
55-
| **gridRowSpan** | Value for CSS Grid’s `grid-row` property. |
56-
| **selectedColumns** | Array of columns selected in the layout field. |
57-
| **selectedRows** | Array of rows selected in the layout field. |
58-
| **selectedCoordinates** | Array of coordinates* selected in the layout field. |
47+
| Property | Description |
48+
|-------------------------|------------------------------------------------------------------------|
49+
| **empty** | Determine if field has a value. |
50+
| **gridColumnStart** | Value for CSS Grid’s `grid-column-start` and `grid-column` properties. |
51+
| **gridColumnEnd** | Value for CSS Grid’s `grid-column-end` and `grid-column` properties. |
52+
| **gridRowStart** | Value for CSS Grid’s `grid-row-start` and `grid-row` properties. |
53+
| **gridRowEnd** | Value for CSS Grid’s `grid-row-end` and `grid-row` properties. |
54+
| **gridColumnSpan** | Value for CSS Grid’s `grid-column` property. |
55+
| **gridRowSpan** | Value for CSS Grid’s `grid-row` property. |
56+
| **selectedColumns** | Array of columns selected in the layout field. |
57+
| **selectedRows** | Array of rows selected in the layout field. |
58+
| **selectedCoordinates** | Array of coordinates* selected in the layout field. |
59+
| **totalColumns** | The columns setting for this field. |
60+
| **totalRows** | The rows setting for this field. |
5961

6062
_*Coordinates are formatted in `'x|y'` format._
6163

@@ -105,7 +107,7 @@ With CSS that looks like this:
105107
If you use a CSS framework, like Tailwind CSS, you could do something like this:
106108

107109
```twig
108-
<div class="col-start-{{ element.fieldHandle.gridColumnStart }} col-span-{{ element.fieldHandle.gridColumnSpan }} row-start-{{ element.fieldHandle.gridRowStart }} row-span-{{ element.fieldHandle.gridRowSpan }}"></div>
110+
<div class="col-start-[{{ element.fieldHandle.gridColumnStart }}] col-span-[{{ element.fieldHandle.gridColumnSpan }}] row-start-[{{ element.fieldHandle.gridRowStart }}] row-span-[{{ element.fieldHandle.gridRowSpan }}]"></div>
109111
```
110112

111113
If your Twig template files are included in your Tailwind confguration’s `content` patterns, you may prefer to preserve full class names using logic and a lookup table:

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "wbrowar/craft-little-layout",
33
"description": "A compact, visual way to lay out fields, elements, and Matrix blocks.",
44
"type": "craft-plugin",
5-
"version": "1.2.0",
5+
"version": "2.0.0",
66
"keywords": [
77
"craft",
88
"cms",

src/fields/Layout.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,18 @@ public function normalizeValue($value, ?ElementInterface $element = null): Layou
202202
$value = Json::decodeIfJson($value);
203203
}
204204
if (\is_array($value)) {
205+
// Field data passed to Twig and GraphQL.
206+
207+
$settings = $this->getSettings();
208+
205209
$model = new LayoutModel($value);
210+
$model->columns = $settings['cols'];
211+
$model->rows = $settings['rows'];
206212
} elseif ($value instanceof LayoutModel) {
213+
// Field data used when saving the field.
207214
$model = $value;
208215
} else {
216+
// Field data used in field inputs.
209217
$model = new LayoutModel(['raw' => $value]);
210218
}
211219

src/gql/types/LittleLayoutType.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,16 @@ public static function getFieldDefinitions(): array
6767
'type' => Type::listOf(Type::string()),
6868
'description' => 'Array of coordinates selected in the layout field. Coordinates are formatted in `x|y` format.',
6969
],
70+
'totalColumns' => [
71+
'name' => 'totalColumns',
72+
'type' => Type::int(),
73+
'description' => 'The columns setting for this field.',
74+
],
75+
'totalRows' => [
76+
'name' => 'totalRows',
77+
'type' => Type::int(),
78+
'description' => 'The rows setting for this field.',
79+
],
7080
];
7181
}
7282
}

src/models/LayoutModel.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,21 @@ class LayoutModel extends Model
2222
// Public Properties
2323
// =========================================================================
2424

25+
/**
26+
* @var int
27+
*/
28+
public $columns = 1;
29+
2530
/**
2631
* @var string
2732
*/
2833
public $raw = '';
2934

35+
/**
36+
* @var int
37+
*/
38+
public $rows = 1;
39+
3040
// Public Methods
3141
// =========================================================================
3242

@@ -151,6 +161,26 @@ public function getSelectedRows():array
151161
return [];
152162
}
153163

164+
/**
165+
* The columns setting for this field.
166+
*
167+
* @return int
168+
*/
169+
public function getTotalColumns(): int
170+
{
171+
return $this->columns;
172+
}
173+
174+
/**
175+
* The rows setting for this field.
176+
*
177+
* @return int
178+
*/
179+
public function getTotalRows(): int
180+
{
181+
return $this->rows;
182+
}
183+
154184
/**
155185
* Array of coordinates selected in the layout field.
156186
* Coordinates are formatted in `x|y` format.

src/templates/_components/fields/Layout_input.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,4 @@
4242
name: name,
4343
value: startingValue,
4444
}) }}
45-
</little-layout-field>
45+
</little-layout-field>

0 commit comments

Comments
 (0)