How to build a custom module


#1

Hi everybody,

I want to learn how to build a custom admin module from scratch.

There should be a menu item in the left navigation and a blank page for main where I want to do custom things like getting and showing data from database and so on …

I searched for a module skeleton or boilerplate but found nothing.

May anybody help me out?

Thx,
D


#2

With regards to creating a module, there are a couple of guides:

https://docs.silverstripe.org/en/4/developer_guides/extending/modules/
https://docs.silverstripe.org/en/4/developer_guides/extending/how_tos/publish_a_module/

If you just want a fairly simple admin screen that shows a list of DataObjects in a GridField then Creating your own extension of ModelAdmin is the best place to start:

https://docs.silverstripe.org/en/4/developer_guides/customising_the_admin_interface/modeladmin/

Finally, there is a load of information about customising the SilverSrtripe admin here:

https://docs.silverstripe.org/en/4/developer_guides/customising_the_admin_interface/


#4

Hi PsychoMo,

thank you for your replay. As Fabian_Schrattenholz mentioned, the docs are not very useful. I know how to work with ModelAdmin.

What I have is this:

<?php

use SilverStripe\Admin\LeftAndMain;
use SilverStripe\Security\PermissionProvider;

class TestAdmin extends LeftAndMain implements PermissionProvider
{
    private static $url_segment = 'test';
    private static $menu_title = 'Test';
    private static $menu_icon_class = 'font-icon-chart-line';
    public function init()
    {
        parent::init();        
    }    
}

This generates me a admin menu and blank page. But how can I show custom content there? What are the best practices to render HTML to the page. How JS (Entwine) is used, wow Ajax work there on so on …


#3

Hej PschoMo,

the dev guide doc is not that detailed.
I wrote a module which works fine. But the templates that have to be exposed to public are not found from silvertripe. I have to put them into the themes/mytheme/templates folder manually

do I have to change something in theme.yml of app?

thanks in advance


#5

You will need to register admin themes separately to front end themes. If you have a specific theme you want to use to overwrite admin templates, you need to add something like the following to your YML config:

SilverStripe\Admin\LeftAndMain:
  admin_themes:
    - 'mythemename'
    - 'silverstripe/admin:cms-forms'
    - $default

Be aware, I have encountered a bug (LeftAndMain extension's templates only render properly in includes folder · Issue #529 · silverstripe/silverstripe-admin · GitHub) that means I am only able to get some of my admin templates (for LeftAndMain extensions) to render in the Includes folder (not ‘Layout’ or the top level ‘templates’ folder).


#6

Thank you.

I solved it this way right now:

<?php

namespace DanielSchweiger\TestModule;

use SilverStripe\Admin\LeftAndMain;
use SilverStripe\Security\PermissionProvider;
use SilverStripe\View\ArrayData;
use SilverStripe\Security\Permission;

class TestAdmin extends LeftAndMain implements PermissionProvider
{
    private static $url_segment = 'test';

    private static $menu_title = 'Testen';

    private static $menu_icon_class = 'font-icon-chart-line';

    private static $url_handlers = array(
        'show/$TestClass/$Action' => 'handleAction'
    );

    private static $required_permission_codes = 'CMS_ACCESS_TestAdmin';

    public function init()
    {
        parent::init();
        
    }

    public function canView($member = null)
    {
        return Permission::check('CMS_ACCESS_TestAdmin', 'any', $member);
    }

    public function providePermissions()
    {
        return array(
            "CMS_ACCESS_TestAdmin" => array(
                'name' => _t('SilverStripe\\CMS\\Controllers\\CMSMain.ACCESS', "Access to '{title}' section", array(
                    'title' => static::menu_title()
                )),
                'category' => _t('SilverStripe\\Security\\Permission.CMS_ACCESS_CATEGORY', 'CMS Access')
            )
        );
    }

    public function getEditForm($id = null, $fields = null)
    {
        $arrayData = new ArrayData([
            'Title' => static::menu_title(),
            'Content' => 'Content'
        ]);
        
        return $arrayData->renderWith('TestAdmin_Content');
        
    }

}

My template is named TestAdmin_Content.ss …


#7

Hej PsychoMo,

sorry its not an admin theme. Just some tempaltes for a frontend-module…