Skip to content

Commit

Permalink
Merge pull request #59 from hchokshi/fix/root-catalog-page
Browse files Browse the repository at this point in the history
Fix root catalog page - empty parent class array
  • Loading branch information
tractorcow committed Sep 10, 2018
2 parents f5fe0aa + 04da590 commit dc0ab96
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 24 deletions.
5 changes: 2 additions & 3 deletions src/Extensions/AutoPublishSortExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

Expand Down
22 changes: 10 additions & 12 deletions src/Extensions/CatalogPageExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down
9 changes: 5 additions & 4 deletions src/Forms/CatalogPageGridFieldItemRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/ModelAdmin/CatalogPageAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit dc0ab96

Please sign in to comment.