Skip to content

Commit 1be81a4

Browse files
committed
WIP: improve column count default handling, allow owner class to set its own default
1 parent d1a20a6 commit 1be81a4

File tree

5 files changed

+96
-9
lines changed

5 files changed

+96
-9
lines changed

src/Extensions/ElementChildGridExtension.php

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace NSWDPC\GridHelper\Extensions;
44

55
use NSWDPC\GridHelper\Models\Configuration;
6+
use SilverStripe\Core\Config\Config;
67
use Silverstripe\Core\Injector\Injector;
78
use Silverstripe\ORM\DataExtension;
89
use Silverstripe\Forms\FieldList;
@@ -22,15 +23,7 @@ class ElementChildGridExtension extends DataExtension
2223
* @var array
2324
*/
2425
private static $db = [
25-
'CardColumns' => 'Int(4)' // grid columns at lg breakpoint
26-
];
27-
28-
/**
29-
* Default values
30-
* @var array
31-
*/
32-
private static $defaults = [
33-
'CardColumns' => 4 // at lg breakpoint
26+
'CardColumns' => 'Int' // grid columns at lg breakpoint
3427
];
3528

3629
/**
@@ -66,6 +59,21 @@ public function updateCMSFields(FieldList $fields)
6659

6760
}
6861

62+
/**
63+
* Ensure default column count is written if not set
64+
* Allow the owner class to set it's own default via configuration
65+
*/
66+
public function onBeforeWrite() {
67+
if(!$this->owner->CardColumns) {
68+
$defaultLargeColumnCount = Configuration::config()->get('default_lg_column_count');
69+
$ownerDefaultCount = Config::inst()->get( get_class($this->owner), 'grid_default_lg_column_count');
70+
if($ownerDefaultCount) {
71+
$defaultLargeColumnCount = $ownerDefaultCount;
72+
}
73+
$this->owner->CardColumns = $defaultLargeColumnCount;
74+
}
75+
}
76+
6977
/**
7078
* This method is retained for BC
7179
*/

src/Models/Configuration.php

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

52+
/**
53+
* Default "lg" viewport number of columns
54+
* @var int
55+
*/
56+
private static $default_lg_column_count = 4;
57+
5258
/**
5359
* @var bool
5460
* When true, grid columns > than the supplied desktop columns value

tests/.gitkeep

Whitespace-only changes.

tests/ColumnCountModel.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace NSWDPC\GridHelper\Tests;
4+
5+
use NSWDPC\GridHelper\Extensions\ElementChildGridExtension;
6+
use SilverStripe\Dev\TestOnly;
7+
use SilverStripe\ORM\DataObject;
8+
9+
/**
10+
* Model to test column count handling
11+
* @author James
12+
*/
13+
class ColumnCountModel extends DataObject implements TestOnly {
14+
15+
/**
16+
* @inheritdoc
17+
*/
18+
private static $table_name = "ColumnCountModel";
19+
20+
/**
21+
* @inheritdoc
22+
*/
23+
private static $db = [
24+
'Title' => 'Varchar(255)'
25+
];
26+
27+
}

tests/ColumnCountTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace NSWDPC\GridHelper\Tests;
4+
5+
use NSWDPC\GridHelper\Extensions\ElementChildGridExtension;
6+
use NSWDPC\GridHelper\Models\Configuration;
7+
use SilverStripe\Core\Config\Config;
8+
use SilverStripe\Dev\SapphireTest;
9+
10+
/**
11+
* Provide tests for a single gallery video
12+
*/
13+
class ColumnCountTest extends SapphireTest {
14+
15+
protected $usesDatabase = true;
16+
17+
/**
18+
* @inheritdoc
19+
*/
20+
protected static $extra_dataobjects = [
21+
ColumnCountModel::class
22+
];
23+
24+
/**
25+
* @inheritdoc
26+
*/
27+
protected static $required_extensions = [
28+
ColumnCountModel::class => [
29+
ElementChildGridExtension::class
30+
]
31+
];
32+
33+
public function testColumnCount() {
34+
$defaultLargeColumnCount = Configuration::config()->get('default_lg_column_count');
35+
36+
$obj = ColumnCountModel::create([
37+
'Title' => 'Test model'
38+
]);
39+
$obj->write();
40+
41+
var_dump($obj->CardColumns);
42+
43+
$this->assertEquals($defaultLargeColumnCount, 4);
44+
}
45+
46+
}

0 commit comments

Comments
 (0)