1. Home
  2. Developers
  3. Remove a menu item from customers area.

Remove a menu item from customers area.

In order to remove a menu item from the customers area, say the Api Keys menu item, we can create a very simple extension to hook into the menu while it is generated and simply remove the given item.

This extension is based on the example extension that should be used each time when you develop mailwizz extensions.

We will name this extension “remove-api-key-item-from-menu” just so that it will be clear what it does and we will install it in the end as described in the extension install guide.

Now, the customers area menu is generated by the class

/apps/customer/components/web/widgets/LeftSideNavigationWidget.php

and if we look inside it for hooks, we can see that somewhere at line 163 it defines following code:

$menuItems = (array)Yii::app()->hooks->applyFilters('customer_left_navigation_menu_items', $menuItems);

This means we can hook into the “customer_left_navigation_menu_items” filter and get access to the $menuItems variable and unset what items we need to.

If the extension folder name is “remove-api-key-item-from-menu” it means our extension entry class file has to be named “RemoveApiKeyItemFromMenuExt.php” and it has to be placed in the “remove-api-key-item-from-menu” folder having the following contents:

<?php defined('MW_PATH') || exit('No direct script access allowed');

/**
 * RemoveApiKeyItemFromMenu Extension
 *
 */

class RemoveApiKeyItemFromMenuExt extends ExtensionInit
{
    // name of the extension as shown in the backend panel
    public $name = 'Remove Api Key Item From Menu';

    // description of the extension as shown in backend panel
    public $description = 'Remove Api Key Item From Menu';

    // current version of this extension
    public $version = '1.0';

    // minimum app version
    public $minAppVersion = '1.3.6.2';

    // the author name
    public $author = 'Your name';

    // author website
    public $website = 'http://www.domain.com/';

    // contact email address
    public $email = 'dev@domain.com';

    /**
     * in which apps this extension is allowed to run
     * '*' means all apps
     * available apps: customer, backend, frontend, api, console
     * so you can use any variation,
     * like: array('backend', 'customer'); or array('frontend');
     */
    public $allowedApps = array('customer');

    /**
     * The run method is the entry point of the extension.
     * This method is called by mailwizz at the right time to run the extension.
     */
    public function run()
    {
        // hook into the menu geberation filter
        Yii::app()->hooks->addFilter('customer_left_navigation_menu_items', function($menuItems){
            // unset the given menu item
            unset($menuItems['api-keys']);

            // and return the rest of items
            return $menuItems;
        });
    }
}

The main part is the run() method, where we hook into the main menu and we unset the menu item we want to.

The extension is also attached so you can play with it to remove other menu items as well, or to even add new ones.

Article Attachments

Was this article helpful?

Related Articles