This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.
For those who wanted to add a notification counter to sideMenu items (like in CMS/Pages) in your plugin but didn't find any documentation, just like myself.
Here's the solution.
in the Plugin registration file add a few settings to the sideMenu item:
'counter' for a static function to calculate the value for the counter 'counterLabel' to add a text hint to the counter
public function registerNavigation()
{
return [
'plugin' => [
'label' => 'Plugin',
'url' => Backend::url('slowslow/plugin/posts'),
'icon' => 'icon-list',
'order' => 10,
'permissions' => ['plugin.*'],
'sideMenu' => [
'posts' => [
'label' => 'Posts',
'icon' => 'icon-list',
'url' => Backend::url('slowslow/plugin/posts'),
'permissions' => ['plugin.access_posts'],
'counter' => SomeClass::someStaticFunction(),
'counterLabel' => 'Published',
],
]
]
];
}
Note, that if the static function returns '0', the counter will show '0'. To switch it off the function shall return null.
Last updated
In this example, a function that returns the number of the counter will be called every time you build the menu, even if at the moment it does not appear to the user (in another section).
- counter - an optional numeric value to output near the menu icon. The value should bea number or a callable returning a number.
Callables: http://php.net/manual/en/language.types.callable.php
Last updated
Do you mean it should be like
'counter' => call_user_func('SomeClass::someStaticFunction'),
?
Last updated
Just in case anyone finds this thread looking how to add a counter to the plugin.yaml file, you can do it like this:
navigation:
(other stuff...)
sideMenu:
messages:
label: Messages
url: acme/plugin/messages
counter: ['\Acme\Plugin\SomeClass', 'someMethod']
counterLabel: Unread Messages
someMethod
would be a static method that returns the number of whatever you are counting.
1-6 of 6