Link options attributes removed on save

Created on 23 May 2019, over 6 years ago
Updated 23 September 2025, 15 days ago

Problem/Motivation

If you are attaching some option attributes to a link field (using a custom module for example), those attributes are removed on node save.
One solution is to use link_attributes, but you might not want to allow users to edit those attributes from node edit.

LinkWidget::formElement() puts the options attributes into $element['attributes'], which is a value type element. Upon submitting, LinkWidget::massageFormValues() does not use this value. It is effectively discarded.

Steps to reproduce

Since there's no form element for the attributes in the LinkWidget the easiest way to reproduce the issue is by using Drush.

  1. Add a Link field to any node type.
  2. Create a new instance of that node type. Fill out the link field with any valid input.
  3. Use the following command to set the attributes, replacing "FIELD_MACHINE_NAME" with the machine name of the field you created and "NID" with the node ID of the one you created in the previous step: drush eval '$node = \Drupal\node\Entity\Node::load(NID);$node->FIELD_MACHINE_NAME->options = ["attributes" => ["class" => "test-class"]];$node->save();'
  4. Verify the options you just created with the command drush eval '$node = \Drupal\node\Entity\Node::load(NID);var_dump($node->FIELD_MACHINE_NAME->options);'
  5. Visit the edit form for the node you created. Re-save it.
  6. Verify the options with the same Drush command.

Expected result:
The options should be the same as when you loaded them in step 4, which should look like this:

array(1) {
  ["attributes"]=>
  array(2) {
    ["class"]=>
    string(17) "test-class-edited"
  }
}

Actual result:
The options array is empty:

array(0) {
}

Proposed resolution

Update LinkWidget::massageFormValues() to merge in the submitted attributes. This is tricky to do in a backward-compatible way, maybe impossible. A previous attempt to solve the issue by changing the element structure in the form broke a few modules that deal with link attributes. It may be impossible to fix this issue in a way that is backward compatible for all contrib modules.

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

🐛 Bug report
Status

Needs work

Version

11.0 🔥

Component

link.module

Created by

🇷🇴Romania aalin

Live updates comments and jobs are added and updated live.
  • Needs change record

    A change record needs to be drafted before an issue is committed. Note: Change records used to be called change notifications.

  • Needs release note

    The major change should have a special release note written to summarize the importance of the change. See Write a release note for an issue.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

No activities found.

Production build 0.71.5 2024