Skip to content

Commit

Permalink
Implement sorting for element list types
Browse files Browse the repository at this point in the history
  • Loading branch information
wilr committed Jun 24, 2016
1 parent c191426 commit 5a1b263
Showing 1 changed file with 56 additions and 21 deletions.
77 changes: 56 additions & 21 deletions code/models/ElementList.php
Expand Up @@ -30,42 +30,31 @@ class ElementList extends BaseElement

private static $enable_title_in_template = true;

/**
* @return FieldList
*/
public function getCMSFields()
{
$elements = $this->Elements();
$isInDb = $this->isInDB();
$allowed = $this->config()->get('allowed_elements');
$disallowed = (array) $this->config()->get('disallowed_elements');

$this->beforeUpdateCMSFields(function ($fields) use ($elements, $isInDb, $allowed, $disallowed) {
$this->beforeUpdateCMSFields(function ($fields) use ($elements, $isInDb) {
$fields->removeByName('Root.Elements');
$fields->removeByName('Elements');

$desc = HTMLEditorField::create('ListDescription', 'List Description');
$desc->setRightTitle('Optional');
$fields->addFieldToTab('Root.Main', $desc);


if ($isInDb) {
$adder = new GridFieldAddNewMultiClass();

if (is_array($allowed)) {
$list = $allowed;
} else {
$classes = ClassInfo::subclassesFor('BaseElement');
$list = array();
unset($classes['BaseElement']);

foreach ($classes as $class) {
if (in_array($class, $disallowed)) {
continue;
}
$list[$class] = singleton($class)->i18n_singular_name();
}
}
$adder = new ElementalGridFieldAddNewMultiClass();

asort($list);
$list = $this->getAvailableTypes();

$adder->setClasses($list);
if($list) {
$adder->setClasses($list);
}

$config = GridFieldConfig_RecordEditor::create(100);
$config->addComponent(new GridFieldSortableRows('Sort'));
Expand All @@ -91,6 +80,52 @@ public function getCMSFields()
return parent::getCMSFields();
}

/**
* @return array
*/
public function getAvailableTypes() {
if (is_array($this->config()->get('allowed_elements'))) {
$list = $this->config()->get('allowed_elements');

if($this->config()->get('sort_types_alphabetically') !== false) {
$sorted = array();

foreach ($list as $class) {
$inst = singleton($class);

if ($inst->canCreate()) {
$sorted[$class] = singleton($class)->i18n_singular_name();
}
}

$list = $sorted;
asort($list);
}
} else {
$classes = ClassInfo::subclassesFor('BaseElement');
$list = array();
unset($classes['BaseElement']);

$disallowedElements = (array) $this->config()->get('disallowed_elements');

foreach ($classes as $class) {
$inst = singleton($class);

if (!in_array($class, $disallowedElements) && $inst->canCreate()) {
$list[$class] = singleton($class)->i18n_singular_name();
}
}

asort($list);
}

if (method_exists($this, 'sortElementalOptions')) {
$this->sortElementalOptions($list);
}

return $list;
}

/**
* Used in template instead of {@link Widgets()} to wrap each widget in its
* controller, making it easier to access and process form logic and
Expand Down

0 comments on commit 5a1b263

Please sign in to comment.