Disable a Field in a Paragraphs Form

3.7k Views Asked by At

Does anyone know how to alter a field in a paragraphs (ajax) backend form in Drupal 8? I want to disable a field, but keep it visible.

Thanks

2

There are 2 best solutions below

1
Fazeela Abu Zohra On

You can disable a form field by either using hook_form_alter() or by hook_form_FORM_ID_alter().

I would always suggest you to use hook_form_FORM_ID_alter(). Suppose test is your modules name and user_register_form is the Id of the form.

test_form_user_register_form_alter(&$form, &$form_state, $form_id) {
  $form['fieldname'] = array(
    '#type' => 'textfield',
    '#title' => t('Text label'),
    '#attributes' => array('disabled' => 'disabled'),
  );
}

Happy coding!!!

0
Nicholas On
function hook__form_FORM_ID_alter(&$form,\Drupal\Core\Form\FormStateInterface 
$form_state, $form_id) {
//output your form structure to know what to target in the form array($form[])
#kint( $form['title']);
$form['title']['#disabled'] = TRUE;

}

The above code disables the title field (Drupal 8.5) in the 'FORM_ID' you want to modify.