How to code a plugin that hides menu items from non-superadmins_

I have been trying it for a few hours, but I can’t manage to get it.

I have a working custom plug-in that works fine. I create Menu.php and put the following:

namespace Piwik\Plugins\ESPlugin;

use Piwik\Menu\MenuAdmin;
use Piwik\Piwik;

class Menu extends \Piwik\Plugin\Menu
{

    public function configureAdminMenu(MenuAdmin $menu)
    {
        if (!Piwik::hasUserSuperUserAccess()) {
                $menu->remove($menuName='CorePluginsAdmin_MenuPlatform');
                $menu->remove($subMenuName='Marketplace_Marketplace');
        }
    }

}

but nothing works. I originally want to either remove the items individually or the whole category. Could someone help me out? I can’t get this to work despite reading the doc (https://developer.matomo.org/api-reference/Piwik/Menu/MenuAdmin).

Hi,

Forget this post, thanks to @SteveG I now know the proper solution (see below)

I’m pretty sure the correct call is

    public function configureTopMenu(MenuTop $menu)
    {
        $menu->remove('CorePluginsAdmin_MenuPlatform', 'Marketplace_Marketplace');
    }

(you can add the superuser check).

But for whatever reason I can’t find out myself, this does not work.

Also keep in mind that all this does is remove the link, not make it impossible for people to access the page, but I think you know that (https://github.com/matomo-org/matomo/issues/7115).

So all I can offer now is going the ugly way and modifying the Matomo source (which of course gets lost on an update):

diff --git a/plugins/Marketplace/Menu.php b/plugins/Marketplace/Menu.php
index 1a366f6168..18b368b0fb 100644
--- a/plugins/Marketplace/Menu.php
+++ b/plugins/Marketplace/Menu.php
@@ -18,7 +18,7 @@ class Menu extends \Piwik\Plugin\Menu
 
     public function configureAdminMenu(MenuAdmin $menu)
     {
-        if (!Piwik::isUserIsAnonymous()) {
+        if (!Piwik::isUserIsAnonymous() && Piwik::hasUserSuperUserAccess()) {
             $menu->addPlatformItem('Marketplace_Marketplace',
                 $this->urlForAction('overview', array('activated' => '', 'mode' => 'admin', 'type' => '', 'show' => '')),
                 $order = 5);

and

diff --git a/plugins/Marketplace/Widgets/GetNewPlugins.php b/plugins/Marketplace/Widgets/GetNewPlugins.php
index d34a40d888..00a982d39d 100644
--- a/plugins/Marketplace/Widgets/GetNewPlugins.php
+++ b/plugins/Marketplace/Widgets/GetNewPlugins.php
@@ -33,7 +33,7 @@ class GetNewPlugins extends Widget
         $config->setCategoryId('Marketplace_Marketplace');
         $config->setName('Marketplace_LatestMarketplaceUpdates');
         $config->setOrder(19);
-        $config->setIsEnabled(!Piwik::isUserIsAnonymous());
+        $config->setIsEnabled(Piwik::hasUserSuperUserAccess());
     }
 
     public function render()
@@ -59,4 +59,4 @@ class GetNewPlugins extends Widget
         ));
     }
diff --git a/plugins/Marketplace/Widgets/GetPremiumFeatures.php b/plugins/Marketplace/Widgets/GetPremiumFeatures.php
index 8f27f8745f..f8bd63b602 100644
--- a/plugins/Marketplace/Widgets/GetPremiumFeatures.php
+++ b/plugins/Marketplace/Widgets/GetPremiumFeatures.php
@@ -35,7 +35,7 @@ class GetPremiumFeatures extends Widget
         $config->setSubcategoryId('Marketplace_PaidPlugins');
         $config->setName('Marketplace_PaidPlugins');
         $config->setOrder(20);
-        $config->setIsEnabled(!Piwik::isUserIsAnonymous());
+        $config->setIsEnabled(Piwik::hasUserSuperUserAccess());
     }
 
     public function render()
@@ -60,4 +60,4 @@ class GetPremiumFeatures extends Widget
         ));
     }
diff --git a/plugins/Marketplace/Widgets/Marketplace.php b/plugins/Marketplace/Widgets/Marketplace.php
index 678ae31ee8..426318668e 100644
--- a/plugins/Marketplace/Widgets/Marketplace.php
+++ b/plugins/Marketplace/Widgets/Marketplace.php
@@ -28,8 +28,8 @@ class Marketplace extends Widget
         $config->setParameters(array('embed' => '1'));
         $config->setIsNotWidgetizable();
         $config->setOrder(19);
-        $config->setIsEnabled(!Piwik::isUserIsAnonymous());
+        $config->setIsEnabled(Piwik::hasUserSuperUserAccess());
     }
1 Like

It turns out you of course have to use configureAdminMenu instead.

So this successfully removes the marketplace from the admin menu:

class Menu extends \Piwik\Plugin\Menu
{

    public function configureAdminMenu(MenuAdmin $menu)
    {
        $menu->remove('CorePluginsAdmin_MenuPlatform', 'Marketplace_Marketplace');
    }
}

And removing the Widget in the reporting menu is also rather easy:

class MenuEditTest extends \Piwik\Plugin
{
    public function registerEvents()
    {
        return [
            'Widget.filterWidgets' => 'filterWidgets',
        ];
    }

    /**
     * @param WidgetsList $list
     */
    public function filterWidgets($list)
    {
        $list->remove('Marketplace_Marketplace');
    }
}
1 Like

Thanks a lot for your help! This is exactly what I was looking for :smiley: