#states not applied to link elements

Created on 19 October 2016, about 8 years ago
Updated 11 October 2023, about 1 year ago

Problem/Motivation

$form['checkbox'] = [
  '#type' => 'checkbox',
  '#title' => t('Show link'),
];
$form['link'] = [
  '#type' => 'link',
  '#title' => t('Link to front page'),
  '#url' => Url::fromRoute('<front>'),
  '#states' => [
    'visible' => [
      ':input[data-drupal-selector=edit-checkbox]' => ['checked' => TRUE],
    ],
  ],
];

Expected output:
<a href="/" data-drupal-selector="edit-link" data-drupal-states="{&quot;visible&quot;:{&quot;:input[data-drupal-selector=edit-checkbox]&quot;:{&quot;checked&quot;:true}}}" id="edit-link">Link to front page</a>

Actual output:
<a href="/" data-drupal-selector="edit-link" id="edit-link">Link to front page</a>

This appears to be because \Drupal\Core\Render\Element\Link::preRenderLink moves the attributes into #options['attributes'], which would be fine... if drupal_process_states() (which adds the states to the element's #attributes) was invoked before pre_render callbacks were invoked in \Drupal\Core\Render\Renderer::doRender.

Proposed resolution


Update Render/Element/Link.php element adding support to js states.

Remaining tasks

none

User interface changes

None

API changes

None

Data model changes

None

🐛 Bug report
Status

Fixed

Version

11.0 🔥

Component
Render 

Last updated 3 days ago

Created by

🇺🇸United States markhalliwell

Live updates comments and jobs are added and updated live.
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.

Production build 0.71.5 2024