3

I have a module_name.links.tasks.yml file for Drupal 8, whose content is the following one.

entity.node.module_admin:
  route_name: module_name.admin
  base_route: entity.node.canonical
  title: Admin Page
  weight: 99

entity.node.check_backend:
  route_name: module_name.check-backend
  base_route: entity.node.canonical
  title: Check Backend
  weight: 100

entity.node.download_webagent:
  route_name: module_name.download_webagent
  base_route: entity.node.canonical
  title: Download Webagent.conf
  weight: 101

entity.node.download_httpdconf:
  route_name: module_name.download_httpdconf
  base_route: entity.node.canonical
  title: Download httpd.conf
  weight: 102

These tabs appear in all the content type nodes. How can I restrict them to specific content types?

4
  • maybe you need to use the content type path in your routing.yml
    – No Sssweat
    Commented Jul 20, 2016 at 10:54
  • but in routing.yml we only specify the path and controller action for the corresponding route when click on the tab item , we cant filter from there .
    – KTM
    Commented Jul 20, 2016 at 11:05
  • ah, right! hmmmm
    – No Sssweat
    Commented Jul 20, 2016 at 13:04
  • You got any idea ;) ?
    – KTM
    Commented Jul 20, 2016 at 13:52

2 Answers 2

6

Yes, routing is correct. Routes specify access rules, if you want your route to only be accessible for a certain node type, implement custom access callback and check for that.

The local task will then automatically respect that.

Example from core:

comment.reply:
  path: '/comment/reply/{entity_type}/{entity}/{field_name}/{pid}'
  defaults:
    _controller: '\Drupal\comment\Controller\CommentController::getReplyForm'
    _title: 'Add new comment'
    pid: ~
  requirements:
    _custom_access: '\Drupal\comment\Controller\CommentController::replyFormAccess'

That method then can receive the same arguments as the controller/form, in your case the node (just add NodeInterface $node) and you can check the type there and everything else you want (like permissions)

5
  • Can you explain the answer with a code sample ? I dint get the area where i can implement custom access callback .
    – KTM
    Commented Jul 20, 2016 at 21:17
  • Core has lots of examples, just search for _custom_access in routing files, added one example
    – Berdir
    Commented Jul 21, 2016 at 8:06
  • so in the controller function '\Drupal\comment\Controller\CommentController::replyFormAccess' can i do like checking $node->type== ?? by receiving $node in the controller function?
    – KTM
    Commented Jul 21, 2016 at 9:29
  • Or how i restrict the tab access even i recieve the node type there ?
    – KTM
    Commented Jul 21, 2016 at 9:38
  • 1
    See drupal.org/docs/8/api/routing-system/… for a good example.
    – colan
    Commented Nov 7, 2017 at 17:42
2

I found the answer !

I have to give like following in module_name.routing.yml which will inherit the entity node tabs which are defined in local.links.task.yml

module_name.admin:
  path: '/node/{node}/module_name'
  defaults:
    _controller: '\Drupal\module_name\Controller\MyController::my_function'
    _title: 'Admin Page'
  requirements:
    _permission: 'access admin tab'
    _custom_access: '\Drupal\module_name\Controller\MyController::admin_tab_access'

and in the controller function i am filtering the node types which are only i wanted to display in tab :

public function admin_tab_access($node) {
        $node_data = node_load($node);
        if(isset($node_data->field_p_type[0]->value)) {
            $node_type = $node_data->field_p_type[0]->value;
            if ($node_type == 'type_1' || $node_type == 'type_2'){
                return AccessResult::allowed();
            } else {
                return AccessResult::forbidden();
            }
        }
      return AccessResult::forbidden();
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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