Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetch all and sort menu sets #24

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 %>
<a href="$Link" class="$LinkingMode">$MenuTitle</a>
<% 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 %>

###Code guidelines

Expand Down
18 changes: 18 additions & 0 deletions code/MenuAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
13 changes: 11 additions & 2 deletions code/MenuManagerTemplateProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ class MenuManagerTemplateProvider implements TemplateGlobalProvider
public static function get_template_global_variables()
{
return array(
'MenuSet' => 'MenuSet'
'MenuSet' => 'MenuSet',
'MenuSets' => 'MenuSets'
);
}

Expand All @@ -25,4 +26,12 @@ public static function MenuSet($name)
)
)->first();
}
}

/**
* @return DataList
*/
public static function MenuSets()
{
return MenuSet::get();
}
}
8 changes: 7 additions & 1 deletion code/MenuSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ class MenuSet extends DataObject implements PermissionProvider
* @var array
*/
private static $db = array(
'Name' => 'Varchar(255)'
'Name' => 'Varchar(255)',
'Sort' => 'Int'
);

/**
Expand Down Expand Up @@ -43,6 +44,11 @@ public function providePermissions()
);
}

/**
* @var string
*/
private static $default_sort = 'Sort';

/**
* @param mixed $member
* @return boolean
Expand Down