How to disable single checkbox and set default values in multi-value checkboxes form elemen

Created on 5 August 2023, 11 months ago
Updated 18 August 2023, 11 months ago

Problem/Motivation

Selecting one of the checkboxes should be done with the field name roles[content_editor] as an example. However, that field is not found, as the content_editor is not available as an element child, whereas the roles[authenticated] is found, as the form field widget declares that option as disabled in the user profile form.

Proposed resolution

Add missing element childs for each option in checkboxes or other multi value field widgets.

💬 Support request
Status

Fixed

Version

2.0

Component

Code

Created by

🇩🇪Germany jurgenhaas Gottmadingen

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @jurgenhaas
  • Open in Jenkins → Open on Drupal.org →
    Core: 10.1.x + Environment: PHP 8.1 & MySQL 8
    last update 11 months ago
    272 pass, 2 fail
  • @jurgenhaas opened merge request.
  • 🇩🇪Germany mxh Offenburg

    It's hard trying to find a solution for a problem that is not concretely described here. What are you trying to achieve?

    lookupFormElements is a read operation. Your MR383 makes changes in a render array within a widely used helper method. This looks wrong from a purposeful and performance perspective. You're probably trying to do something with a form render array that is not yet completely built up (wrong event).

  • 🇩🇪Germany jurgenhaas Gottmadingen

    It's hard trying to find a solution for a problem that is not concretely described here. What are you trying to achieve?

    It's about the user profile form (just as an example, like mentioned above) with a field widget of type checkboxes. In addition to that, there is this thread on #ECA channel on Slack which has mentioned you as well.

    You're probably trying to do something with a form render array that is not yet completely built up (wrong event).

    Maybe not. When I analysed the situation for that scenario, the field widget for checkboxes already disabled the authenticated role, and as a result, it declares that role id as an element child with the disabled and default value as the array keys. It appears that this form widget doesn't do this for options, that are enabled instead of disabled. And that's why they are not found by our lookup; it's not prepared for the absence of that element child being all default.

    The event being used is "Build form". I don't think there is a later one that could be used. Thee action being used is "Form field: set as disabled".

    Of course, if there is a better way of doing this, I'm all up for suggestions and happy to review.

  • Open in Jenkins → Open on Drupal.org →
    Core: 10.1.x + Environment: PHP 8.1 & MySQL 8
    last update 11 months ago
    296 pass
  • 🇩🇪Germany mxh Offenburg

    I've read the Slack thread and attaching an example model for it here. Some notes for this example:

    • You need to use Process form for setting available options on the field, because only then you can react upon the pre-built array coming from the field widget plugin.
    • You need to use After form build for manipulating the display of the field widget outcome, because only then the processed render array of the field widget is available.

    Further information about the form-events and their order of execution can be found here: https://ecaguide.org/plugins/eca/form/#usage

  • Status changed to Needs review 11 months ago
  • 🇩🇪Germany jurgenhaas Gottmadingen

    Oh, this is amazing, thanks @mxh

    And in the current use case with user roles, we don't even have to set the available roles in the process form event, as the roles are already present. It's totally sufficient, to disable a role in the after form build event. And to reference the correct option, there is the roles.administrator syntax possible, or one can even use roles[administrator] which is the name of the field when grabbing it from the browser console in the source code.

    @Zee, can you please review this and provide us with your feedback as well?

  • 🇩🇪Germany mxh Offenburg

    Just seen that the OP also asked for how to set default values on a checkboxes field. Attached an example model for that.

    Unfortunately, I encountered a bug while trying to set default values on a checkboxes fied. A fix for that is available in the linked issue. You'd need to apply the fix in that issue first, otherwise the attached example won't work.

  • 🇩🇪Germany jurgenhaas Gottmadingen

    FYI @Zee, the fixes for both issues are already available in 2.0.x-dev, just in case you wanted to test this as well and then setting this one to RTBC if it resolves your issues.

  • Status changed to Fixed 11 months ago
  • 🇩🇪Germany jurgenhaas Gottmadingen

    Looks like we lost @Zee on the way here. I'm marking the issue as fixed, a new issue can still be created if any follow-up issue arises.

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

Production build 0.69.0 2024