I'm working with paragraph and I try to change a paragraph with AJAX.

My content type have a content field which contains "section" paragraphs which can contain various paragraph types which themselves can contain various paragraphs types.

To do certain modification on paragraphs, I use hook_field_widget_paragraphs_form_alter. It's work well but I meet a problem when I try to use #ajax because I don't know how to retrieve which paragraph has been modified in the ajax callback because I receive the entire form from the content and no information on which part has been modified. Here my code:

function mymodule_field_widget_paragraphs_form_alter(&$element, &$form_state, $context) {
  switch ($element['#paragraph_type']) {
    case 'block_special':
      $element['subform']['field_type']['widget']['#ajax'] = [
          'wrapper' => 'type-bs-wrapper',
          'callback' => '_type_paragraph_callback',

        $element['subform']['field_content']['widget']['#attributes'] = ['id' => 'type-bs-wrapper'];
        $values_paragraph = $form_state->getValues();
        if(!empty($values_paragraph)) {
          foreach ($element['#field_parents'] as $field) {
            $values_paragraph = $values_paragraph[$field];

          $type = $values_paragraph['subform']['field_type'];

        if (!empty($type) && $type === 'exception') {
function _type_paragraph_callback(array $form, FormStateInterface $form_state) {

  return $form; // <- How can I know what I should return?

I don't know which paragraph have been return. I don't know if I can pass the argument $element['#parent'] from the function hook_field_widget_paragraphs_form_alter (but how in this case) or if I should do a recursive research between default value of all paragraphs of my form between form_values (which will be laborious).

1 Answer 1


You should be able to get this information from the submitted data in $form_state, specifically the triggering element.

For example:

use Drupal\Component\Utility\NestedArray;

function _type_paragraph_callback(array $form, FormStateInterface $form_state) {
  $button = $form_state->getTriggeringElement();
  // Go one level up in the form, to the widgets container.
  $element = NestedArray::getValue($form, array_slice($button['#array_parents'], 0, -2));

  return $element;

I've copied this from the Paragraphs module: modules/contrib/paragraphs/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php

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.