Problem/Motivation

The class attribute of a Drupal link expects an array.

The button class added in the code below casts that attribute to a string:

    $form['url_redirects']['actions']['#links']['add'] = [
      'title' => t('Add URL redirect'),
      'url' => Url::fromRoute('redirect.add', [
        'redirect' => $node->toUrl()->getInternalPath(),
        'destination' => \Drupal::destination()->get(),
      ]),
      'attributes' => [
        'class' => 'button',
        'target' => '_blank',
      ],
    ];

An operations that attempt to manipulate the attributes values of links (e.g., through hook_link_alter(); see use case: #2665320: Menu links to unpublished nodes are visible to privileged users) will trigger a fatal error, "Error: [] operator not supported for strings."

Proposed resolution

diff --git a/redirect.module b/redirect.module
index 5d5bae2..182dc31 100644
--- a/redirect.module
+++ b/redirect.module
@@ -372,7 +372,7 @@ function redirect_form_node_form_alter(&$form, FormStateInterface $form_state, $
         'destination' => \Drupal::destination()->get(),
       ]),
       'attributes' => [
-        'class' => 'button',
+        'class' => ['button'],
         'target' => '_blank',
       ],
     ];

Priority

The likelihood of sites performing link alters that would include the link provided by the Redirect module seem low, but on the other hand, it will provoke a fatal error. So: low frequency, high impact.

CommentFileSizeAuthor
#2 3230079-incorrect-link-class_2.patch449 bytesmark_fullmer

Comments

mark_fullmer created an issue. See original summary.

mark_fullmer’s picture

Status: Active » Needs review
StatusFileSize
new449 bytes

The attached patch provides the "button" class as an array element, respecting the Drupal core expectation and preventing a fatal error when the link attributes array is merged by other processes.

dave reid’s picture

Status: Needs review » Reviewed & tested by the community

I just ran into this as well, looks good.

  • mark_fullmer authored c90cb93 on 8.x-1.x
    Issue #3230079 by mark_fullmer: Fixed "Add URL Redirect" button on...
dave reid’s picture

Status: Reviewed & tested by the community » Fixed

Committed #2 to 8.x-1.x. Thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.