Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
integrate knpMenu into sonata Admin
- Loading branch information
Amine Zaghdoudi
committed
Feb 18, 2015
1 parent
41ecba8
commit df42796
Showing
11 changed files
with
400 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
KnpMenu | ||
======= | ||
|
||
The admin comes with `KnpMenu <https://github.com/KnpLabs/KnpMenu>`_ integration | ||
It integrates a menu with the KnpMenu library. This menu can be a SonataAdmin service or a route of a custom controller. | ||
|
||
Add a custom controller entry in the menu | ||
----------------------------------------- | ||
|
||
To add a custom controller entry in the admin menu: | ||
|
||
Create your controller | ||
|
||
.. code-block:: php | ||
/** | ||
* @Route("/blog", name="blog_home") | ||
*/ | ||
public function blogAction() | ||
{ | ||
// ... | ||
} | ||
/** | ||
* @Route("/blog/article/{articleId}", name="blog_article") | ||
*/ | ||
public function ArticleAction($articleId) | ||
{ | ||
// ... | ||
} | ||
Add the controller route as an item of the menu | ||
|
||
.. code-block:: yaml | ||
# Default configuration for "SonataAdminBundle" | ||
sonata_admin: | ||
dashboard: | ||
groups: | ||
news: | ||
label: ~ | ||
label_catalogue: ~ | ||
items: | ||
- sonata.news.admin.post | ||
- route: blog_home | ||
label: Blog | ||
- route: blog_article | ||
route_params: { articleId: 3 } | ||
label: Article | ||
... | ||
Also you can override the template of knp_menu used by sonata. The default one is `SonataAdminBundle:Menu:sonata_menu.html.twig`: | ||
|
||
.. code-block:: yaml | ||
# Default configuration for "SonataAdminBundle" | ||
sonata_admin: | ||
templates: | ||
knp_menu_template: ApplicationAdminBundle:Menu:custom_knp_menu.html.twig | ||
... | ||
And voilà, now you have a new menu group which contains an entry to sonata_admin_id, to your blog and to a specific article. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
{% extends 'knp_menu.html.twig' %} | ||
|
||
{% block root %} | ||
{%- set listAttributes = item.childrenAttributes|merge({'class': 'sidebar-menu'}) %} | ||
{%- set request = item.getExtra('request') %} | ||
{{ block('list') -}} | ||
{% endblock %} | ||
|
||
{% block item %} | ||
{%- if item.displayed %} | ||
{#- check role of the group #} | ||
{%- set display = (item.getExtra('roles') is empty or is_granted('ROLE_SUPER_ADMIN') ) %} | ||
{%- for role in item.getExtra('roles') if not display %} | ||
{%- set display = is_granted(role) %} | ||
{%- endfor %} | ||
{%- endif %} | ||
|
||
{%- if item.displayed and display|default %} | ||
{%- set active = false %} | ||
{%- if item.getExtra('admin') is not empty and item.getExtra('admin').hasroute('list') and item.getExtra('admin').isGranted('LIST') and request.get('_sonata_admin') == item.getExtra('admin').code %} | ||
{%- set active = true %} | ||
{%- elseif item.route is defined and request.get('_route') == item.route %} | ||
{%- set active = true %} | ||
{%- else %} | ||
{%- for child in item.children if not active %} | ||
{%- if child.getExtra('admin') is not empty and child.getExtra('admin').hasroute('list') and child.getExtra('admin').isGranted('LIST') and request.get('_sonata_admin') == child.getExtra('admin').code %} | ||
{%- set active = true %} | ||
{%- elseif child.route is defined and request.get('_route') == child.route %} | ||
{%- set active = true %} | ||
{%- endif %} | ||
{%- endfor %} | ||
{%- endif %} | ||
|
||
{%- if item.level == 1%} | ||
{%- do item.setAttribute('class', (item.attribute('class')~' treeview')|trim) %} | ||
{%- endif %} | ||
{%- if active %} | ||
{%- do item.setAttribute('class', (item.attribute('class')~' active')|trim) %} | ||
{%- do item.setChildrenAttribute('class', (item.childrenAttribute('class')~' active')|trim) %} | ||
{%- endif %} | ||
|
||
{%- do item.setChildrenAttribute('class', (item.childrenAttribute('class')~' treeview-menu')|trim) %} | ||
{{ parent() }} | ||
{% endif %} | ||
{% endblock %} | ||
|
||
{% block linkElement %} | ||
{% spaceless %} | ||
{% set translation_domain = item.getExtra('translationdomain', 'messages') %} | ||
{% set icon = item.level > 1 ? '<i class="fa fa-angle-double-right"></i>' : '' %} | ||
{% set is_link = true %} | ||
{{ parent() }} | ||
{% endspaceless %} | ||
{% endblock %} | ||
|
||
{% block spanElement %} | ||
{% spaceless %} | ||
|
||
<a href="#"> | ||
{% set translation_domain = item.attribute('label_catalogue') %} | ||
{% set icon = item.attribute('icon')|default ? item.attribute('icon') : '' %} | ||
{{ icon|default|raw }} | ||
{{ parent() }} | ||
<i class="fa pull-right fa-angle-left"></i> | ||
</a> | ||
{% endspaceless %} | ||
{% endblock %} | ||
|
||
{% block label %}{% if is_link is defined and is_link %}{{ icon|default|raw }}{% endif %}{% if options.allow_safe_labels and item.getExtra('safe_label', false) %}{{ item.label|raw }}{% else %}{{ item.label|trans({}, translation_domain|default('messages')) }}{% endif %}{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.