Add paragraph bundle to widget forms to allow easier editing of paragraph forms

Created on 10 April 2017, over 8 years ago
Updated 25 January 2023, over 2 years ago

This issues started as an ask for new hooks, but has been simplified to updating the widget form arrays to provide the paragraph bundle names.

hook_field_widget_WIDGET_TYPE_form_alter() can be used to alter paragraph forms using with type name paragraphs for fields using the new experimental widget or entity_reference_paragraphs for the classic inline forms.

Original suggestion:
Add 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()

When trying to form alter paragraphs for an individual type of paragraph is extremely difficult having to pick through the parent forms to and to edit a single type of form has a lot of overheard.

With these 2 alter hooks you can quickly alter the subform for and not take up too much processing time.

Feature request
Status

Fixed

Version

1.0

Component

Code

Created by

🇦🇺Australia gordon Melbourne

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇫🇷France arnaud-brugnon

    Great news

    https://www.drupal.org/node/3180429

    hook_field_widget_WIDGET_TYPE_form_alter no longer exist.
    Use hook_field_widget_single_element_form_alter instead

  • 🇨🇦Canada alberto56

    Here is my experiencing updating paragraphs 1.17 (with the patch at #16) to 1.19 (at the time of this writing the latest version of Paragraphs) on Drupal 10.5.1. This is purely my own experience and I am probably missing something, but I'd like to explain why I have decided to keep the patch at #16 of this issue, even though the issue is purportedly fixed as of May 18, 2018, as per #73.

    When I inherited the site with paragraphs 1.17 with the patch at #16, one of the custom modules was implementing a hook like this:

    /**
     * Implements hook_form_paragraphs_subform_TYPE_alter().
     */
    function MY_MODULE_form_paragraphs_subform_PARAGRAPH_TYPE_alter(array &$subform, FormStateInterface $form_state, $delta) {
      $subform['description']['#markup'] = 'Some markup';
    }
    

    This was causing "some markup" to appear when I visited a widget controlling PARAGRAPH TYPE.

    Because this issue #2868155 is marked as fixed, I updated paragraphs to 1.19 and did not patch it. I examined the code at https://git.drupalcode.org/project/paragraphs/-/blob/8.x-1.x/tests/modul... and found that the test implements

    /**
     * Implements hook_field_widget_WIDGET_TYPE_form_alter().
     */
    function paragraphs_test_field_widget_single_element_entity_reference_paragraphs_form_alter(&$element, &$form_state, $context) {
      if ($element['#paragraph_type'] == 'altered_paragraph') {
        $element['subform']['field_text']['widget'][0]['#title'] = 'Altered title';
      }
    }
    

    However, as mentioned by @arnaud-brugnon in #94, according to https://www.drupal.org/node/3180429 , hook_field_widget_WIDGET_TYPE_form_alter() has been "marked as deprecated and will be removed in Drupal 10.".

    I am assuming, therefore, that I cannot hook_field_widget_WIDGET_TYPE_form_alter().

    https://www.drupal.org/node/3180429 states that:

    The new field widget hooks hook_field_widget_single_element_form_alter (hook_field_widget_single_element_WIDGET_TYPE_form_alter) should be used instead of hook_field_widget_form_alter (hook_field_widget_WIDGET_TYPE_form_alter).

    However when I try to replace my existing hook, MY_MODULE_form_paragraphs_subform_PARAGRAPH_TYPE_alter() for Paragraphs 1.17 patched with #16, with MY_MODULE_field_widget_single_element_form_alter() for Paragraphs 1.19 unpatched as described in https://api.drupal.org/api/drupal/core%21modules%21field%21field.api.php..., I am unable to determine the paragraph type (I'm sure there is a way, I just can't figure it out):

    I can determine that the widget type is entity_reference because $field_definition->getType() == 'entity_reference', however it's not clear to me how to determine the paragraph type in order to alter the form for that paragraph.

    In my tests, applying the patch at #16 to Paragraphs 1.19 makes it so I can continue to use hook_form_paragraphs_subform_TYPE_alter() as I could with Paragraphs 1.17 patched with #16.

Production build 0.71.5 2024