Adding your module's menu to the "Settings" menu in the admin

squatto - 3 weeks ago

I recently created a "Contacts" module to manage a list of contacts. They're used to populate some fields throughout the system. Nothing complicated. A simple CRUD module. However, I didn't want a full parent-child menu in admin menu, just for one simple thing:

What I didn't want

That's what I didn't want

Instead, I wanted to put "Contacts" into the "Settings" menu. As with most things in Pyro, it's quite easy (once I figured out what I was doing with the help of everyone in the Slack channel!)

In contacts-module/src/ContactsModuleServiceProvider.php, in a boot() method:

use Anomaly\Streams\Platform\Addon\AddonCollection;
use Illuminate\Routing\Router;

public function boot(AddonCollection $addonCollection, Router $router)
{
    // create a "Contacts" section in the "Settings" menu
    $slug = 'contacts';
    $section = [
        'title'   => 'myaddon.module.contacts::addon.section.contacts',
        'href'    => 'admin/contacts',
        'buttons' => [
            'new' => [
                'text' => 'myaddon.module.contacts::button.new_contact',
            ],
        ],
    ];
    $addonCollection->get('anomaly.module.settings')->addSection($slug, $section);

    // define routes
    // you have to set streams::addon to anomaly.module.settings in order for "Settings" to stay active in the menu
    $router->any('admin/contacts', [
        'uses'           => 'MyAddon\ContactsModule\Http\Controller\Admin\ContactsController@index',
        'streams::addon' => 'anomaly.module.settings',
    ]);

    $router->any('admin/contacts/create', [
        'uses'           => 'MyAddon\ContactsModule\Http\Controller\Admin\ContactsController@create',
        'streams::addon' => 'anomaly.module.settings',
    ]);

    $router->any('admin/contacts/edit/{id}', [
        'uses'           => 'MyAddon\ContactsModule\Http\Controller\Admin\ContactsController@edit',
        'streams::addon' => 'anomaly.module.settings',
    ]);
}

Some key things to notice:
1) I had to explicitly point the section title and buttons to the contacts module language keys
2) You have to set href on the section or it is appended to admin/settings/ (in other words, you end up with admin/settings/contacts)
3) You have to set streams::addon in each route to anomaly.module.settings in order for the "Settings" menu to remain active
4) In your module (ContactsModule.php for me) you need to set protected $navigation = false; so that it doesn't show up in the admin menu

ryanthompson - 3 weeks ago

Superb!

stevenweijdt - 3 weeks ago

awesome! Thanks for this how-to

jolusadev - 2 weeks ago

Great! Thanks for sharing.

alireza - 2 weeks ago

Nice! Thanks for sharing it.