Event subscriber for hook_field_widget_single_element_WIDGET_TYPE_form_alter does not work

Created on 2 January 2024, 6 months ago
Updated 20 January 2024, 5 months ago

Problem/Motivation

Attempting to replace an implementation of hook_field_widget_single_element_WIDGET_TYPE_form_alter with an event subscriber by following the example at \Drupal\hook_event_dispatcher\ExampleFieldEventSubscribers does not work. The event subscriber method is never called.

Steps to reproduce

  1. Install paragraphs module, create a paragraph type, create a field on a content type that references the paragraph type.
  2. Add the equivalent hook to a .module file for comparison purposes:
    /**
     * Implements hook_field_widget_single_element_WIDGET_TYPE_form_alter().
     */
    function MY_MODULE_field_widget_single_element_paragraphs_form_alter(&$element, &$form_state, $context): void {
      if ($element['#paragraph_type'] === 'MY_PARAGRAPH_TYPE') {
        // Code here.
      }
    }
    
  3. Clear caches.
  4. Create a node of the content type. Add a paragraph. See that the hook is called.
  5. Enable field_event_dispatcher module.
  6. Create an event subscriber class with these methods:
      /**
       * {@inheritdoc}
       *
       * Establish what events this subscriber responds to.
       *
       * To add specific base form forms:
       * 'hook_event_dispatcher.form_base_node_form.alter' => 'alterNodeForm',
       */
      public static function getSubscribedEvents() {
        return [
          FormHookEvents::FORM_ALTER => 'alterEventNodeForm',
          FieldHookEvents::WIDGET_SINGLE_ELEMENT_FORM_ALTER => 'widgetSingleElementAlter',
          'hook_event_dispatcher.widget_single_element_paragraphs.alter' => 'widgetSingleElementParagraphsAlter',
          FieldHookEvents::WIDGET_COMPLETE_FORM_ALTER => 'widgetCompleteAlter'
        ];
      }
    
      /**
       * EQUIVALENT: hook_field_widget_single_element_WIDGET_TYPE_form_alter().
       *
       * @param \Drupal\field_event_dispatcher\Event\Field\WidgetSingleElementTypeFormAlterEvent $event
       *   The event.
       */
      public function widgetSingleElementParagraphsAlter(WidgetSingleElementTypeFormAlterEvent $event): void {
        $element = &$event->getElement();
        if (isset($element['#paragraph_type']) && $element['#paragraph_type'] === 'p_location_off_campus') {
          // Code here.
        }
      }
      
      // Add other methods from getSubscribedEvents() if you like. All the other methods will be called as expected.
    
  7. Clear caches.
  8. Create a node of the content type. Add a paragraph. See that the hook is called. See that the event subscriber method is not.

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

🐛 Bug report
Status

Fixed

Version

4.0

Component

Code

Created by

🇺🇸United States maskedjellybean Portland, OR

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

Merge Requests

Comments & Activities

Production build 0.69.0 2024