From 067e40984f7634fc8ead3892ef2295918393811c Mon Sep 17 00:00:00 2001 From: Marco Crespi Date: Thu, 1 Feb 2018 15:45:50 +0100 Subject: [PATCH] fix(page-sections): Move classes to FlxLabs\PageSections namespace. Apply extension to sitetree, because class Page might not exist. Fix some basic styling issues in gridview. Rename "Title" field to "Name". --- _config/config.yml | 2 +- code/GridFieldPageSectionsExtension.php | 4 +- code/PageElement.php | 68 ++++++++++--------- code/PageSectionsExtension.php | 15 ++-- css/GridFieldPageSectionsExtension.css | 8 ++- .../{ => FlxLabs/PageSections}/PageElement.ss | 2 +- 6 files changed, 53 insertions(+), 46 deletions(-) rename templates/{ => FlxLabs/PageSections}/PageElement.ss (89%) diff --git a/_config/config.yml b/_config/config.yml index 331a027..be14e1b 100644 --- a/_config/config.yml +++ b/_config/config.yml @@ -1,6 +1,6 @@ --- Name: pagesections --- -PageSections\PageElement: +FlxLabs\PageSections\PageElement: extensions: - SilverStripe\Versioned\Versioned diff --git a/code/GridFieldPageSectionsExtension.php b/code/GridFieldPageSectionsExtension.php index 8f9c16a..8c8e797 100644 --- a/code/GridFieldPageSectionsExtension.php +++ b/code/GridFieldPageSectionsExtension.php @@ -1,6 +1,6 @@ Title = "New " . $type; + $child->Name = "New " . $type; $child->write(); $obj->Children()->Add($child); diff --git a/code/PageElement.php b/code/PageElement.php index 23390da..72098b0 100644 --- a/code/PageElement.php +++ b/code/PageElement.php @@ -1,6 +1,6 @@ 'Varchar(255)', + "Name" => "Varchar(255)", ); private static $many_many = array( - 'Children' => PageElement::class, + "Children" => PageElement::class, ); private static $belongs_many_many = array( - 'Parents' => PageElement::class, - 'Pages' => 'Page', + "Parents" => PageElement::class, + "Pages" => SiteTree::class, ); private static $many_many_extraFields = array( - 'Children' => array( - 'SortOrder' => 'Int', + "Children" => array( + "SortOrder" => 'Int', ), ); @@ -60,15 +65,15 @@ function canCreate($member = null, $context = array()) { return true; } ]; private static $summary_fields = array( - 'SingularName', - 'ID', - 'GridFieldPreview', + "SingularName", + "ID", + "GridFieldPreview", ); private static $searchable_fields = array( - 'ClassName', - 'Title', - 'ID' + "ClassName", + "Name", + "ID", ); public static function getAllowedPageElements() { @@ -89,12 +94,24 @@ public function onBeforeWrite() { } } + public function onAfterWrite() { + $stage = Versioned::get_stage(); + + foreach ($this->Parents() as $parent) { + $parent->copyVersionToStage($stage, $stage, true); + } + + foreach ($this->Pages() as $page) { + $page->copyVersionToStage($stage, $stage, true); + } + } + public function getChildrenGridField() { $addNewButton = new GridFieldAddNewMultiClass(); $addNewButton->setClasses($this->getAllowedPageElements()); $autoCompl = new GridFieldAddExistingAutocompleter('buttons-before-right'); - $autoCompl->setResultsFormat('$Title ($ID)'); + $autoCompl->setResultsFormat('$Name ($ID)'); $autoCompl->setSearchList(PageElement::get()->exclude("ID", $this->getParentIDs())); $config = GridFieldConfig::create() @@ -111,7 +128,7 @@ public function getChildrenGridField() { } public function getGridFieldPreview() { - return $this->Title; + return $this->Name; } public function getCMSFields() { @@ -135,7 +152,7 @@ public function getParentIDs() { return $IDArr; } - public function renderChildren($parents) { + public function renderChildren($parents = null) { return $this->renderWith( "RenderChildren", array("Elements" => $this->Children(), "ParentList" => strval($this->ID) . "," . $parents) @@ -156,19 +173,4 @@ public function forTemplate($parentList = "") { array("ParentList" => $parentList, "Parents" => $parents, "Page" => $page) ); } - - public function getBetterButtonsUtils() { - $fieldList = FieldList::create(array( - BetterButtonPrevNextAction::create(), - )); - return $fieldList; - } - - public function getBetterButtonsActions() { - $fieldList = FieldList::create(array( - BetterButton_SaveAndClose::create(), - BetterButton_Save::create(), - )); - return $fieldList; - } } diff --git a/code/PageSectionsExtension.php b/code/PageSectionsExtension.php index 46da018..aa72559 100644 --- a/code/PageSectionsExtension.php +++ b/code/PageSectionsExtension.php @@ -1,6 +1,6 @@ update($class, "many_many", $many_many); - //Config::inst()->update($class, "many_many_extraFields", $many_many_extraFields); - Config::modify()->merge($class, "owns", $owns); return array( "many_many" => $many_many, "many_many_extraFields" => $many_many_extraFields, + "owns" => $owns, ); } @@ -51,6 +50,8 @@ public static function getAllowedPageElements() { } public function onBeforeWrite() { + parent::onBeforeWrite(); + $sections = $this->owner->config()->get("page_sections"); if (!$sections) $sections = array("Main"); @@ -80,7 +81,7 @@ public function updateCMSFields(FieldList $fields) { $addNewButton->setClasses($this->owner->getAllowedPageElements()); $autoCompl = new GridFieldAddExistingAutocompleter('buttons-before-right'); - $autoCompl->setResultsFormat('$Title ($ID)'); + $autoCompl->setResultsFormat('$Name ($ID)'); $config = GridFieldConfig::create() ->addComponent(new GridFieldButtonRow("before")) @@ -98,8 +99,8 @@ public function updateCMSFields(FieldList $fields) { } } - public function PageSection($name = "Main") { - $elements = $this->owner->{"PageSection" . $name}(); + public function RenderPageSection($name = "Main") { + $elements = $this->owner->{"PageSection" . $name}()->Sort("SortOrder"); return $this->owner->renderWith( "RenderChildren", array("Elements" => $elements, "ParentList" => strval($this->owner->ID)) diff --git a/css/GridFieldPageSectionsExtension.css b/css/GridFieldPageSectionsExtension.css index a031998..6ba66ea 100644 --- a/css/GridFieldPageSectionsExtension.css +++ b/css/GridFieldPageSectionsExtension.css @@ -43,15 +43,19 @@ .ss-gridfield-pagesections .col-treenav { height: 100%; - padding: 0 !important; + padding-left: 0; + /*padding: 0 !important;*/ } .ss-gridfield-pagesections .col-treenav > button { - //width: 100% !important; + /*width: 100% !important;*/ height: 100% !important; font-weight: bold !important; /*font-size: 150% !important;*/ color: black !important; + border: none; + background: transparent; + padding-right: 0; } .ss-gridfield-pagesections .col-treenav > button, .ss-gridfield-pagesections .col-treenav > button > span { diff --git a/templates/PageElement.ss b/templates/FlxLabs/PageSections/PageElement.ss similarity index 89% rename from templates/PageElement.ss rename to templates/FlxLabs/PageSections/PageElement.ss index 04c2f2c..346d3d5 100644 --- a/templates/PageElement.ss +++ b/templates/FlxLabs/PageSections/PageElement.ss @@ -1,5 +1,5 @@
-

$Title

+

$Name

$Layout
$RenderChildren($ParentList)