top-level #attributes in hook_node_links_alter() has no effect

Created on 26 March 2024, 6 months ago

Problem/Motivation

The docs for the hook, and several implementations in code, put #attributes at the top of their links:

  $links['my_module'] = [
    '#theme' => 'links__node__my_module',
    '#attributes' => ['class' => ['links', 'inline']],  <-- here
    '#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")]]),
      ],
    ],
  ];

AFAICT this has no effect.

This is because Drupal\Core\Render\Element\Link::preRenderLinks() does this:

        $element['#links'] += $child['#links'];

and the rest of the child array is ignored.

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

🐛 Bug report
Status

Active

Version

11.0 🔥

Component
Node system 

Last updated 1 minute ago

No maintainer
Created by

🇬🇧United Kingdom joachim

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024