4

How do I create a tab in the admin page using a custom module?

The files defining the routes, links and task are the following.

cst_core.routing.yml

cst.admin:
  path: '/admin/MyCustomModule'
  defaults:
    _controller: '\Drupal\cst_core\Controller\CstController::content'
    link_id: 'cst.admin'
    _title: 'MyCustomModule Management'
  requirements:
    _permission: 'cst admin'
    
cst.admin_menu:
  path: '/admin/MyCustomModule/menu-mgmt'
  defaults:
    _controller: '\Drupal\cst_core\Controller\CstController::content'
    link_id: 'cst.admin_menu'
    _title: 'MyCustomModule Menu Management'
  requirements:
    _permission: 'cst menu'

cst.admin_menu_menu:
  path: '/admin/MyCustomModule/menu-mgmt/menu'
  defaults:
    _controller: '\Drupal\cst_core\Controller\CstController::content'
    link_id: 'cst.admin_menu_menu'
    _title: 'MyCustomModule Menu Management'
  requirements:
    _permission: 'cst menu'

cst.admin_menu_menu_taba:
  path: '/admin/MyCustomModule/menu-mgmt/menu/taba'
  defaults:
    _controller: '\Drupal\arm_core\Controller\CstController::content'
    link_id: 'cst.admin_menu_menu_taba'
    _title: 'MyCustomModule Menu Management tabs'
  requirements:
    _permission: 'cst menu'

cst_core.links.menu.yml

cst.admin:
title: 'MyCustomModule'
description: 'MyCustomModule System'
parent: system.admin
route_name: cst.admin
weight: -20

cst.admin_menu:
title: 'Menu'
description: 'Menu Management.'
parent: cst.admin
route_name: cst.admin_menu
weight: 0

cst.admin_menu_menu:
title: 'Menu'
description: 'Menus.'
parent: cst.admin_menu
route_name: cst.admin_menu_menu
weight: 0

cst_core.links.task.yml

cst.admin_menu_menu_taba:
route_name: cst.admin_menu_menu_taba
title: 'tab-menu A'
base_route: cst.admin_menu_menu

With this, I should have:

-- MyCustomModule

--- Menu Management

---- Menu

+++++ tab-menu A (as a tab) --> but this didn't happend T_T

something like this:

something like this:

2

if I suppose cst.admin_menu_menu_tabA: is your sub-menu A then also add

cst_core.links.task.yml for tabs or sub menu tabs in an admin page

then add the following code below

cst.admin_menu_menu_tabA:
  route_name: cst.admin_menu_menu_tabA
  title: 'sub-menu A'
  base_route: cst.admin_menu_menu

proceed so on for the rest of the tabs..

and also add a route for cst.admin_menu_menu_tabA in cst_core.routing.yml

8
  • Hi thanks for the response, but it didn't happened, maybe do you mind to help elaborate whats the different route_name and base_route? my structure actually like this: admin page- module main page - menu page- menu_menu (sub menu) - (and i like to build tab inside menu_menu page) – AnD May 13 '16 at 9:45
  • @AnD route_name is used to affix the current tab whereas base_route is used to describe under which parent tab it should appear . – make-me-alive May 13 '16 at 9:49
  • so in my case, base_route should be: cst.admin_menu_menu and route_name should be: cst.admin_menu_menu_tabA (new route), correct? let me try this – AnD May 13 '16 at 9:54
  • hold on... but how about task name, you name it: cst.admin_menu_menu - is it should linked to something? – AnD May 13 '16 at 9:56
  • 2
    Good example can be seen at core/modules/locale/locale.links.task.yml – leymannx Dec 14 '16 at 12:53
1

The tab is actually appear in this path: /admin/MyCustomModule/menu-mgmt/menu/taba

but it didn't appear in: /admin/MyCustomModule/menu-mgmt/menu

the reason is:

you need to add menu_menu in task as well, as follow:

cst.admin_menu_menu:
  title: 'Menu'
  route_name: cst.admin_menu_menu
  base_route: cst.admin_menu_menu

so your cst_core.links.task.yml should look like:

cst.admin_menu_menu:
  title: 'Menu'
  route_name: cst.admin_menu_menu
  base_route: cst.admin_menu_menu

cst.admin_menu_menu_taba:
  title: 'sub-menu A'
  route_name: cst.admin_menu_menu_taba
  base_route: cst.admin_menu_menu

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.