Problem/Motivation
I tried to identify if a checkbox / radio is part of checkboxes / radios. For proper form theming I require to add a CSS class to single form elements that are of type checkbox / radio, but not if these checkbox / radio is part of a group of checkboxes / radios.
After long debugging I found \Drupal\Core\Render\Element\Checkboxes class, but it is far away from doing something reliable for this themers situation. My findings have shown that there are no good keys in checkbox/radio render arrays that indicate if the element is a checkbox / radio of checkboxes / radios - except abusing some other keys like:
#error_no_message=TRUE#weight=- data type integer (value = 0) - if a single checkbox
- data type float (value = 0.001+) - if multiple checkboxes
Proposed resolution
Add key '#is_group_of_elements' = TRUE to the element to indicate that a checkbox/radio is part of a checkboxes/radios group.
Remaining tasks
Patch
Review
Commit
User interface changes
None
API changes
No breaking change, just an addition.
Data model changes
None.
| Comment | File | Size | Author |
|---|---|---|---|
| #40 | interdiff_2643012_32-40.txt | 7.34 KB | andregp |
| #40 | 2643012-40.patch | 4.36 KB | andregp |
| #32 | interdiff_28-32.txt | 1.95 KB | ravi.shankar |
| #32 | 2643012-32.patch | 4.43 KB | ravi.shankar |
| #28 | interdiff-2643012-23-28.txt | 3.67 KB | mohit_aghera |
Comments
Comment #2
hass commentedComment #3
hass commentedComment #4
hass commentedComment #5
hass commentedComment #6
hass commentedFixed radio documentation.
Comment #7
hass commentedI'm not sure if this is the best idea. I tried getting an idea what #parent is, but failed. I would much more prefer to see the checkboxes element go into #parent what gives us more flexibility.
But as I have not found a single example how #partent is used in render arrays this requires some help how to proceed.
Comment #16
kristen polDoesn't apply to 9.1.x so needs reroll.
Comment #17
hardik_patel_12 commentedRe-rolled against 9.1.x. Kindly review a patch.
Comment #18
kristen polThanks for the update.
1) Not sure this would be considered a bug but leaving for now.
2) This would need tests so tagging.
3) Not sure how this could be manually tested without custom code.
4) Looking at other
'#is_'usage in core and see:Given that, assuming this approach might be accepted, it might be better to shorten the text though maybe "#is_group" is too ambiguous and/or hard to differentiate from
#is_multiple.Comment #19
kristen polMoving back to "Needs work" for tests.
Comment #21
mohit_aghera commentedAdding a few Kernel test cases to check if the field attribute is present or not.
I'm not quite sure about this approach. Feel free to suggest better approaches to implement test cases related to this.
Another one that I am thinking of:
- Create one form
- invoking one template_preprocess_hook
- Set the state variable inside template preprocess hook based on "#is_group_of_elements"
- Again assert the state variable and ensure that it is true.
Comment #22
kristen polThanks for the tests! Noticed something trivial but otherwise it seems clear to me. Tests pass. Let me see if anyone in bugsmash has thoughts on the naming convention.
Extreme Nitpick: Could be 'Test Checkboxes' since other one is 'Test Radios'.
Comment #23
guilhermevp commentedSmall update in patch 20 just addressing comment #22 suggestion.
Comment #24
kristen polThank you for the wording change. That looks good. I'll double check again to see if anyone in bugsmash has thoughts on the
is_group_of_elementsnaming.Comment #25
kristen polAfter discussing with the bugsmash team, moving this to a task. But, leaving the Bug Smash Initiative tag since we worked on this issue as part of the initiative.
Comment #26
quietone commentedI was asked by mohit_aghera for a review. This is likely the first time I have reviewed an issue about a form element.
Starting with the issue summary. That gave me an understanding of the problem so far so good.
Now reading the issue. In #7 the solution used here is questioned and help for a better solution was asked for. The following comments do not address that question. I don't know enough about forms to know if there is a better way to solve this. Can someone confirm?
Like Kristen Pol I think 'is_group_of_elements' a big long and prefer 'is_group'. Personally, I would not confuse that with 'is_multiple' another option could be 'is_group_member'. That makes more sense to me because an element is not a group of elements.
Now looking at the patch.
This is setting the default to be TRUE? How does one make it FALSE?
Explaining the purpose of the property should be in FormElement. Here, tell the user why it is set to TRUE.
The new property should be documented in \Drupal\Core\Render\Element\FormElement. Looking at the file it would be something like
"(bool) TRUE if this element is part of a group of checkboxes or radios. FALSE otherwise. This allows themers to to apply conditional theming." That is not meant to be copy/pasted. But it raises another question. Does it matter that this property is only applicable to Checkboxes and Radios?
Next I looked at the test. The test should also test the case when is_group_of_elements is FALSE as well.
And back to the IS. The proposed resolution is not the same as what is implemented, so it needs updating.
I hope that is useful.
Comment #27
quietone commentedI think I found a duplicate of this #1612596: Support css class per radio item. It is too late for me to read it thoroughly, so noting it here for now.
Comment #28
mohit_aghera commentedFor Suggestion #1
I am not sure if we need to set it FALSE or not.
Reason is, we are setting
is_group_of_elementsto TRUE for checkboxes and radios element only.Let me know your thoughts on this.
Suggestion #2
I've updated the comments, so current patches addresses that issue.
Feel free to review the wordings.
Added property in the FormElement class' comments. I've added another test case for single checkbox as well to ensure that property doesn't exist for single checkboxes.
Comment #29
mohit_aghera commentedUpdating issue summary.
Comment #31
quietone commentedHaving another look. Nice to see the additional test case.
A few lines above this is the descriptions for another boolean, #tree, let's use the same format
(bool) TRUE if .... ; FALSE if the values should be flat..The use of t() can be remove throughout the test. Tests should only use the t() function when specifically testing translations.
I also think the test can be simplified by creating all the fields on one form and then testing that form.
Updated the IS to fix typos and simplify the proposed resolution.
Comment #32
ravi.shankar commentedTried to address point 1 and 2 of comment #31.
Comment #33
quietone commentedAdd related issue.
Comment #34
mohit_aghera commentedComment #35
quietone commented@mohit_aghera, thanks for the answers in #28. Thanks resolves my questions.
@ravi.shankar, thanks for the fixes for #31 1 and 2. I think you missed the paragraph about changing the test?
#26
paragraph 3. The question about the approach being used here needs to be resolved. Or maybe I just do not understand because I don't know much about forms.
paragraph 4. Both Kristen Pol and I think that the property name is too long. See #24 and #26
#31 The test can be simplified.
A few code comments.
See my comment about this comment in Radios.php
s/if element/if the element/
This comment is the Radios file so should not be referring to checkboxes. And now that I see this shorted version I think we can get it on one line. As a suggestion,
"// Indicates this is part of a group." Change that if you like.
The title here suggests that this is testing all Form Element properties. It needs a new name. Maybe, RadioCheckboxGroupPropertyTest ? And the test can be simplified, there is no need to create two forms.
Comment #36
quietone commentedmohit_aghera asked for some clarification of the second paragraph in #35 in #bugsmash. Ah, I see that is poorly formatted as well. There are two parts. The first, in #7 hass said they were not sure that the approach taken in the patch was the 'right' one and talked about looking at #parent. And, as far as I can tell, that approach has been used in the following patches without question. So, is there a better way to do this? I don't know - I just want to make sure that someone has thought about it. And the second, is questing the name of the new property.
And that ping in #bugsmash was timely as well. I was wondering the other day about #26.2 and didn't manage to get back here to check. In that comment I said I thought the documentation for the new property should be in FormElement and now I think that is wrong. Other elements, such as Url and Weight define properties that are unique to those elements. I now think the documentation for the new element should be in Radios and Checkboxes. If you agree, sorry about the extra work.
Hope that helps.
Comment #38
jwilson3Hit this issue this week #3276250: Gin theme checkbox styles breaks Webform custom composite fields required. Gin theme has some fancy CSS to avoid theming checkboxes incorrectly that would be greatly simplified by this fix getting into core. Thanks for the work that was done on this last year. I guess its not super clear what is being asked to change to take this forward.
Comment #39
andregp commentedI'm working on #35 and #36 I'll send a patch in a few moments.
Comment #40
andregp commentedRegarding #35
i - Done. Shortened property name to
#is_group_member1 & 3 - Done! Shortened comments to a single line.
2 - Done.
4 - Done. Renamed test to CheckboxRadioGroupPropertyTest and simplified test to use a single form. I also added one more test case for the single radio case.
Regarding #36
I moved the new property description to Checkbox.php and Radios.php
Comment #42
andregp commentedThe fail seems unrelated, requeueing test.
Comment #43
joshua1234511Tested the patch from #40
All suggestions are rectified in the latest patch
Steps followed.
Added a custom form
Added custom class in preprocess hook using the variable is_group_member
Patch works correctly.
Comment #46
andregp commentedUnrelated test fail, re-queueing tests and restoring status
Comment #48
andregp commentedUnrelated test fail. Restoring status.
Comment #50
andregp commentedUnrelated test fail. Restoring status.
Comment #51
joshua1234511Tested the patch from #40
Against latest 9.5.x-dev
Patch cleanly apply and works correctly.
Comment #53
kristen polUnrelated test fail. Restoring status.