From aea471fbaf93749b231aa2440c52f74f7fd89b44 Mon Sep 17 00:00:00 2001 From: Marcus Nyeholt Date: Thu, 17 Sep 2015 13:31:50 +1000 Subject: [PATCH] NEW Allow a blockset to be bound to parent node Adds an option that, if selected, allows a blockset to be bound on the selected node from the tree instead of just the _child_ nodes of the selected item. Addresses issue #34 --- code/dataobjects/BlockSet.php | 4 +++- code/extensions/BlocksSiteTreeExtension.php | 19 ++++++++++++++----- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/code/dataobjects/BlockSet.php b/code/dataobjects/BlockSet.php index 4815398..0240cf0 100644 --- a/code/dataobjects/BlockSet.php +++ b/code/dataobjects/BlockSet.php @@ -11,7 +11,8 @@ class BlockSet extends DataObject implements PermissionProvider{ **/ private static $db = array( 'Title' => 'Varchar(255)', - 'PageTypes' => 'MultiValueField' + 'PageTypes' => 'MultiValueField', + 'IncludePageParent' => 'Boolean', ); /** @@ -51,6 +52,7 @@ public function getCMSFields(){ $fields->addFieldToTab('Root.Main', MultiValueCheckboxField::create('PageTypes', 'Only apply to these Page Types:', $this->pageTypeOptions()) ->setDescription('Selected Page Types will inherit this Block Set automatically. Leave all unchecked to apply to all page types.')); $fields->addFieldToTab('Root.Main', TreeMultiselectField::create('PageParents', 'Only apply to children of these Pages:', 'SiteTree')); + $fields->addFieldToTab('Root.Main', CheckboxField::create('IncludePageParent', 'Apply block set to selected page parents as well as children')); if(!$this->ID){ $fields->addFieldToTab('Root.Main', LiteralField::create('NotSaved', "

You can add Blocks to this set once you have saved it for the first time

")); diff --git a/code/extensions/BlocksSiteTreeExtension.php b/code/extensions/BlocksSiteTreeExtension.php index 42e2003..00a2266 100644 --- a/code/extensions/BlocksSiteTreeExtension.php +++ b/code/extensions/BlocksSiteTreeExtension.php @@ -195,11 +195,20 @@ public function getAppliedSets() { foreach ($sets as $set) { $restrictedToParerentIDs = $set->PageParents()->column('ID'); - if (count($restrictedToParerentIDs) && count($ancestors)) { - foreach ($ancestors as $ancestor) { - if (in_array($ancestor, $restrictedToParerentIDs)) { - $list->add($set); - continue; + if (count($restrictedToParerentIDs)) { + // check whether the set should include selected parent, in which case check whether + // it was in the restricted parents list. If it's not, or if include parentpage + // wasn't selected, we check the ancestors of this page. + if ($set->IncludePageParent && in_array($this->owner->ID, $restrictedToParerentIDs)) { + $list->add($set); + } else { + if (count($ancestors)) { + foreach ($ancestors as $ancestor) { + if (in_array($ancestor, $restrictedToParerentIDs)) { + $list->add($set); + continue; + } + } } } } else {