From ef98f31e26290f8c1846b63459b32de85b652a2c Mon Sep 17 00:00:00 2001 From: Chris Penny Date: Fri, 14 Oct 2016 12:42:10 +1300 Subject: [PATCH 1/3] Added method "MenuSets" to "get_template_global_variables" Allows devs to fetch all created MenuSets, or optionally fetch all and ".Limit()" --- code/MenuManagerTemplateProvider.php | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/code/MenuManagerTemplateProvider.php b/code/MenuManagerTemplateProvider.php index 1c46876..0e7bdee 100644 --- a/code/MenuManagerTemplateProvider.php +++ b/code/MenuManagerTemplateProvider.php @@ -8,7 +8,8 @@ class MenuManagerTemplateProvider implements TemplateGlobalProvider public static function get_template_global_variables() { return array( - 'MenuSet' => 'MenuSet' + 'MenuSet' => 'MenuSet', + 'MenuSets' => 'MenuSets' ); } @@ -25,4 +26,14 @@ public static function MenuSet($name) ) )->first(); } -} \ No newline at end of file + + /** + * @return DataList + */ + public static function MenuSets() + { + $menuSets = MenuSet::get(); + + return $menuSets; + } +} From 8fa2848cf490fa2e7de442c11c48627bfb044b06 Mon Sep 17 00:00:00 2001 From: Chris Penny Date: Fri, 14 Oct 2016 12:43:08 +1300 Subject: [PATCH 2/3] Add config and support for MenuSets to be sorted in ModelAdmin. --- README.md | 27 +++++++++++++++++++++++++++ code/MenuAdmin.php | 18 ++++++++++++++++++ code/MenuSet.php | 8 +++++++- 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c6359a7..e6f040c 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,14 @@ MenuSet: - Footer ``` +### Allow sorting of MenuSets + +By default menu sets cannot be sorted, however, you can set your configuration to allow it. + +```yaml +MenuSet: + allow_sorting: true +``` ### Creating MenuItems @@ -83,10 +91,29 @@ Can be used as a check to see if 'target="_blank"' should be added to links. ### Usage in template +To loop through a specific MenuSet's items: + <% loop $MenuSet('YourMenuName').MenuItems %> $MenuTitle <% end_loop %> +To loop through *all* MenuSets and their items: + + <% loop $MenuSets %> + <% loop $MenuItems %> + $MenuTitle + <% end_loop %> + <% end_loop %> + +Optionally you can also limit the number of MenuSets and MenuItems that are looped through. + +The example below will fetch the top 4 MenuSets (as seen in Menu Management), and the top 5 MenuItems for each: + + <% loop $MenuSets.Limit(4) %> + <% loop $MenuItems.Limit(5) %> + $MenuTitle + <% end_loop %> + <% end_loop %> ###Code guidelines diff --git a/code/MenuAdmin.php b/code/MenuAdmin.php index 55476b1..33867ce 100644 --- a/code/MenuAdmin.php +++ b/code/MenuAdmin.php @@ -26,4 +26,22 @@ class MenuAdmin extends ModelAdmin * @var array */ private static $model_importers = array(); + + /** + * @param mixed $id + * @param mixed $fields + * @return ModelAdmin + */ + public function getEditForm($id = null, $fields = null) { + $form = parent::getEditForm($id, $fields); + + if (Config::inst()->get($this->modelClass, 'allow_sorting')) { + $gridFieldName = $this->sanitiseClassName($this->modelClass); + $gridField = $form->Fields()->fieldByName($gridFieldName); + + $gridField->getConfig()->addComponent(new GridFieldOrderableRows()); + } + + return $form; + } } diff --git a/code/MenuSet.php b/code/MenuSet.php index 2d25605..621fa6e 100644 --- a/code/MenuSet.php +++ b/code/MenuSet.php @@ -9,7 +9,8 @@ class MenuSet extends DataObject implements PermissionProvider * @var array */ private static $db = array( - 'Name' => 'Varchar(255)' + 'Name' => 'Varchar(255)', + 'Sort' => 'Int' ); /** @@ -43,6 +44,11 @@ public function providePermissions() ); } + /** + * @var string + */ + private static $default_sort = 'Sort'; + /** * @param mixed $member * @return boolean From 1d8e8509ede76e61239310fafeccba3b972a8d8f Mon Sep 17 00:00:00 2001 From: Chris Penny Date: Fri, 14 Oct 2016 15:41:46 +1300 Subject: [PATCH 3/3] Don't save "MenuSet::get()" to variable before returning. --- code/MenuManagerTemplateProvider.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/code/MenuManagerTemplateProvider.php b/code/MenuManagerTemplateProvider.php index 0e7bdee..3026478 100644 --- a/code/MenuManagerTemplateProvider.php +++ b/code/MenuManagerTemplateProvider.php @@ -32,8 +32,6 @@ public static function MenuSet($name) */ public static function MenuSets() { - $menuSets = MenuSet::get(); - - return $menuSets; + return MenuSet::get(); } }