Skip to content

Commit

Permalink
Merge pull request #9 from stecman/default-menu-sets
Browse files Browse the repository at this point in the history
Allow configuring default menu sets in yaml config
  • Loading branch information
glenn-bautista committed Feb 11, 2015
2 parents ba3622f + 725a4cd commit 21f705e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,16 @@ There are 2 main steps to creating a menu using menu management.

### Creating a MenuSet

This is pretty straight forward. You just give the MenuSet a Name (which is what
you reference in the templates when controlling the menu)
This is pretty straight forward. You just give the MenuSet a Name (which is what you reference in the templates when controlling the menu).

As it is common to reference MenuSets by name in templates, you can configure sets to be created automatically during the /dev/build task. These sets cannot be deleted through the CMS.

```yaml
MenuSet:
default_sets:
- Main
- Footer
```
### Creating MenuItems
Expand Down
45 changes: 41 additions & 4 deletions code/MenuSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function canCreate($member = null)
*/
public function canDelete($member = null)
{
return Permission::check('MANAGE_MENU_SETS');
return !$this->isDefaultSet() && Permission::check('MANAGE_MENU_SETS');
}
/**
* @param null $member
Expand All @@ -77,6 +77,36 @@ public function Children()
{
return $this->MenuItems();
}

/**
* Check if this menu set appears in the default sets config
* @return bool
*/
public function isDefaultSet()
{
return in_array($this->Name, $this->getDefaultSetNames());
}

/**
* Set up default records based on the yaml config
*/
public function requireDefaultRecords()
{
parent::requireDefaultRecords();

foreach ($this->getDefaultSetNames() as $name) {
$existingRecord = MenuSet::get()->filter('Name', $name)->first();

if (!$existingRecord) {
$set = new MenuSet();
$set->Name = $name;
$set->write();

DB::alteration_message("MenuSet '$name' created", 'created');
}
}
}

/**
* @return FieldList
*/
Expand Down Expand Up @@ -106,9 +136,7 @@ public function getCMSFields()

return $fields;
}
/**
*
*/

public function onBeforeDelete()
{
$menuItems = $this->MenuItems();
Expand All @@ -122,4 +150,13 @@ public function onBeforeDelete()
parent::onBeforeDelete();
}

/**
* Get the MenuSet names configured under MenuSet.default_sets
*
* @return string[]
*/
protected function getDefaultSetNames()
{
return $this->config()->get('default_sets') ?: array();
}
}

0 comments on commit 21f705e

Please sign in to comment.