0

I was wondering if there is a way to alter paragraph fields when the paragraph is set to "closed" in the Edit mode setting of form display. Because in that case the hook_field_widget_WIDGET_TYPE_form_alter does not return any of the inline paragraph fields inside the $element['subform']

To give a little more details, there's a paragraph field that holds some more native fields within itself. When I am trying to add/edit the node with which it is attached, i want to alter some fields within the paragraph. Since the paragraph is closed the fields within will be rendered via ajax call on the fly. I was looking for a way to alter them during this ajax call, that is just before they get rendered in the form so that I can alter their attributes.

Any idea?

3
  • Can you explain your use case a little more and share some code? Do you want to edit the paragraph field itself, or the way its rendered? The form is not yet rendered in closed mode because the form is rendered on the fly via AJAX when the closed paragraph is opened.
    – Beau
    Apr 24, 2020 at 19:35
  • Well there's a paragraph field that holds some more native fields within itself. When I am trying to add/edit the node with which it is attached, i want to alter some fields within the paragraph. Yes you are right that when the paragraph is closed the fields within will be rendered via ajax call on the fly. But is there a way to alter them during this ajax call, that is just before they get rendered in the form so that I can alter their attributes. I hope I make myself clear. Apr 26, 2020 at 3:12
  • Thanks, Pratip. Could you please edit your question to include that information? I've offered an answer that I think might help.
    – Beau
    Apr 27, 2020 at 13:54

1 Answer 1

1

I think there are two approaches you could take here. In the past I've successfully extended the InlineParagraphsWidget field widget in order to alter the form display for Paragraphs. I think that hook_ajax_render_alter() might also be a good solution.

I have not used hook_ajax_render_alter(), but I have successfully extended InlineParagraphsWidget for the purpose of altering the AJAX response. Specifically, look into overriding the ::addMoreAjax() and ::formElement() methods.

Below is a simplified version of the ::addMoreAjax() callback that I used. Use $insert_element = parent::addMoreAjax($form, $form_state); to get a reference to the triggering element.

public static function addMoreAjax(array $form, FormStateInterface $form_state)
    {
        $insert_element = parent::addMoreAjax($form, $form_state);

        // TODO: This is NOT the cleanest way to find the wrapper ID.
        // Find a cleaner way to get these values.
        $wrapper_id = explode('"', $insert_element['#prefix'])[1];
        $pos = strrpos($wrapper_id, '--');
        $wrapper_id = '#' . substr($wrapper_id, 0, $pos);

        // If you needed the field name.
        $field_name = end($insert_element['#parents']);

        $response = new AjaxResponse();
        $response->addCommand(new InsertCommand($wrapper_id, $insert_element));
        return $response;
    }

You will probably also want to provide validation by overriding ::elementValidate().

You will also likely need to implement hook_field_widget_info_alter() in order to make your new widget active.

function my_module_field_widget_info_alter(array &$info)
{

    $info['entity_reference_paragraphs']['class'] = MyCustomInlineParagraphsWidget::class;
    $info['entity_reference_paragraphs']['provider'] = 'my_module';
}

Happy coding!

1
  • Excellent Beau. Thanks for taking your time out to explain it so well. Let me try these methods to see if this helps. I will append my findings later in this thread. Apr 28, 2020 at 5:37

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.