Skip to content

Commit 51e4f96

Browse files
committed
Adding sortability to a page's modules; Removing example module from codebase
1 parent 478a1ba commit 51e4f96

4 files changed

Lines changed: 36 additions & 53 deletions

File tree

src/Example.php

Lines changed: 0 additions & 25 deletions
This file was deleted.

src/Module.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class Module extends DataObject {
2929
];
3030

3131
private static $summary_fields = [
32-
'Type' => 'Type',
32+
'ClassName' => 'Type',
3333
'Title' => 'Title',
3434
'Position' => 'Position',
3535
'Pages.Count' => 'Number of pages'
@@ -52,7 +52,7 @@ public function getCMSFields() {
5252
$fields->addFieldToTab('Root.Main', DropdownField::create(
5353
'Position',
5454
'Position',
55-
Config::inst()->get('ModuleManager', 'positions')
55+
Config::inst()->get(ModuleManager::class, 'positions')
5656
)->setEmptyString('Please select'));
5757
$fields->addFieldToTab("Root.Main", TreeMultiselectField::create("Pages", "Pages", SiteTree::class));
5858

src/ModuleAdmin.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ class Admin extends ModelAdmin {
1616
Module::class
1717
);
1818

19-
2019
public function getEditForm($id = null, $fields = null){
2120
$form = parent::getEditForm($id, $fields);
2221

src/ModuleSiteTreeExtension.php

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
use SilverStripe\Forms\GridField\GridField;
1717
use SilverStripe\Forms\GridField\GridFieldConfig_RelationEditor;
1818
use SilverStripe\Forms\GridField\GridFieldAddNewButton;
19+
use SilverStripe\Forms\GridField\GridFieldPageCount;
1920
use Symbiote\GridFieldExtensions\GridFieldAddNewMultiClass;
21+
use Symbiote\GridFieldExtensions\GridFieldOrderableRows;
2022

2123
class ModuleSiteTreeExtension extends DataExtension {
2224

@@ -44,34 +46,36 @@ class ModuleSiteTreeExtension extends DataExtension {
4446
* @return FieldList obj
4547
**/
4648
public function updateCMSFields(FieldList $fields){
47-
48-
// inherit field
49-
$fields->addFieldToTab("Root.Modules", $inheritField = CheckboxField::create('InheritModules','Inherit modules')->setDescription('Inherit <strong>all</strong> modules from the parent page. If the parent page is also set to inherit, then we go further up the hierarchy.'));
50-
51-
// module manager gridfield
52-
$gridFieldConfig = GridFieldConfig_RelationEditor::create();
53-
//$gridFieldConfig->addComponent(new GridFieldSortableRows('SortOrder'));
54-
55-
$gridField = GridField::create("Modules", "Modules", $this->owner->Modules(), $gridFieldConfig);
56-
57-
$gridFieldConfig->removeComponentsByType(GridFieldAddNewButton::class);
58-
$gridFieldConfig->addComponent(new GridFieldAddNewMultiClass());
59-
$gridField->addExtraClass('modulemanager-modules-field');
49+
6050
if ($this->owner->InheritModules){
61-
$gridField->addExtraClass('hide');
51+
$tab_name = "Root.Modules";
52+
} else {
53+
$tab_name = "Root.Modules (".$this->owner->Modules()->count().")";
54+
}
55+
56+
$fields->removeByName('Modules');
57+
58+
$fields->addFieldToTab($tab_name, $inherit_field = CheckboxField::create('InheritModules','Inherit modules')->setDescription('Inherit <strong>all</strong> modules from the parent page. If the parent page is also set to inherit, then we go further up the hierarchy.'));
59+
60+
// When we inherit, the page's modules become irrelevant
61+
if (!$this->owner->InheritModules){
62+
$gridfield_config = GridFieldConfig_RelationEditor::create();
63+
64+
$gridfield = GridField::create("Modules", "Modules", $this->owner->Modules(), $gridfield_config);
65+
$gridfield->addExtraClass('modulemanager-modules-field');
66+
67+
$gridfield_config->addComponent(new GridFieldOrderableRows('SortOrder'));
68+
$gridfield_config->addComponent(new GridFieldAddNewMultiClass());
69+
$gridfield_config->removeComponentsByType(GridFieldAddNewButton::class);
70+
$gridfield_config->removeComponentsByType(GridFieldPageCount::class);
71+
72+
$fields->addFieldToTab($tab_name, $gridfield);
6273
}
6374

64-
$fields->addFieldToTab("Root.Modules", $gridField);
6575

6676
return $fields;
6777

68-
}
69-
70-
// get my parent page
71-
public function MyParentPage($page){
72-
return $page->parent;
73-
}
74-
78+
}
7579

7680
/**
7781
* Get all the modules attached to this page (across all ModuleAreas)
@@ -83,7 +87,12 @@ public function PageModules(){
8387

8488
// check for inheritance by recursively searching
8589
while ($page->InheritModules && $page->ParentID > 0){
86-
$page = $this->MyParentPage($page);
90+
$page = $page->parent();
91+
}
92+
93+
// We're at the top, but we're still set to inherit, so let's inherit an array of nothing
94+
if ($page->InheritModules){
95+
return ArrayList::create();
8796
}
8897

8998
return $page->Modules()->sort('SortOrder ASC');
@@ -98,10 +107,10 @@ public function PageModules(){
98107
**/
99108
public function ModulePosition($alias, $limit = false){
100109

101-
$positions = Config::inst()->get('ModuleManager', 'positions');
110+
$positions = Config::inst()->get(ModuleManager::class, 'positions');
102111

103112
if (!$positions || !isset($positions[$alias])){
104-
user_error("Trying to call module position \"".$alias."\" but this doesn't exist. Make sure you have setup your custom positions in your config.yml",E_USER_NOTICE);
113+
user_error("Module position \"".$alias."\" doesn't exist. Have setup your custom positions in your config.yml?",E_USER_NOTICE);
105114
}
106115

107116
// get this page's module list for specified position

0 commit comments

Comments
 (0)