1

I have written a plugin that works fine, it has a subnav which i build like so:

public function getCpNavItem()
  {
    $item = parent::getCpNavItem();
    $item['subnav'] = [
      'moderateentries' => ['label' => 'Moderate Entries', 'url' => 'entry-moderator'],
      'notificationmessages' => ['label' => 'Messages', 'url' => 'entry-moderator/notificationmessages'],
      'log' => ['label' => 'Log', 'url' => 'entry-moderator/log'],
    ];
    return $item;
  }

Problem i am having is that i cannot get the left hand nav item to display as active, the only one that has the sel class applied is 'moderateentries', if i click on the second or third nav item, the first one is the one that is displayed as active with the sel css class. where am i going wrong?

3

Based on the docs and this guide, looks like you'll want to set selectedSubnavItem in your Twig template(s) to whatever the key of your selected subnav item is (moderateentries, notificationmessages, etc).

Alternatively, you could do somthing similar to what Andrew Welch does in his SEOmatic plugin; he sets the selectedSubnavItem variable as a keyed item in a variables array and then right after that renders his plugin template (including that variables array in the render method).

| improve this answer | |
  • Thanks! ok... I looked into that, the menu in question is the one found in the left hand nav. Therefore i have no control over that through twig. But i searched for selectedSubnavItem and found it in 'templates/_layouts/cp.html'. What i havent been able to do is work out how i modify my code to set selectedSubnavItem. I have altered my question above to include what i have tried. – BillyMedia Dec 17 '18 at 17:16
  • Managed to get it working just by adding it to my twig template, didnt realise that by setting it in my twig template, it would carry over into the cp template. – BillyMedia Dec 17 '18 at 17:58
  • Yes, if you set selectedSubnavItem in your plugin's Twig template it will override what is being set in the parent template. Also, you wouldn't set the selectedSubnavItem variable in the getCpNavItem() method, but rather right before you render the plugin's control panel templates. Updated my answer with better info about that 👆️ – Jalen Davenport Dec 17 '18 at 18:07

Your Answer

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

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