Problem/Motivation

When using the form element checkbox, it is possible to disable the element but having its value remain checked. This is used, for example, on the module overview page, where it is possible to check more modules, but impossible to uncheck already enabled modules:
screenshot_1.png

The same functionality is needed when using the form element "checkboxes". Currently, when an option of the checkboxes is disabled, it is saved as unchecked, even when it was set as default value as checked

screenshot_3.png

Steps to reproduce

  • Configure a block to be only visible on articles
  • Add this hook to block.module
    function block_form_block_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
      $form['visibility']['node_type']['bundles']['article']['#disabled'] = TRUE;
    }
       
  • Go back to the block configuration form -- see that article is disabled but selected
  • Save the block configuration again, without any changes
  • Go back to the block configuration form -- see that article is disabled but NOT selected

Proposed resolution

Ensure that disabled checkboxes values are not saved or their values persist.

Remaining tasks

None.

User interface changes

None

API changes

None

Data model changes

None

CommentFileSizeAuthor
#79 interdiff_76-79.txt1.31 KBravi.shankar
#79 2911473-79.patch4.8 KBravi.shankar
#76 2911473-76.patch4.85 KBranjith_kumar_k_u
#71 2911473-71.patch4.87 KBdhirendra.mishra
#71 interdiff_69-71.txt879 bytesdhirendra.mishra
#69 drupal-selected-disabled-checkboxes-2911473-69.patch4.86 KBadinac
#66 2911473-66.patch4.8 KBkuldeep_mehra27
#64 drupal-selected-disabled-checkboxes-2911473-64.patch4.8 KBadinac
#62 application_features_disabled_checkbox.png39.69 KBcarolpettirossi
#54 check-readonly.php_.zip900 bytesskaught
#50 check.php_.zip822 bytesskaught
#49 edition_article.PNG1.08 KBfabienly
#49 creation_article.PNG1.19 KBfabienly
#39 2911473-39.drupal.Selected-yet-disabled-individual-options-from-checkboxes-element-dont-persist-through-save.patch4.71 KBjoachim
#37 interdiff.2911473.26-37.txt824 bytesjoachim
#37 2911473-37.drupal.Selected-yet-disabled-individual-options-from-checkboxes-element-dont-persist-through-save.patch4.71 KBjoachim
#33 2911473.patch725 bytesmahtab_alam
#32 2911473_32_values_for_disabled_checkboxes_lost.patch730 bytesMaskOta
#26 19-26-interdiff.txt1.17 KBMaouna
#26 2911473-disabled_checkboxes-26.patch4.72 KBMaouna
#19 9-19-interdiff.txt1.49 KBMaouna
#19 2911473-disabled_checkboxes-19.patch4.69 KBMaouna
#8 7-9-interdiff.txt1.09 KBMaouna
#8 2911473-disabled_checkboxes-9.patch4.15 KBMaouna
#7 2-7-FAILING-interdiff.txt3.12 KBMaouna
#7 2911473-disabled_checkboxes-FAILING-7.patch2.94 KBMaouna
#3 screenshot_3.png138.28 KBMaouna
#3 screenshot_2.png140.63 KBMaouna
#3 screenshot_1.png144.29 KBMaouna
#2 2911473-disabled_checkboxes-FAILING-2.patch1.81 KBMaouna

Comments

Maouna created an issue. See original summary.

Maouna’s picture

Status: Active » Needs review
StatusFileSize
new1.81 KB
Maouna’s picture

Issue summary: View changes
StatusFileSize
new144.29 KB
new140.63 KB
new138.28 KB
Maouna’s picture

Issue summary: View changes
Maouna’s picture

Issue summary: View changes

Status: Needs review » Needs work

The last submitted patch, 2: 2911473-disabled_checkboxes-FAILING-2.patch, failed testing. View results

Maouna’s picture

Status: Needs work » Needs review
StatusFileSize
new2.94 KB
new3.12 KB

Made the failing test more specific.

Maouna’s picture

StatusFileSize
new4.15 KB
new1.09 KB

Fixed the bug by extending the Checkboxes form element to use the default value also for disabled options.

Status: Needs review » Needs work

The last submitted patch, 8: 2911473-disabled_checkboxes-9.patch, failed testing. View results

