Skip to content

Commit 422e35a

Browse files
authored
Merge pull request #1 from nswdpc/feat-sync-grid-desktop
Sync grid to desktop
2 parents f5b532e + da8ed14 commit 422e35a

File tree

2 files changed

+40
-10
lines changed

2 files changed

+40
-10
lines changed

src/Extensions/ElementChildGridExtension.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class ElementChildGridExtension extends DataExtension
2222
* @var array
2323
*/
2424
private static $db = [
25-
'CardColumns' => 'Varchar(64)' // grid columns at lg breakpoint
25+
'CardColumns' => 'Int(4)' // grid columns at lg breakpoint
2626
];
2727

2828
/**
@@ -75,33 +75,36 @@ public function getColumns() {
7575

7676
/**
7777
* Return the CSS class representing a grid
78-
* @param string $lg the number of large columns eg 3. An empty string, the default defers to the selected CardColumns value
78+
* @param int|null $lg the number of large columns eg 3. Default=null meaning defer to the selected CardColumns value
7979
* @param int $max the max grid size. Used to work out the CSS class. $max/$lg = grid 'width'
8080
* @param int $xs number of columns at XS media size, default = 1 col @ 100% width
8181
* @param int $sm number of columns at SM media size, default = 2 cols @ 50% width
8282
* @param int $md number of columns at MD media size, default = 3 cols @ 33.3% width
8383
* @param mixed $xl number of columns at XL media size, if supported, default = none
8484
*/
85-
public function ColumnClass($lg = '', $max = 12, $xs = 1, $sm = 2, $md = 3, $xl = '') : string
85+
public function ColumnClass($lg = null, $max = 12, $xs = 1, $sm = 2, $md = 3, $xl = null) : string
8686
{
87-
// If there are no override columns from code, use the saved field value
88-
$lg = trim($lg);
89-
if($lg) {
90-
$columns = $lg;
87+
88+
if(is_int($lg)) {
89+
$desktopColumns = $lg;
9190
} else {
92-
$columns = $this->owner->CardColumns;
91+
$desktopColumns = $this->owner->CardColumns;
9392
}
9493

9594
$max = trim($max);
9695
if(!$max) {
9796
$max = $this->getConfigurator()->config()->get('max_columns');
9897
}
9998

100-
if(!$columns) {
99+
if(!$desktopColumns) {
101100
return '';
102101
} else {
103102

104-
$gridLg = ceil($max / $columns);
103+
$xs = $this->getConfigurator()->getGridValue($xs, $desktopColumns);
104+
$sm = $this->getConfigurator()->getGridValue($sm, $desktopColumns);
105+
$md = $this->getConfigurator()->getGridValue($md, $desktopColumns);
106+
107+
$gridLg = ceil($max / $desktopColumns);
105108
$gridXs = ceil($max / $xs);
106109
$gridSm = ceil($max / $sm);
107110
$gridMd = ceil($max / $md);
@@ -115,6 +118,7 @@ public function ColumnClass($lg = '', $max = 12, $xs = 1, $sm = 2, $md = 3, $xl
115118

116119
$gridXl = null;
117120
if($xl > 0) {
121+
$xl = $this->getConfigurator()->getGridValue($xl, $desktopColumns);
118122
$gridXl = ceil($max / $xl);
119123
}
120124
if($gridXl) {

src/Models/Configuration.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ class Configuration {
4949
'6' => '6'
5050
];
5151

52+
/**
53+
* @var bool
54+
* When true, grid columns > than the supplied desktop columns value
55+
* will be made = to the desktop value
56+
* When false, the values used will be as supplied by input
57+
* See
58+
*/
59+
private static $sync_grid_to_desktop = true;
60+
5261
/**
5362
* Work out and return the grid mapping based on prefix and a key
5463
* Use the config values to return the relevant values from configuration
@@ -61,4 +70,21 @@ public function ColumnMapping($key) : string {
6170
return $prefix . ($breakpoint ? "-{$breakpoint}" : "-");
6271
}
6372

73+
/**
74+
* Determine grid value based on configuration and desktop value
75+
*/
76+
public function getGridValue(int $cols, int $desktopColumns) : int {
77+
$sync = $this->config()->get('sync_grid_to_desktop');
78+
if(!$sync) {
79+
return $cols;
80+
}
81+
if($cols > $desktopColumns) {
82+
$cols = $desktopColumns;
83+
}
84+
if($cols == 0) {
85+
$cols = 1;
86+
}
87+
return $cols;
88+
}
89+
6490
}

0 commit comments

Comments
 (0)