0

I want to hide all tabs (local tasks) for non-admin users except the tabs on the user account page (for example, I want to hide tabs on nodes of all content types).

To hide the tabs, I added a check for admin permission in my theme template: templates/menu/menu-local-tasks.html.twig

{% if user.hasPermission('administer nodes') %}
  {% if primary %}
    <h2 class="visually-hidden">{{ 'Primary tabs'|t }}</h2>
    <ul class="tabs--primary nav nav-tabs">{{ primary }}</ul>
  {% endif %}
  {% if secondary %}
    <h2 class="visually-hidden">{{ 'Secondary tabs'|t }}</h2>
    <ul class="tabs--secondary pagination pagination-sm">{{ secondary }}</ul>
  {% endif %}
{% endif %}

This works, but it hides the tabs on the user account page as well.

I tried to find a more specific template of menu-local-tasks for the user account. I turned on twig debugging, but these were the suggestions:

<!-- FILE NAME SUGGESTIONS:
   * block--mytheme-local-tasks.html.twig
   x block--local-tasks-block.html.twig
   * block--core.html.twig
   * block.html.twig
-->

All of these affect the entire site; there is no way to target just the user account local tasks.

Is there a way to do this with theme templates? I chose this approach rather than trying to unset the local tasks in a hook because in a template you can do it in just a line or two of code.

1 Answer 1

1

I wouldn't recommend not doing this by code, this can be achieved using built-in configuration settings, by using a second block with proper rules. Try this:

  1. Go to Structure > Block layout
  2. Select your theme
  3. Place a second tabs block where-ever you want it
  4. Limit the visibility on the block fitting your requirements
    • Most likely limit Pages to something like user/*
    • and by Roles, e.g. Authenticated user
    • (remember that administrators are authenticated users too, so you might need more specific roles to to prevent admins seeing tabs block twice; or add the "hide"-option on user-pages for the first, original tabs block )
2
  • 1
    I totally forgot that tabs were a block in D8. Best way to configure the Pages is to use /user/* on the "user tabs" and NOT /user/* on the "original tabs"; that way, you don't need to set roles. Commented Oct 17, 2017 at 9:46
  • Also note I had to change the theme file name to be more specific: block--mytheme-local-tasks.html.twig Commented Oct 17, 2017 at 10:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.