21

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.

1
  • 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
17

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';
  }
}
4
  • 16
    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
  • 3
    I am not sure how to alter some paragraph fields when the edit mode of that paragraph is set to "closed" in the form display. Because in that case, the 'subform' does not return any of the inline fields. – Pratip Ghosh Apr 24 '20 at 11:09
  • doesn't work for me but mymodule_field_widget_paragraphs_form_alter works, see below my answer if you are in the same case. – Christophe CARON Oct 9 '20 at 9:15
3

If you are using Paragraphs Browser Field Widgets, then the WIDGET_TYPE ids for hook hook_field_widget_WIDGET_TYPE_form_alter() are different:

Field Widget Label Field Widget ID Provider
Paragraphs Classic entity_reference_paragraphs Paragraphs
Paragraphs EXPERIMENTAL paragraphs Paragraphs
Paragraphs Browser Classic entity_reference_paragraphs_browser Paragraphs Browser
Paragraphs Browser EXPERIMENTAL paragraphs_browser Paragraphs Browser
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

If you are using Paragraphs Browser as field widget, things have one more twist for you. The hook that fits is named hook_field_widget_paragraphs_browser_form_alter() Took my quite some time to figure out...

0

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
  • 6
    Per this comment, those hooks were not added in favor of core's hook_field_widget_WIDGET_TYPE_form_alter(). – powpow12 Feb 28 '19 at 1:39
0

In my case ( Drupal 8/9 - multi paragraphs entity reference field) hook mymodule_field_widget_entity_reference_paragraphs_form_alter doesn't work, I used mymodule_field_widget_paragraphs_form_alter like this :

<?php
/**
 * Implements hook_field_widget_WIDGET_TYPE_form_alter().
*/
function mymodule_field_widget_paragraphs_form_alter(&$element, &$form_state, $context) {
 if ( $element['#paragraph_type'] == 'content_selection') {
   //my specifid need 
   $element["subform"]["field_contents_related"]["widget"][0]["target_type"]["#options"]["taxonomy_term"] = t('Another text');
 }
}

Tips : to find quickly which field widget form alter is available in your form :

In core core/lib/Drupal/Core/Field/WidgetBase.php Just before :

\Drupal::moduleHandler()->alter(['field_widget_form', 'field_widget_' . $this->getPluginId() . '_form'], $element, $form_state, $context);

(in my case line 353 in protected function formSingleElement )

\Drupal::messenger()->addMessage('field_widget_' . $this->getPluginId() . '_form');

You will see all the form field widget alter in my case :

field_widget_datetime_timestamp_form field_widget_entity_browser_entity_reference_form field_widget_multiple_options_select_form field_widget_comment_default_form field_widget_dynamic_entity_reference_default_form field_widget_string_textfield_form field_widget_paragraphs_form field_widget_boolean_checkbox_form field_widget_options_buttons_form field_widget_text_textarea_form field_widget_metatag_firehose_form field_widget_entity_reference_autocomplete_form field_widget_language_select_form field_widget_path_form field_widget_datetime_timestamp_no_default_form field_widget_string_textarea_form

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.