0

I want to show / hide an option from menu depending some field from user table. Exist a way to do that ? For example if user.isFirst() then hide option 'Product' from menu.

easy_admin:
site_name: '<img src="/build/images/logo.png" alt="dfg" />'
formats:
    datetime: 'Y-m-d'
design:
    brand_color: '#009036'
    color_scheme: 'light'
    form_theme: 'vertical'
    templates:
        layout: 'admin/default/layout.html.twig' #for rewrite layout and add edit my account button

    menu:
    - label: 'menu.labels.dashboard'
      route: 'route_declarant_dashboard'
      icon: 'dashboard'
      default: true
      params:
          menuIndex: 0
    - label: 'menu.labels.products'
      entity: 'Products'
      icon: 'shopping-bag'
    - label: 'menu.labels.users'
      entity: 'Users'
      icon: 'bullhorn'  

So the idea is :

  1. Hide "Products" --> if user.isFirst()

  2. Show "Products" --> other cases.

HareaCostea
  • 145
  • 9

1 Answers1

1

You can extends easy admin menu (doc) by creating a menu.html.twig in the folder app/templates/easy_admin/

{% extends '@!EasyAdmin/default/menu.html.twig' %}

<!-- [...] extract for example -->
{% if is_granted("CAN_SEE_PRODUCT")) and item.label = 'Products' %} <!-- your if condition of viewing menu link -->
    <a href="{{ path }}" {% if item.target|default(false) %}target="{{ item.target }}"{% endif %}>
            {% if item.icon is not empty %}<i class="fa {{ item.icon }}"></i>{% endif %}
            <span>{{ item.label|trans(domain = translation_domain) }}</span>
            {% if item.children|default([]) is not empty %}<span class="pull-right-container"><i class="fa fa-angle-left pull-right"></i></span>{% endif %}
    </a>
{% endif %}
<!-- [...] extract -->

This will only hide the link. The user will still be able to access the other pages unless you change easy admin controller. You can get some inspiration from this github pull request discution about showing menu according to user roles for easy admin.

Nsy
  • 71
  • 4