0

I can't seem to figure out how to make checkbox field on my entity display as disabled in the admin form area. Looked in Drupal 8s "documentation" but couldn't seem to find anything on it.

I tried variations on the code below but nothing seemed to work. Any help is appreciated. Thank you.

$fields['some_field'] = BaseFieldDefinition::create('boolean')
 ->setLabel(t('Some Field'))
 ->setDescription(t('A Field description'))
 ->setReadOnly(TRUE)
 ->setRevisionable(FALSE)
 ->setDefaultValue(FALSE)
 ->setDisplayOptions('form', [
   'type' => 'boolean_checkbox',
   'attributes' => array(
    'disabled' => true,
   ),
   'weight' => 0,
]);
0

The BooleanCheckboxWidget has no settings for HTML attributes. You either have to create your own Widget or use hook_field_widget_WIDGET_TYPE_form_alter to add the disabled attribute.

Not tested personally, but you might also take a look at the Read-only Field Widget, could save you some coding: setDisplayOptions('form', ['type' => 'readonly_field_widget', ...])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.