michaellenahan’s picture

joelpittet’s picture

Version: 8.5.x-dev » 8.4.x-dev
Priority: Normal » Critical

This is major at least and a bug and should hopefully be fixed for 8.4. IMO. Data could be potentially lost (which may make it critical?) if the disabled form fields have been submitted with checked boxes values will be lost due to this bug.

Thank you very much @Maouna for finding this issue.

joelpittet’s picture

Title: Selected but disabled option of checkboxes is not kept » Selected but disabled options from checkboxes element don't persist through save
Issue summary: View changes
joelpittet’s picture

This has tests but the solution may not be right yet. Thanks for writing the tests to show this is reproducible:) I'm adding those steps to the issue summary.

Maybe someone at the sprint can confirm the steps added are correct?

al0a’s picture

Priority: Critical » Normal

So I tested this, I can't reproduce this issue.

Steps i went through:

  • Added checkbox fields to a node
  • Downloaded "disable field" module
  • created a node
  • disabled the field via this module
  • edit and save node

also, I tried disabling it via the form-display, but same results.

In your test script you disable a individual checkbox inside a list of checkboxes, Is there a way we can easily reproduce this case via the UI?

Maouna’s picture

Version: 8.4.x-dev » 8.5.x-dev
Status: Needs work » Needs review

Steps to reproduce:
1) Add a boolean field to article
2) Configure the form display to use the widget "Checkboxes/radio buttons".
3) Add an article, selecting the first option of the new field
4) Add this hook to any module:

function my_module_form_node_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  $form['field_my_field']['widget'][0]['#disabled'] = TRUE;
}

5) Go to the node form and see that the first option is selected but disabled
6) Submit the node form without any changes and see that the first option is not selected anymore

Thanks for the feedback! I am working on a solution right now.

@al0a: there is no need for a module for the disabling. Also, this bug is about disabling a single option (aka only one checkbox), not the whole field (aka all checkboxes).

Maouna’s picture

Status: Needs review » Needs work
joelpittet’s picture

Title: Selected but disabled options from checkboxes element don't persist through save » Selected yet disabled individual options from checkboxes element don't persist through save
Version: 8.5.x-dev » 8.4.x-dev
Issue summary: View changes
Priority: Normal » Critical

Thanks for the steps I replaced my earlier steps with your updated steps to reproduce in the summary.

joelpittet’s picture

Version: 8.4.x-dev » 8.5.x-dev
Priority: Critical » Normal
Maouna’s picture

Status: Needs work » Needs review
StatusFileSize
new4.69 KB
new1.49 KB
Maouna’s picture

Assigned: Maouna » Unassigned
jenlampton’s picture

Issue summary: View changes
jenlampton’s picture

Issue summary: View changes
jenlampton’s picture

Issue summary: View changes

I was unable to reproduce the bug with the steps provided, but with a little more guidance i was able to find the problem. I've updated the steps to reproduce so hopefully it will be easier for the next person to find the problem as well.

Maouna’s picture

Assigned: Unassigned » Maouna
Status: Needs review » Needs work
sutharsan’s picture

I mentored Maouna on debugging / reproducing the problem and improving the code comments.
@Maouna you are doing great. I'm glad you are contributing to Drupal core :)

Maouna’s picture

StatusFileSize
new4.72 KB
new1.17 KB

@Sutharsan and I improved the the comments.

Maouna’s picture

Issue summary: View changes
joelpittet’s picture

Status: Needs work » Needs review

Enabling the testbot with "needs review" status

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

jody lynn’s picture

MaskOta’s picture

Priority: Normal » Critical

This is still an issue and IMO a critical one because of potential data loss.

In our case we had a classification field that should persist on all translations and because different users could add translations we wanted to make sure only the original author could do the classification change on the original language. The result was the new translations caused all classifications being lost accross all translations.

MaskOta’s picture

Assigned: Maouna » Unassigned
StatusFileSize
new730 bytes

@Maouna patch didn't work for me unfortunately. Here is my take on it which made my field save correctly. It is not inline with the proposed resolution in the IS tho.

Please review

mahtab_alam’s picture

StatusFileSize
new725 bytes
joachim’s picture

