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.
| Comment | File | Size | Author |
|---|---|---|---|
| #9 | 3335537-9.patch | 678 bytes | _utsavsharma |
| #9 | interdiff_7-9.txt | 561 bytes | _utsavsharma |
| #7 | 3335537-101x-2.patch | 678 bytes | simonminter |
Comments
Comment #2
cilefen commentedI am moving this to the feature branch and setting it for review.
Comment #3
simonminter commentedOops, messed up my patch. New version created against fresh Drupal download attached here.
Comment #4
simonminter commentedComment #5
simonminter commentedOof, sorry, messed up another patch. Sunday night. Trying again against 10.1.x, new version attached.
Comment #6
simonminter commentedComment #7
simonminter commentedNew version to meet coding standards.
Comment #8
simonminter commentedComment #9
_utsavsharma commentedFIxed CCF for #7.
Comment #10
smustgrave commentedThis 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.
Comment #11
simonminter commentedAdded 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.
Comment #12
smustgrave commentedThere's a #testing channel in drupal slack where you mind someone interested. Or someone who could offer a direction for the test.