Add alter hooks to allow external modules and themes to extend ContextValidator form ID logic

Created on 17 June 2025, 18 days ago

Problem/Motivation

The ContextValidator service in the gin_lb module contains hardcoded lists of form IDs and regular expressions used to determine whether a form should be styled and enhanced by Gin Layout Builder.

Currently, there is no supported way for external modules or themes to declare their own forms as part of the Layout Builder context. This limits the flexibility of Gin LB integrations and violates Drupal’s extensibility best practices.

Steps to reproduce

  • Create a custom form related to Layout Builder (e.g., a custom block configuration form).
  • Attempt to have it styled by Gin LB.
  • Observe that it is not detected by ContextValidator::isLayoutBuilderFormId() unless its ID is hardcoded.

Proposed resolution

  • Refactor ContextValidator to lazy-load the form ID and regex lists.
  • Introduce two alter hooks:
    • hook_gin_lb_form_ids_alter(array &$form_ids)
    • hook_gin_lb_form_id_regexes_alter(array &$regexes)
  • Invoke these hooks using ModuleHandlerInterface.
  • Allow themes to influence behavior via ThemeManagerInterface if necessary (e.g., context-based theming decisions).
  • Document the new alter hooks with examples and update related PHPDocs.

Remaining tasks

  • Implement getFormIds() and getFormIdRegexes() methods in ContextValidator.
  • Use moduleHandler->alter() to trigger hook implementations for both arrays.
  • Ensure compatibility with existing form detection logic.
  • Document the new hooks and update API reference where applicable.
  • Test with example form ID and regex added from a custom module and a theme.

User interface changes

None directly. This change affects backend extensibility only.

Introduced terminology

None.

API changes

New alter hooks introduced to extend Gin Layout Builder context form detection:

function hook_gin_lb_form_ids_alter(array &$form_ids): void;
function hook_gin_lb_form_id_regexes_alter(array &$regexes): void;

These allow external modules and themes to register additional forms for styling and enhancements.

Data model changes

None.

Release notes snippet

The gin_lb module now supports extending form detection logic used by ContextValidator via alter hooks. Use hook_gin_lb_form_ids_alter() and hook_gin_lb_form_id_regexes_alter() in your module or theme to register custom forms for Gin LB enhancements.

Feature request
Status

Active

Version

1.0

Component

Code

Created by

🇫🇷France xavier.masson Haute-Normandie

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

Comments & Activities

Production build 0.71.5 2024