Search code examples
phpsilverstripesilverstripe-4

How to add a navigation item in Silverstripe while making a module?


I am trying to build a plugin/module for Silverstripe and I am trying to understand how to add a menu item in the CMS admin panel.

CMS admin panel navigation

This is where I want to add a new nav item and attach it to a view.

I looked through the API and I found SilverStripeNavigatorItem.html this page but I am not sure how to use it.

I am using the silverstripe-module module skeleton provided by Silverstripe official docs.

How do I add this navigation item in the admin panel and attach a view to it in the admin panel itself?


Solution

  • To create a completely custom admin screen we can create a class that extends LeftAndMain.

    app/src/Admin/TestAdmin.php

    <?php
    
    use SilverStripe\Admin\LeftAndMain;
    
    class TestAdmin extends LeftAndMain
    {
        private static $url_segment = 'test';
        private static $menu_title = 'Test';
        private static $menu_priority = 100;
        private static $menu_icon = 'public/images/treeicons/test.png';
    
    }
    

    $menu_priority allows us to set the order of the menu item relative to the other menu items. This is optional.

    $menu_icon allows us to set a custom icon for our menu item. This is optional.

    We then create a TestAdmin_EditForm.ss template with the following:

    app/templates/Includes/TestAdmin_EditForm.ss

    <div class="toolbar toolbar--north cms-content-header vertical-align-items">
        <div class="cms-content-header-info flexbox-area-grow vertical-align-items">
            <% include SilverStripe\\Admin\\BackLink_Button %>
            <% with $Controller %>
                <% include SilverStripe\\Admin\\CMSBreadcrumbs %>
            <% end_with %>
        </div>
    </div>
    
    <div class="panel panel--padded panel--scrollable flexbox-area-grow cms-panel-padded">
        <h1>Hello</h1>
    </div>