Skip to content

Commit

Permalink
feat: add ability to reorder menu sets (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Penny authored and wilr committed Oct 5, 2022
1 parent 7a93b58 commit 29f8134
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 2 deletions.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,24 @@ content._
<% end_loop %>
```

To loop through *all* MenuSets and their items:

<% loop $MenuSets %>
<% loop $MenuItems %>
<a href="$Link" class="$LinkingMode">$MenuTitle</a>
<% 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) %>
<a href="$Link" class="$LinkingMode">$MenuTitle</a>
<% end_loop %>
<% end_loop %>

#### Enabling partial caching

[Partial caching](https://docs.silverstripe.org/en/4/developer_guides/performance/partial_caching/)
Expand All @@ -135,6 +153,15 @@ can be enabled with your menu to speed up rendering of your templates.
<% end_with %>
```

### Allow sorting of MenuSets

By default menu sets cannot be sorted, however, you can set your configuration to allow it.

```yaml
Heyday\MenuManager\MenuSet:
allow_sorting: true
```


### Code guidelines

Expand Down
10 changes: 10 additions & 0 deletions src/MenuAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
namespace Heyday\MenuManager;

use SilverStripe\Admin\ModelAdmin;
use SilverStripe\Core\Config\Config;
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\GridField\GridFieldAddNewButton;
use SilverStripe\Forms\GridField\GridFieldImportButton;
use Symbiote\GridFieldExtensions\GridFieldOrderableRows;

/**
* Class MenuAdmin
Expand Down Expand Up @@ -66,6 +68,14 @@ public function getEditForm($id = null, $fields = null)
}
}

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;
}
}
13 changes: 12 additions & 1 deletion src/MenuManagerTemplateProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ class MenuManagerTemplateProvider implements TemplateGlobalProvider
public static function get_template_global_variables()
{
return [
'MenuSet' => 'MenuSet'
'MenuSet' => 'MenuSet',
'MenuSets' => 'MenuSets'
];
}

Expand All @@ -30,6 +31,16 @@ public static function MenuSet($name)
return Injector::inst()->get(self::class)->findMenuSetByName($name);
}

/**
* @param $name
* @return MenuSet|null
*/
public static function MenuSets()
{
return MenuSet::get();
}


/**
* Find a MenuSet by name
*
Expand Down
5 changes: 4 additions & 1 deletion src/MenuSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ class MenuSet extends DataObject implements PermissionProvider

private static $db = [
'Name' => 'Varchar(255)',
'Description' => 'Text'
'Description' => 'Text',
'Sort' => 'Int'
];

private static $has_many = [
Expand All @@ -41,6 +42,8 @@ class MenuSet extends DataObject implements PermissionProvider
'Description'
];

private static $default_sort = 'Sort ASC';

/**
* @return array
*/
Expand Down

0 comments on commit 29f8134

Please sign in to comment.