2

I am making a plugin where I have about 10 tabs at the top. Depending on the admin users privileges they may see a subset of 1-5 or 6-10 and super admins can see all 10. The issue I am running into is I built the logic to show appropriate tabs for the user based on their privilege and it works fine. What I now want to do is include that into an include file so I don't have the 60 lines of code on each of the 10 pages in the plugin.

I know I can put it into the main layout template in _layouts/cp but my logic is checking some variables that are set on each page of the plugin. So if I put it in there none of my variables are set yet so the tabs don't show as nothing will match. Example _layouts/cp template.

{% if user.superAdmin == "no" %}
  {% if color == "red" %}
    {% if usercolor != "red" %}
      {% redirect redirectUrl %}
    {% else %}
      {% set tabs = { 
       red: {label: "red", url: url('admin/pluginname')},
       darkred: {label: "dark red", url: url('admin/pluginname/darkred')}
       } %}
    {% endif %}
 {% else %}
    {% if usercolor != "blue" %}
       {% redirect redirectUrl %}
    {% else %}
      {% set tabs = { 
       blue: {label: "blue", url: url('admin/pluginname/blue')},
       darkblue: {label: "dark blue", url: url('admin/pluginname/darkblue')}
       } %}
    {% endif %}
 {% endif %}
{% else %}
      {% set tabs = { 
       red: {label: "red", url: url('admin/pluginname')},
       darkred: {label: "dark red", url: url('admin/pluginname/darkred')},
       blue: {label: "blue", url: url('admin/pluginname/blue')},
       darkblue: {label: "dark blue", url: url('admin/pluginname/darkblue')
       } %}
{% endif %}

{% set block('content') %}

In each template file I will set what color the user is associated to and it should give a different set of tabs. Example of template file.

{% set redirectUrl = "/admin/pluginname/darkblue" %}

{% set color = "darkred" %}

{% set usercolor = user.userColor %}

{% extends "_layouts/cp" %}

{% set selectedTab = "darkred" %}

{% set content %}

{% endset %}  

If I move all my tab logic in the layout file and put it just below the extends _layout/cp the tabs work fine but not when it is in the layouts file. I am sure there is an easy fix that I am just missing something but any help would be appreciated. Not the end of the world if I have to have the tab logic within each template file but not ideal for any future changes.

Thanks

  • Is there a space in your {% end if %} ? – Clark Nelson Jun 5 '18 at 17:50
  • Yes there was but this is not my actual code. Just wrote it up for the question. Code I have works if I move it from layout file and put it into template file so i know there are no errors such as that making it not work. I fixed the spacing error in the question. Thanks – Chris Jun 5 '18 at 18:02
  • Do you have a specific question? I'm a bit confused as to what you are looking for. – Clark Nelson Jun 6 '18 at 18:17
  • My question is how can I include the tab data in the master layout file? Currently when I do no tabs show up on any of the pages but if I move all of the code into each child template file the tabs show up fine. I just don't like having the same 50-60 lines of redundant code in 10 files when they all share a same master parent which is where the code should live. – Chris Jun 6 '18 at 20:28
  • i'm not very familiar with the CP layout files, but can you use twig macros? – Clark Nelson Jun 7 '18 at 16:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.