0

I'm trying to disable a field in a Paragraph when it's added to a certain content type. Currently I'm using the following:

function my_module_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  switch($form_id) {
    case "node_content_type_form":
        $form['field_paragraph_content']['widget'][0]['subform']['field_paragraph_field']['#disabled'] = 'disabled';
      break;
  }
}

This works only after the node containing the paragraph has already been saved. When the paragraph in question is first added to the node the field_paragraph_field is not disabled.

How can I preprocess the Paragraph form when it is initially added to the content type so this field is always disabled?

1 Answer 1

2

If you want it to work when creating a node and when editing the node, you should make the change using both the node add form and the node edit form:

mymodule_form_node_content_type_form_alter()

mymodule_form_node_content_type_edit_form_alter()

Or in your hook_form_alter in the question:

case "node_content_type_form":
case "node_content_type_edit_form":

There's another way to do this using hook_field_widget_form_alter():

function mymodule_field_widget_form_alter(&$element, FormStateInterface $form_state, $context) {

  if ($context['items']->getFieldDefinition()->getTargetBundle() == 'content_type' && $context['items']->getFieldDefinition()->getName() == 'field_paragraph_field') {
    $element['#disabled'] = TRUE;
  }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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