diff --git a/src/Extensions/AutoPublishSortExtension.php b/src/Extensions/AutoPublishSortExtension.php index 21d63fd..a9768b7 100644 --- a/src/Extensions/AutoPublishSortExtension.php +++ b/src/Extensions/AutoPublishSortExtension.php @@ -3,6 +3,7 @@ namespace LittleGiant\CatalogManager\Extensions; use SilverStripe\CMS\Model\SiteTree; +use SilverStripe\Core\Config\Config; use SilverStripe\Core\Extension; use SilverStripe\ORM\DataObject; use SilverStripe\ORM\DB; @@ -23,9 +24,7 @@ class AutoPublishSortExtension extends Extension public function onAfterReorderItems(SS_List &$list, array $values, array $sortedIDs) { $modelClass = $list->dataClass(); - /** @var CatalogPageExtension|SiteTree $model */ - $model = singleton($modelClass); - if (!$model::config()->get('automatic_live_sort')) { + if (!Config::forClass($modelClass)->get('automatic_live_sort')) { return; } diff --git a/src/Extensions/CatalogPageExtension.php b/src/Extensions/CatalogPageExtension.php index 5166261..2645760 100644 --- a/src/Extensions/CatalogPageExtension.php +++ b/src/Extensions/CatalogPageExtension.php @@ -69,22 +69,20 @@ public function isPublishedNice() */ public function updateCMSFields(FieldList $fields) { - $parentClass = $this->getParentClasses(); - $pages = $this->getCatalogParents(); - - if ($pages === null) { + $parentPages = $this->getCatalogParents(); + if ($parentPages === null) { + // Root page return; } - $parentCount = $pages->count(); - + $parentCount = count($parentPages); if ($parentCount === 0) { - throw new Exception('You must create a parent page with one of these classes: ' . implode(', ', $parentClass)); + throw new Exception('You must create a parent page with one of these classes: ' . implode(', ', $this->getParentClasses())); } elseif ($parentCount === 1) { - $field = HiddenField::create('ParentID', 'ParentID', $pages->first()->ID); + $field = HiddenField::create('ParentID', 'ParentID', $parentPages->first()->ID); } else { - $defaultParentID = $this->owner->ParentID ?: $pages->first()->ID; - $field = DropdownField::create('ParentID', _t(__CLASS__ . '.PARENTPAGE', 'Parent Page'), $pages->map(), $defaultParentID); + $defaultParentID = $this->owner->ParentID ?: $parentPages->first()->ID; + $field = DropdownField::create('ParentID', _t(__CLASS__ . '.PARENTPAGE', 'Parent Page'), $parentPages->map(), $defaultParentID); } $fields->addFieldToTab('Root.Main', $field); @@ -113,7 +111,7 @@ public function getCatalogParents() $parentClasses = $this->getParentClasses(); $parents = null; - if ($parentClasses !== null) { + if (!empty($parentClasses)) { $parents = SiteTree::get()->filter('ClassName', $parentClasses); } $this->owner->extend('updateCatalogParents', $parents); @@ -158,7 +156,7 @@ public static function getClassSortFieldName($class) */ public function canCreate($member) { - return $this->getCatalogParents()->count() === 0 + return count($this->getCatalogParents()) === 0 ? false : null; } diff --git a/src/Forms/CatalogPageGridFieldItemRequest.php b/src/Forms/CatalogPageGridFieldItemRequest.php index bbe9d30..ae537d3 100644 --- a/src/Forms/CatalogPageGridFieldItemRequest.php +++ b/src/Forms/CatalogPageGridFieldItemRequest.php @@ -23,11 +23,12 @@ class CatalogPageGridFieldItemRequest extends VersionedGridFieldItemRequest */ public function ItemEditForm() { - if (!$this->record->ParentID) { - // set a parent id for the record, even if it will change + if (!empty($this->record) && !$this->record->ParentID) { + // Set a default parent id for the record, even if it will change $parents = $this->record->getCatalogParents(); - if ($parents !== null && $parents->exists()) { - $this->record->ParentID = $parents->first()->ID; + $first = $parents !== null ? $parents->first() : null; + if ($first !== null) { + $this->record->ParentID = $first->ID; } } diff --git a/src/ModelAdmin/CatalogPageAdmin.php b/src/ModelAdmin/CatalogPageAdmin.php index b5f579f..9b16e75 100755 --- a/src/ModelAdmin/CatalogPageAdmin.php +++ b/src/ModelAdmin/CatalogPageAdmin.php @@ -47,7 +47,7 @@ public function getEditForm($id = null, $fields = null) $model = singleton($this->modelClass); if ($model->has_extension(CatalogPageExtension::class)) { - $form = $this->getCatalogEditForm($id, $fields, $model); + $form = $this->getCatalogEditForm($model, $id, $fields); } elseif (method_exists($model, 'getAdminListField')) { $form = Form::create( $this, @@ -64,12 +64,12 @@ public function getEditForm($id = null, $fields = null) } /** - * @param null $id - * @param null $fields * @param \SilverStripe\CMS\Model\SiteTree|\LittleGiant\CatalogManager\Extensions\CatalogPageExtension $model + * @param null|string $id + * @param null|string $fields * @return \SilverStripe\Forms\Form */ - protected function getCatalogEditForm($id = null, $fields = null, $model) + protected function getCatalogEditForm($model, $id = null, $fields = null) { $originalStage = Versioned::get_stage(); Versioned::set_stage(Versioned::DRAFT); @@ -102,7 +102,7 @@ protected function getCatalogEditForm($id = null, $fields = null, $model) new FieldList() )->setHTMLID('Form_EditForm'); - if ($model->getCatalogParents()->count() === 0) { + if (count($model->getCatalogParents()) === 0) { $form->setMessage($this->getMissingParentsMessage($model), ValidationResult::TYPE_WARNING); }