18

Using the Paragraphs module in Drupal 8, I am attempting to alter the behavior of the form fields contained within the paragraph field. This is so I can add behavior like AJAX callbacks, hide show other fields using states etc.

I can access the paragraph parent field using this function:

function hook_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) { 
}

But how do I access the fields inside the paragraph?

I have tried hook_field_widget_form_alter but to no avail.

How do I do this?

Note: I am still quite new to Drupal development.

  • Try this in your alter, kint($form); and kint($form_id); and that should give you the id for the hook id alter and then it should also give you the array paths for what you want to alter. – Danny Englander Aug 8 '16 at 14:34
12

Since paragraphs^1.3 you can use hook_field_widget_WIDGET_TYPE_form_alter() (I linked the 8.2 version because there is a good comment about using the hook).

You can see the following example in modules/contrib/paragraphs/tests/modules/paragraphs_test/paragraphs_test.module

/**  
 * Implements hook_field_widget_WIDGET_TYPE_form_alter().  
 */  
function paragraphs_test_field_widget_entity_reference_paragraphs_form_alter(&$element, &$form_state, $context) {
  if ($element['#paragraph_type'] == 'altered_paragraph') {
    $element['subform']['field_text']['widget'][0]['#title'] = 'Altered title';
  }
}
  • 5
    Note that if you are using the "Paragraphs EXPERIMENTAL" widget instead of "Paragraphs Classic", then the widget type is just paragraphs instead of entity_reference_paragraphs. – Dalin Dec 14 '18 at 21:32
  • 1
    Some useful things (because it's impossible to debug these hooks): /* @var $paragraph \Drupal\paragraphs\Entity\Paragraph */ $paragraph = $form_state->get('paragraph'); $delta = $context['delta']; – Dalin Dec 14 '18 at 22:12
1

Using hook_form_alter you could alter anything in the form, even stuff coming from widgets. It can easily get a bit messy and it tend to break easily when you mess around with widgets.

If you want to alter widgets what you can do it make your own widget. Widgets are plugins (PHP class) so you can make your own subclass the one you have and alter either by completely overwriting functions or by extending functions.

Widgets use annotation system, so you need to define name, id etc as comments above the class. Easiest way is simply to copy the original widget and alter give it a new name and id, rest should be like it needs to be.

Remember to clear cache to make Drupal find the new plugin.

-1

There's a patch for this in the Paragraphs issue queue.

It adds new hooks:

hook_form_paragraphs_subform_alter()
hook_form_paragraphs_subform_TYPE_alter()
hook_form_paragraphs_subform_WIDGET_alter()
hook_form_paragraphs_subform_WIDGET_TYPE_alter()
  • 1
    Per this comment, those hooks were not added in favor of core's hook_field_widget_WIDGET_TYPE_form_alter(). – powpow12 Feb 28 at 1:39

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.