Assigned: Unassigned » joachim
+++ b/core/lib/Drupal/Core/Render/Element/Checkboxes.php
@@ -94,7 +94,7 @@ public static function processCheckboxes(&$element, FormStateInterface $form_sta
+    if ($input === FALSE || ($input === null && isset($element['#attributes']['disabled']))){

- null should be NULL
- the way to disable an element is just $element['disabled'], not with #attributes
- should be !empty(), not isset(). isset() only checks for NULL. isset(FALSE) returns TRUE!

Working on a new patch...

joachim’s picture

Actually, patches #33 and #34 don't work for me at all.

Debugging shows that valueCallback() is called for the checkboxes element overall, not for the individual checkbox sub-elements.

joachim’s picture

Status: Needs review » Needs work

Patch #26 looks like it has the right approach, but that's not working for me either...

joachim’s picture

Status: Needs work » Needs review
StatusFileSize
new4.71 KB
new824 bytes

Ok, so patch #26 does work in general, but my problem was that User module slightly misbehaves in the AccountForm:

    $form['account']['roles'] = [
      '#type' => 'checkboxes',
      '#title' => $this->t('Roles'),
      '#default_value' => (!$register ? $account->getRoles() : []),
      '#options' => $roles,
      '#access' => $roles && $user->hasPermission('administer permissions'),
    ];

The correct way, AFAIK, to set a default value for checkboxes is an array whose keys and values match, e.g.:

[
'foo' => 'foo',
'bar' => 'bar',

However... the Checkboxes form element class doesn't document this, so we can't treat my recollection as being correct. Therefore, the isset() check for the default value should be an in_array() instead.

Here's an updated version of patch #26. Credit should still be to @Maouna, my change is a 1-line tweak.

joachim’s picture

Argh, needed a reroll for 8.6.x for changes in the test class.

Same interdiff as before.

(Note to users of Commerce License linked here from the README: use the patch in #37 above for Drupal core 8.5 and earlier.)

MaskOta’s picture

@Joachim #32 probably didn't work for you because i was basing my disabled value on the attribute, which was incorect as you have stated.
But if instead you do

if ($input === FALSE || ($input === NULL && !empty($element['#disabled']))) {

It should work.

And this is probably the reason why @Maouna's patch did not work for me. She was basing it on the correct way of disabling the element.
I think this opens up a new problem here. We have more than one way of disabling form elements that work but some might cause this data loss.

berdir’s picture

> The correct way, AFAIK, to set a default value for checkboxes is an array whose keys and values match, e.g.:

Just an array of values is definitely valid and pretty common.

joachim’s picture

> if ($input === FALSE || ($input === NULL && !empty($element['#disabled']))) {

I tried that too. But $element is the whole of the checkboxes element, not each single checkbox. So that doesn't work.

> I think this opens up a new problem here. We have more than one way of disabling form elements that work but some might cause this data loss.

Using [#attributes][disabled] is incorrect, AFAIK. Or at least, it's only cosmetic.

> Just an array of values is definitely valid and pretty common.

Thanks! That confirms my change from #26 to #39 as correct.

joachim’s picture

Assigned: joachim » Unassigned
skaught’s picture

This is used, for example, on the module overview page, where it is possible to check more modules, but impossible to uncheck already enabled modules:

Modules are not suppose to be disabled (and thereby, uninstalled) by core that way. the fact that the checkbox is disabled is simply to show that the module.. is active. the 'problem' on /admin/modules is it lack help test to clarify that a sitebuilder wouldn't be disabling a module on that tab but should be on the 'uninstall' tab (tab is poorly named. people don't get they are actually doing both then.)

-------
re:#31 -- sounding like your field translation and field access process in that project may need to be addressed differently.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

kclarkson’s picture

This seems major to me. Can a core team member review?

subhojit777’s picture

Issue tags: +#DCD19
fabienly’s picture

StatusFileSize
new1.19 KB
new1.08 KB

Hi,
I tried on drupal-8.8.x-dev. The patch applied without any problem but give me no modification.
I follow the step for the case 2 but as my screenshot display the problem still occur:

  • Add a field to article
  • Configure the Manage form display
  • Add this line of code
  • Enable the book module
  • Add an article
  • screenshot of my field in the new content display
    screenshot of my field in the new content display
  • Submit the node form
  • Edit the new article node
  • screenshot of my field in the edition content display
    screenshot of my field in the edition content display
skaught’s picture

StatusFileSize
new822 bytes
<!DOCTYPE html>
<html>
<head>
	<title></title>
</head>
<body>
<?php
var_dump($_GET);
?><form action="/check.php" method="GET">
  <input type="checkbox" name="vehicle" value="Bike"> I have a bike<br>
  <input type="checkbox" name="vehicle" value="Car"> I have a car<br>
  <input type="checkbox" name="vehicle" value="Boat" checked='checked' disabled='disabled'> I have a boat<br>
  <input type="submit" value="Submit">
</form>
</body>
</html>

a basic test. disabled checkboxes NO NOT HOLD A VALUE when submitted as the basic behaviour of html forms.

---
Using disabled as a UX approach to show previous selected items as 'selected, but now now un-selectible' in a group of checkboxes is not a workable idea due to constraints of Forms themselves.

possibly: you can use JS(ie: if you have disabled one of the checkboxes via a #state operation) and another hidden input to 'keep watch' of those items and pass that back through and process that within a _validate or #after_build. This is essentially a custom component/widget.

IMO: core shouldn't handle this kind of use case. it's a customization of the natural element.

Again, the module page uses individual checkbox items, not grouped to show 'selected/but frozen' in it's own process -- it is a highly customized core form in that way.

ghost of drupal past’s picture

My opinion was sought on this issue (no wonder). Somewhat reluctantly (I am not the form API maintainer now! not any more) here it comes.

First of all, I do not quite understand what's critical here.

Second, I am surprised. I was not aware Drupal supports disabled individual checkbox elements under checkboxes. When was this added, where is it documented and where is it tested? Of course, any form API work I remember is now many years old but searching for the string "disabled" in the Checkboxes class finds nothing, as far as I can see Drupal\Tests\system\Functional\Form\FormTest doesn't test such, nor does ElementTest. In general, I feel #type checkboxes is a convenience, if your use case goes beyond the capabiities of it then just use individual #checkbox elements.

miroslavbanov’s picture

Re #51. Bringing up documentation is a good point. On checkboxes being poorly documented - check this issue and its linked issue: https://www.drupal.org/project/drupal/issues/2779999

Re #50 As for drupal "#disabled", it's a bit of a mystery for me, but I don't think it's relying only on standard form behavior.

If you want more standard and clear thing use "readonly" attribute. Related:
https://stackoverflow.com/questions/7730695/whats-the-difference-between...

ghost of drupal past’s picture

Thanks for the pointers, now I have answers: this was added in #915936: Make it easier to define checkboxes/radios with customized sub-elements and it is tested in FormTestCheckboxesRadiosForm: while #disabled is explicitly not tested, that form tests the sub element customization. This is a big surprise for me: I never knew about this and this is old (it was in D7 already). Oh well. I still think this is neither critical nor a novice issue but what do I know, really.

skaught’s picture

StatusFileSize
new900 bytes

#52 cheers.

Readonly would be the correct attribute for the situation. Regardless if using a group or individual checboxed/radios.

klonos’s picture

This problem exists in D7 too, so unless there is another issue for D7 specifically, tagging this for backporting.

klonos’s picture

Issue tags: +Needs backport to D7
volkswagenchick’s picture

Issue tags: +badcamp2019

Tagging for badcamp2019, thanks! (October 2-5)

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

mradcliffe’s picture

I am removing the Novice tag from this issue because it's not clear to me what the next step is. There does not seem to be consensus on the approach. I removed the Needs manual testing tag because the patch was tested and screenshots were added. Thank you for adding screenshots, @fabienly.

The issue summary has a lot of documentation and hard work done several years ago, but it probably needs to be updated. I added the Needs issue summary update tag. I hesitate to add this now because the issue isn't clear but also the issue needs Accessibility and probably Subsystem maintainer review.

The issue still seems to be Critical based on user report of data loss caused by lost form input.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

carolpettirossi’s picture

StatusFileSize
new39.69 KB

I'm facing this issue too.

I have a group type where non-admin can enable features. However, the Portal feature requires other configurations and it's available only for administrator roles.

Disabled checkbox

The above is achieved with a form_alter + after_build method:

$form['field_application_features']['widget']['#after_build'][] = 'mymodule_field_application_features_after_build';

function mymodule_field_application_features_after_build(array $element, FormStateInterface $form_state) {
  // Disable portal feature for non-administrator users.
  $user_roles = \Drupal::currentUser()->getRoles();
  if (!in_array('administrator', $user_roles)) {
//    $element['portal']['#disabled'] = TRUE;
    $element['portal']['#attributes']['readonly'] = TRUE;
    $element['portal']['#description'] = t('If you want to enable/disable Private Portal feature, please contact the Tech Team.');
  }
  return $element;
}

The bug happens when an admin enables Portal feature and then a non-admin user edits the content. After the non-admin submits the form, Portal feature is disabled. (data loss)

jayelless’s picture

Hi. I note the discussion in https://drupal.stackexchange.com/questions/832/how-can-i-disable-a-single-checkbox-in-a-checkboxes-fapi-element which advised that this can be achieved by the following code:

function mymodule_form_alter(&$form, &$form_state, $form_id) {
  $form['checkboxes_element']['#disabled'] = TRUE; //disables all options
  $form['checkboxes_element'][abc]['#disabled'] = TRUE; //disables option, called abc
}

I have used this and found is is a suitable work-around.

adinac’s picture

Re-roll of #39

dww’s picture

Status: Needs review » Needs work
Issue tags: +Bug Smash Initiative

Thanks for working on this. I agree it's a data loss bug, and therefore critical. Even though 'checkboxes' is a bit weird, if core's going to provide it, we need to support it working correctly.

Visual inspection of the patch:

  1. +++ b/core/lib/Drupal/Core/Render/Element/Checkboxes.php
    @@ -73,12 +73,19 @@ public static function processCheckboxes(&$element, FormStateInterface $form_sta
    +        if (isset($value[$key]) || (in_array($key, $element['#default_value']) && isset($element[$key]['#disabled']))) {
    

    I believe we need ... && !empty($element[$key]['#disabled']. Otherwise, '#disabled' => FALSE would trigger this case, which we don't want.

  2. +++ b/core/lib/Drupal/Core/Render/Element/Checkboxes.php
    @@ -115,6 +122,17 @@ public static function valueCallback(&$element, $input, FormStateInterface $form
    +          if (isset($element[$key]['#disabled'])) {
    

    Same here.

I tried manually testing. Indeed, case #2 from the summary is still not solved by the patch, nor covered by the updated test.

kuldeep_mehra27’s picture

StatusFileSize
new4.8 KB
mohit_aghera’s picture

Status: Needs work » Needs review

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

adinac’s picture

Re-roll

quietone’s picture

Status: Needs review » Needs work
dhirendra.mishra’s picture

Status: Needs work » Needs review
StatusFileSize
new879 bytes
new4.87 KB

Fixed the below issues..

FILE: /var/www/html/core/lib/Drupal/Core/Render/Element/Checkboxes.php
----------------------------------------------------------------------
FOUND 6 ERRORS AFFECTING 5 LINES
----------------------------------------------------------------------
129 | ERROR | [x] Line indented incorrectly; expected 8 spaces,
| | found 6
130 | ERROR | [x] Line indented incorrectly; expected 10 spaces,
| | found 8
131 | ERROR | [x] Line indented incorrectly; expected 12 spaces,
| | found 10
132 | ERROR | [x] Line indented incorrectly; expected 10 spaces,
| | found 8
133 | ERROR | [x] Line indented incorrectly; expected 8 spaces,
| | found 7
133 | ERROR | [x] Closing brace indented incorrectly; expected 6
| | spaces, found 7

danflanagan8’s picture

Is there any interest in perhaps spinning off Case 2 from the IS into a separate issue? Here are some hopefully compelling reasons to split it off.

Technical Reason
1. Case #1 is a Form API issue while Case #2 is a Field API issue.

Practical Reasons
2. Case #1 seems to be nearly fixed and tested, while Case #2 has no fix and no tests.
3. Case #1 appears to be the more frequently encountered bug based on this issue's comments, while Case #2 looks like a pretty edgy edge case.

Thoughts?

rob230’s picture

#66 does not apply to 9.2, but #69 and #71 apply fine.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Christopher Riley’s picture

I am trying to apply this patch against 9.3.5 has anyone gotten #71 to apply? I am needing it for the commerce_license module.

Thanks in advance

ranjith_kumar_k_u’s picture

StatusFileSize
new4.85 KB

Re-rolled #71

jonathanshaw’s picture

Issue summary: View changes
Status: Needs review » Reviewed & tested by the community
Related issues: +#3268325: Selected yet disabled individual options from checkboxes widget don't persist through save

#50 and #51 make it clear that core has not explicitly intended to support this use, and that to do so would be a helper feature provided by #checkboxes, the absence of support for this use case is not a bug as HTML itself does not natively support this.

However, allowing #disabled and removing data unexpectedly is definitely a nasty bug. Either we should disallow #disabled, or it should work.

I think it's a useful feature to have this, so let's continue to allow it and make it work.

I agree with #72 that splitting out case 1 and case 2 from the IS is a good idea; smaller issues make for faster progress as a rule of thumb. I have created #3268325: Selected yet disabled individual options from checkboxes widget don't persist through save to handle case 2 and postponed it on this issue.

The fix has been extensively reviewed, and the test coverage looks good to me.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
  1. +++ b/core/lib/Drupal/Core/Render/Element/Checkboxes.php
    @@ -73,12 +73,19 @@ public static function processCheckboxes(&$element, FormStateInterface $form_sta
    +        if (isset($value[$key]) || (in_array($key, $element['#default_value']) && isset($element[$key]['#disabled']) && !empty($element[$key]['#disabled']))) {
    

    in_array() needs the strict param set.

    isset($element[$key]['#disabled']) && !empty($element[$key]['#disabled'])) is equivalent to !empty($element[$key]['#disabled'])

    !empty() is probably more performant than in_array() so should be done first.

    Therefore this is better written as:
    if (isset($value[$key]) || (!empty($element[$key]['#disabled']) && in_array($key, $element['#default_value'], TRUE))) {

  2. +++ b/core/lib/Drupal/Core/Render/Element/Checkboxes.php
    @@ -115,6 +122,17 @@ public static function valueCallback(&$element, $input, FormStateInterface $form
    +          if (isset($element[$key]['#disabled']) && !empty($element[$key]['#disabled'])) {
    

    Can be written as:
    if (!empty($element[$key]['#disabled'])) {

ravi.shankar’s picture

Status: Needs work » Needs review
StatusFileSize
new4.8 KB
new1.31 KB

Made changes as per comment #78.

jonathanshaw’s picture

Status: Needs review » Reviewed & tested by the community

Thanks @ravi.shankar

alexpott’s picture

Version: 9.4.x-dev » 9.3.x-dev
Status: Reviewed & tested by the community » Fixed

Committed and pushed 28a3a8779d to 10.0.x and e5590b2254 to 9.4.x and e2443fbb83 to 9.3.x. Thanks!

diff --git a/core/modules/system/tests/src/Functional/Form/FormTest.php b/core/modules/system/tests/src/Functional/Form/FormTest.php
index 9b4c22fb7d..505bc76dbc 100644
--- a/core/modules/system/tests/src/Functional/Form/FormTest.php
+++ b/core/modules/system/tests/src/Functional/Form/FormTest.php
@@ -803,7 +803,7 @@ public function assertFormValuesDefault(array $values, array $form): void {
           $expected_value = $form[$key]['#default_value'];
         }
 
-        if (in_array($key, ['checkboxes_multiple', 'checkboxes_single_select', 'checkboxes_single_unselect'])) {
+        if (in_array($key, ['checkboxes_multiple', 'checkboxes_single_select', 'checkboxes_single_unselect'], TRUE)) {
           // Checkboxes values are not filtered out.
           $values[$key] = array_filter($values[$key]);
         }

Fixed on commit. in_array() should always have the strict param set.

  • alexpott committed 28a3a87 on 10.0.x
    Issue #2911473 by Maouna, joachim, adinac, dhirendra.mishra, ravi....

  • alexpott committed e5590b2 on 9.4.x
    Issue #2911473 by Maouna, joachim, adinac, dhirendra.mishra, ravi....

  • alexpott committed e2443fb on 9.3.x
    Issue #2911473 by Maouna, joachim, adinac, dhirendra.mishra, ravi....

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.