<p>Based on <a href="https://docs.craftcms.com/v3/extend/cp-section.html#subnavs" rel="nofollow noreferrer">the docs</a> and <a href="https://craftcms.com/guides/updating-plugins-for-craft-2-5#control-panel-subnavs" rel="nofollow noreferrer">this guide</a>, looks like you'll want to set <code>selectedSubnavItem</code> in your Twig template(s) to whatever the key of your selected subnav item is (<code>moderateentries</code>, <code>notificationmessages</code>, etc).</p>

<p>Alternatively, you could do somthing similar to what Andrew Welch does in his SEOmatic plugin; he <a href="https://github.com/nystudio107/craft-seomatic/blob/e3cd8734d50fd5b6a41bf8bd51d440c936c06acf/src/controllers/SettingsController.php#L141" rel="nofollow noreferrer">sets the <code>selectedSubnavItem</code> variable</a> as a keyed item in a <code>variables</code> array and then right after that <a href="https://github.com/nystudio107/craft-seomatic/blob/e3cd8734d50fd5b6a41bf8bd51d440c936c06acf/src/controllers/SettingsController.php#L192" rel="nofollow noreferrer">renders his plugin template</a> (including that <code>variables</code> array in the render method).</p>

CC BY-SA 4.0
2018-12-17T17:16:32.533Z
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
CC BY-SA 4.0
2018-12-17T17:58:23.810Z
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
CC BY-SA 4.0
2018-12-17T18:07:45.803Z
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
CC BY-SA 4.0
2018-12-17T14:37:42.927Z
2018-12-17T18:13:39.883Z
2018-12-17T18:13:39.883Z
Jalen Davenport
<p>I have written a plugin that works fine, it has a subnav which i build like so:</p>

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

<p>Problem i am having is that i cannot get the left hand nav item to display as active, the only one that has the <code>sel</code> 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 <code>sel</code> css class. where am i going wrong?</p>

<plugin-development><craft3><navigation>
Setting plugin subnav to active
# Setting plugin subnav to active

(Asked by: username_0 on 2018-12-17)

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?

**Tags:** plugin-development, craft3, navigation

---

## 1 answers

### Answer by username_1 on 2018-12-17. Score: 4 | Accepted answer

Based on [the docs](https://docs.craftcms.com/v3/extend/cp-section.html#subnavs) and [this guide](https://craftcms.com/guides/updating-plugins-for-craft-2-5#control-panel-subnavs), 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](https://github.com/nystudio107/craft-seomatic/blob/e3cd8734d50fd5b6a41bf8bd51d440c936c06acf/src/controllers/SettingsController.php#L141) as a keyed item in a `variables` array and then right after that [renders his plugin template](https://github.com/nystudio107/craft-seomatic/blob/e3cd8734d50fd5b6a41bf8bd51d440c936c06acf/src/controllers/SettingsController.php#L192) (including that `variables` array in the render method).

> **Comments:**
> **username_0**: 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.  
> **username_0**: 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.  
> **username_1**: 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 ️  

---

