Problem/Motivation

#states do not work on table cells, even though these can be render elements. This is the case with both #theme = table and #type = table.

Example:

    $form['control'] = [
      '#type' => 'checkbox',
      '#title' => 'Ticky this box plx',
    ];

    $rows[] = [
      'name' => ['data' => ['#markup' => 'data 1']],
      'age' => ['data' => ['#markup' => 'age']],
      'email' => ['data' => ['#markup' => 'email']],
    ];
    $rows[] = [
      'name' => ['data' => ['#markup' => 'data 2']],
      'age' => ['data' => ['#markup' => 'age']],
      'email' => ['data' => [
        '#markup' => 'should be only shown if ticky',
        '#states' => [
          'visible' => [
            ':input[name="control"]' => ['checked' => TRUE],
          ],
        ],
      ]],
    ];

    $form['table'] = [
      '#type' => 'table',
      '#rows' => $rows,
    ];

    $form['reacts'] = [
      '#type' => 'textfield',
      '#title' => 'shown if ticky',
      '#states' => [
        'visible' => [
          ':input[name="control"]' => ['checked' => TRUE],
        ],
      ],
    ];

As a workaround, you can add this to a table cell render element:

      '#wrapper_attributes' => [
        // Need this because states JS code looks for nearest parent with this
        // class.
        'class' => ['js-form-wrapper'],
        // Have to add this by hand because this doesn't get states processed.
        'data-drupal-states' => \Drupal\Component\Serialization\Json::encode($states),
      ],

For a header, or a cell made in the older method using a 'data' array key, this works:

      [
        'data' => 'my cell contents',
        'class' => ['js-form-wrapper'],
        'data-drupal-states' => \Drupal\Component\Serialization\Json::encode($states),
      ],

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

Issue fork drupal-3614783

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

joachim created an issue. See original summary.

charlliequadros’s picture

I’ll work on this issue.

charlliequadros’s picture

Assigned: Unassigned » charlliequadros

charlliequadros’s picture

Hi @joachim

Implemented support for #states on table cells containing #markup or #plain_text.

This covers tables rendered using both #type => 'table' and #theme => 'table'. The state data, wrapper class, and core/drupal.states library are now applied to the table cell when required.

Render elements that define their own #type, #theme, or #theme_wrappers continue to use their existing state handling.

charlliequadros’s picture

Assigned: charlliequadros » Unassigned
Status: Active » Needs review
smustgrave’s picture

Status: Needs review » Needs work

Summary is incomplete so moving back.

joachim’s picture

Issue summary: View changes
joachim’s picture

Issue summary: View changes