Skip to content

Commit

Permalink
Stash: Trying to fix add new
Browse files Browse the repository at this point in the history
  • Loading branch information
robertvanlienden committed Mar 16, 2023
1 parent 8ec396b commit 573c7a4
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 32 deletions.
6 changes: 6 additions & 0 deletions _config/app.yml
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions src/Pages/ItemDetailPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
74 changes: 42 additions & 32 deletions src/Pages/ItemOverviewPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
*
Expand All @@ -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) {
Expand Down

0 comments on commit 573c7a4

Please sign in to comment.