You are here

function hook_node_links_alter in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/node/node.api.php \hook_node_links_alter()
  2. 9 core/modules/node/node.api.php \hook_node_links_alter()

Alter the links of a node.

Parameters

array &$links: A renderable array representing the node links.

\Drupal\node\NodeInterface $entity: The node being rendered.

array &$context: Various aspects of the context in which the node links are going to be displayed, with the following keys:

  • 'view_mode': the view mode in which the node is being viewed
  • 'langcode': the language in which the node is being viewed

See also

\Drupal\node\NodeViewBuilder::renderLinks()

\Drupal\node\NodeViewBuilder::buildLinks()

Entity CRUD, editing, and view hooks

Related topics

3 functions implement hook_node_links_alter()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

book_node_links_alter in core/modules/book/book.module
Implements hook_node_links_alter().
comment_node_links_alter in core/modules/comment/comment.module
Implements hook_node_links_alter().
statistics_node_links_alter in core/modules/statistics/statistics.module
Implements hook_node_links_alter().

File

core/modules/node/node.api.php, line 420
Hooks specific to the Node module.

Code

function hook_node_links_alter(array &$links, NodeInterface $entity, array &$context) {
  $links['mymodule'] = [
    '#theme' => 'links__node__mymodule',
    '#attributes' => [
      'class' => [
        'links',
        'inline',
      ],
    ],
    '#links' => [
      'node-report' => [
        'title' => t('Report'),
        'url' => Url::fromRoute('node_test.report', [
          'node' => $entity
            ->id(),
        ], [
          'query' => [
            'token' => \Drupal::getContainer()
              ->get('csrf_token')
              ->get("node/{$entity->id()}/report"),
          ],
        ]),
      ],
    ],
  ];
}