Score:-2

How to check and disable one of the checkboxes in a form?

kz flag

I found this snippet in the code for a form ($roles is an array of roles).

$form['config_options']['roles'] = [
  '#type' => 'checkboxes',
  '#options' => $roles,
];
// Check and disable the 'authenticated' role.
$form['config_options']['roles'][RoleInterface::AUTHENTICATED_ID] = [
  '#default_value' => TRUE,
  '#disabled' => TRUE,
];

What this does is to check the checkbox for the 'authenticated' role, and then disables it (so it can't be unchecked). This works.

I have a very similar requirement. In a form I have checkboxes for a bunch of fields, where $fields is an array of them. The fields are from user profile. I want to check and disable the 'email' field.

Based upon the code snippet above, I think the solution should be something like this:

$form['config_fields']['fields'] = [
  '#type' => 'checkboxes',
  '#options' => $fields,
];
// Check and disable the 'email' field.
$form['config_fields']['fields'][ -- What to put here? -- ] = [
  '#default_value' => TRUE,
  '#disabled' => TRUE,
];

However, I have no idea what to put in the selector where I've written " -- What to put here? --".

Based upon a comment from Clive I've tried to examine $fields. This is what the devel Default variables-dumper gives me (using short array syntax):

$fields = [
  …
  mail => stdClass Object (
    [__CLASS__] => Drupal\Core\StringTranslation\TranslatableMarkup
    [translatedMarkup:protected] => 
    [options:protected] => []
    [stringTranslation:protected] => 
    [string:protected] => Email
    [arguments:protected] => []
  )
  …
]

So it is a bit more complex than the example given in Clive's comment (e.g. it is an object, not an array (as implied by Clive).

So far I've tried 'Email'.

$form['config_fields']['fields'] = [
  '#type' => 'checkboxes',
  '#options' => $fields,
];
// Check and disable the 'email' field.
$form['config_fields']['fields']['Email'] = [
  '#default_value' => TRUE,
  '#disabled' => TRUE,
];

This does not work.

Help will be appreciated.

Also, if I am barking up the wrong tree, and there exists another way to do this, I shall equally appreciate an alternative solution.

(I am using Drupal 9).

cn flag
It would be the relevant array key from `$fields`. e.g. if `$fields = ['foo' => 'Foo', 'bar' => 'Bar'];`, and you want to check `Bar`, the key is `bar`
Free Radical avatar
kz flag
@Clive Thanks for the suggestion. I've expanded `fields` and updated the question with the expanded version. It looks like an object, while your suggestion implies that it is an array. It is still not clear to me what to use as key. Following your suggestion, I think it should be 'Email', but 'Email' does not work for me.
Score:0
kz flag

OK, it turns out that the key is simply the machine name of the field you want to check and disable. This works:

$form['config_fields']['fields'] = [
  '#type' => 'checkboxes',
  '#options' => $fields,
];
// Check and disable the 'mail' field.
$form['config_fields']['fields']['mail'] = [
  '#default_value' => TRUE,
  '#disabled' => TRUE,
];
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.