Problem/Motivation

When using the Conditional Form Fields functionality to alter form fields based on the status of other form fields, it could be useful to allow the addition and removal of CSS classes to target fields.

An example use case is to, using CSS, change the colour/appearance of one field based on the choice selected in another. Another is to make a field (including its wrapper and label) unclickable using CSS pointer-events and opacity values. The latter would not affect the field value being submitted/handled – which in the case of a select field, for example, can cause problems.

Proposed resolution

A patch is attached to add this functionality. It expects a #states value as follows, to toggle the 'class' values provided based on the provided logic:

// Test Field Three is given the classes 'TESTING' and 'TESTINGTWO' when Test Field One has the value 'One' selected.
$form['field_test_field_three']['#states'] = [
  'classes' => [
    'select[name="field_test_field_one"]' => [
      'value' => 'one',
    ],
    'class' => [
      'TESTING',
      'TESTINGTWO',
    ],
  ],
];

Remaining tasks

Test coverage
Change record
Code review

Peer review, and adding suitable documentation to core if this is moved forward.

User interface changes

None directly, but changes will be made if this new functionality is used.

API changes

As described above.

Data model changes

None.

Comments

simonminter created an issue. See original summary.

cilefen’s picture

Version: 10.0.x-dev » 10.1.x-dev
Status: Active » Needs review
Issue tags: +Needs change record

I am moving this to the feature branch and setting it for review.

simonminter’s picture

StatusFileSize
new661 bytes

Oops, messed up my patch. New version created against fresh Drupal download attached here.

simonminter’s picture

simonminter’s picture

StatusFileSize
new663 bytes

Oof, sorry, messed up another patch. Sunday night. Trying again against 10.1.x, new version attached.

simonminter’s picture

simonminter’s picture

StatusFileSize
new678 bytes

New version to meet coding standards.

simonminter’s picture

_utsavsharma’s picture

StatusFileSize
new561 bytes
new678 bytes

FIxed CCF for #7.

smustgrave’s picture

Issue summary: View changes
Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative, +Needs tests

This issue is being reviewed by the kind folks in Slack, #needs-review-queue-initiative. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge request as a guide.

This will need test coverage
Also change record is needed still.

simonminter’s picture

Issue tags: -Needs change record

Added change record at https://www.drupal.org/node/3343007, reviews of this would be appreciated.

If anybody has the availability to create test coverage for this change that too would be appreciated.

smustgrave’s picture

There's a #testing channel in drupal slack where you mind someone interested. Or someone who could offer a direction for the test.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.