From 573c7a42ff2a429b72356d2529a2375b7fc2800e Mon Sep 17 00:00:00 2001 From: Robert van Lienden Date: Thu, 16 Mar 2023 16:17:03 +0100 Subject: [PATCH] Stash: Trying to fix add new --- _config/app.yml | 6 +++ src/Pages/ItemDetailPage.php | 1 + src/Pages/ItemOverviewPage.php | 74 +++++++++++++++++++--------------- 3 files changed, 49 insertions(+), 32 deletions(-) diff --git a/_config/app.yml b/_config/app.yml index 023ff52..01fd89e 100644 --- a/_config/app.yml +++ b/_config/app.yml @@ -1,12 +1,18 @@ --- Name: silverstripeaddons --- +RobertVanLienden\SilverStripeAddons\Pages\ItemOverviewPage: + extensions: + - SilverStripe\Lumberjack\Model\Lumberjack + Page: extensions: - RobertVanLienden\SilverStripeAddons\Extensions\PageExtension + PageController: extensions: - RobertVanLienden\SilverStripeAddons\Extensions\PageControllerExtension + Silverstripe\SiteConfig\SiteConfig: extensions: - RobertVanLienden\SilverStripeAddons\Extensions\SiteConfigExtension diff --git a/src/Pages/ItemDetailPage.php b/src/Pages/ItemDetailPage.php index eae0151..a136bba 100644 --- a/src/Pages/ItemDetailPage.php +++ b/src/Pages/ItemDetailPage.php @@ -17,6 +17,7 @@ class ItemDetailPage extends \Page private static string $description = 'A portfolio item page'; private static bool $can_be_root = false; + private static array $allowed_children = []; private static array $db = [ 'ProjectSummary' => 'HTMLText', diff --git a/src/Pages/ItemOverviewPage.php b/src/Pages/ItemOverviewPage.php index 93bf6cf..828c0ed 100644 --- a/src/Pages/ItemOverviewPage.php +++ b/src/Pages/ItemOverviewPage.php @@ -2,11 +2,14 @@ namespace RobertVanLienden\SilverStripeAddons\Pages; +use Restruct\GridFieldSiteTreeButtons\GridFieldAddNewSiteTreeItemButton; use SilverStripe\Forms\CheckboxField; +use SilverStripe\Forms\FieldList; use SilverStripe\Forms\GridField\GridField; -use SilverStripe\Forms\GridField\GridFieldAddNewButton; -use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor; +use SilverStripe\Lumberjack\Forms\GridFieldSiteTreeAddNewButton; +use SilverStripe\Lumberjack\Model\Lumberjack; use SilverStripe\ORM\DataList; +use SilverStripe\Lumberjack\Forms\GridFieldConfig_Lumberjack; /** * @@ -18,49 +21,56 @@ class ItemOverviewPage extends \Page private static string $icon_class = 'font-icon-p-articles'; private static string $description = 'A page with an overview with your portfolio items.'; + private static array $allowed_children = [ + ItemDetailPage::class + ]; + private static array $db = [ 'AllItemDetailPages' => 'Boolean', ]; + private static $extensions = [ + Lumberjack::class, + ]; + private static array $defaults = [ 'AllItemDetailPages' => false, ]; public function getCMSFields() { - $fields = parent::getCMSFields(); - - $fields->addFieldsToTab('Root.Main', [ - CheckboxField::create('AllItemDetailPages', 'Display all item detail pages on this page') - ->setDescription('By default a overview page only shows pages under THIS overview page. '), - ], 'Title'); - - $itemGridConfig = GridFieldConfig_RecordEditor::create(); - - if ($this->AllItemDetailPages === 0) { - $fields->addFieldsToTab('Root.Items', [ - GridField::create('ItemPages', - 'Item detail pages', - ItemDetailPage::get()->where(['ParentID' => $this->ID]), - $itemGridConfig) - ]); - } else { - $fields->addFieldsToTab('Root.Items', [ - GridField::create('ItemPages', - 'Item pages', - ItemDetailPage::get(), - $itemGridConfig) - ]); - } - - - return $fields; + $this->beforeUpdateCMSFields( + function (FieldList $fields) { + $fields->addFieldsToTab('Root.Main', [ + CheckboxField::create('AllItemDetailPages', 'Display all item detail pages on this page') + ->setDescription('By default a overview page only shows pages under THIS overview page. '), + ], 'Title'); + + $itemGridConfig = GridFieldConfig_Lumberjack::create(); + + if ($this->AllItemDetailPages === 0) { + $fields->addFieldsToTab('Root.Items', [ + $this->createGridField('Items', ItemDetailPage::get()->where(['ParentID' => $this->ID])), + ]); + } else { + $fields->addFieldsToTab('Root.Items', [ + $this->createGridField('Items', ItemDetailPage::get()) + ]); + } + } + ); + + return parent::getCMSFields(); } - private static array $allowed_children = [ - ItemDetailPage::class - ]; + private function createGridField(string $title, DataList $list): GridField + { + $config = GridFieldConfig_Lumberjack::create() + ->removeComponentsByType(GridFieldSiteTreeAddNewButton::class) + ->addComponent(new GridFieldAddNewSiteTreeItemButton('buttons-before-left')); + return GridField::create('Items', $title, $list, $config); + } public function getItemPages(?string $limit = null, ?string $all = null): DataList { if ($all == '1' && !$limit) {