NodeLink add classes twice

Created on 8 April 2025, 10 days ago

Problem/Motivation

When using "NodeLink" for a "Read more" link you can add some CSS classes in the field configuration form in "Link class" input.

In the output the CSS classes are added twice.

Not blocking, juste anoying :)

Steps to reproduce

  • On a display mode of a content, use the "Read more" link added by Display Suite.
  • Setup the settings of the link by adding some classes in the "Link Class" input.
  • Save :)
  • Edit a content with this display, use Preview to change the view mode and use the one with "Read more" link
  • Check the classes in the DOM

Analyse

When checking the code in https://git.drupalcode.org/project/ds/-/blob/8.x-3.x/src/Plugin/DsField/... & https://git.drupalcode.org/project/ds/-/blob/8.x-3.x/src/Plugin/DsField/...

The classes are added both :

  • on the entity and the link_attributes, the first one as an array
  • the second one as a string.

It use the Drupal twig function link some line before https://git.drupalcode.org/project/ds/-/blob/8.x-3.x/src/Plugin/DsField/...

On a Drupal 10.4.5 https://github.com/drupal/drupal/blob/10.4.5/core/lib/Drupal/Core/Templa...

We can see that it merge the attributes, but the classes dont have the same type.

It result on the list of classes in the array + the string of same classes added in the output.

Proposed resolution

My easy solution, send an array too! Drupal will only keep one class if added twice or more.

Here https://git.drupalcode.org/project/ds/-/blob/8.x-3.x/src/Plugin/DsField/...

Change
$link_attributes->addClass($config['link class']);
For
$link_attributes->addClass(explode(' ', $config['link class']));

Et voilà :)

🐛 Bug report
Status

Active

Version

3.25

Component

Code

Created by

🇫🇷France stephane aimar

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

Merge Requests

Comments & Activities

Production build 0.71.5 2024