Preprocess events should not require specific class

Created on 29 June 2023, 12 months ago

Problem/Motivation

Currently in order to preprocess any template via an event there needs to be a specific event class for that specific theme hook. For example, to preprocess a block using an event as you would using hook_preprocess_block(), BlockPreprocessEvent.php was created.

This seems unsustainable when you consider that in order to match Drupal core hook_preprocess_HOOK functionality, theme hooks provided by contrib modules will need to be hardcoded into the the hook_event_dispatcher/preprocess_event_dispatcher module. I assume the purpose of this module is to eventually hopefully be part of Drupal core. If we continue down this route, I don't see how that's possible due to this dependency issue. For example, in order for Drupal core to run hook_preprocess_paragraph(), it does not need a class specifically for paragraphs. Core code does not need to contain any reference to "paragraph" in order for the preprocess hook to work, and it shouldn't need to.

The preprocess event system seems especially odd when you compare these event classes and realize how little they actually differ. For example, here is BlockPreProcessEvent vs. FieldPreprocessEvent. Surely there's some way to do this via a single generic preprocess event class?

Thanks for considering.


namespace Drupal\preprocess_event_dispatcher\Event;

/**
 * Class BlockPreprocessEvent.
 */
final class BlockPreprocessEvent extends AbstractPreprocessEvent {

  /**
   * {@inheritdoc}
   */
  public static function getHook(): string {
    return 'block';
  }

}

namespace Drupal\preprocess_event_dispatcher\Event;

/**
 * Class FieldPreprocessEvent.
 */
final class FieldPreprocessEvent extends AbstractPreprocessEvent {

  /**
   * {@inheritdoc}
   */
  public static function getHook(): string {
    return 'field';
  }

}

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

✨ Feature request
Status

Active

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

Comments & Activities

Production build 0.69.0 2024