Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 429 Vote(s) - 3.55 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Drupal 8 - How to add task and contextual links for specific node type?

#1
I created a task link and a contextual one for base_route: entity.node.canonical

mymodule.routing.yml

mymodule.mycustomroute:
path: '/node/{node}/custom-path'
defaults:
_form: '\Drupal\mymodule\Form\MyForm'
requirements:
_permission: 'my permission'
node: '[0-9]+'

mymodule.links.tasks.yml

mymodule.mycustomroute:
route_name: mymodule.mycustomroute
base_route: entity.node.canonical
title: 'my title'

mymodule.links.contextual.yml

mymodule.mycustomroute:
route_name: mymodule.mycustomroute
group: node

My link shows up next to View / Edit / Delete links on each node as I wanted.

Now I am wondering how is it possible to make these links available only for specific node type(s)?
Reply

#2
Or you can specify route parameters in the mymodule.links.menu.yml file:

mymodule.add_whatever:
title: 'Add whatever'
description: 'Add whatever'
route_name: node.add
route_parameters: { node_type: 'name_of_node_type' }
menu_name: main
weight: 7
Reply

#3
**mymodule/mymodule.routing.yml** :

mymodule.mycustomroute:
path: '/node/{node}/custom-path'
defaults:
_form: '\Drupal\mymodule\Form\MyForm'
requirements:
_permission: 'my permission'
_custom_access: '\Drupal\mymodule\Access\NodeTypeAccessCheck::access'
_node_types: 'node_type_1,node_type_2,node_type_n'
node: '\d+'

**mymodule/src/Access/NodeTypeAccessCheck.php** :

namespace Drupal\mymodule\Access;

use Drupal\Core\Access\AccessCheckInterface;
use Drupal\Core\Access\AccessResult;
use Drupal\node\NodeInterface;
use Symfony\Component\Routing\Route;

/**
* Check the access to a node task based on the node type.
*/
class NodeTypeAccessCheck implements AccessCheckInterface {

/**
* {@inheritdoc}
*/
public function applies(Route $route) {
return NULL;
}

/**
* A custom access check.
*
* @param \Drupal\node\NodeInterface $node
* Run access checks for this node.
*/
public function access(Route $route, NodeInterface $node) {
if ($route->hasRequirement('_node_types')) {
$allowed_node_types = explode(',', $route->getRequirement('_node_types'));
if (in_array($node->getType(), $allowed_node_types)) {
return AccessResult::allowed();
}
}
return AccessResult::forbidden();
}

}
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through