Wildcards in form_id?

Created on 8 November 2023, almost 2 years ago
Updated 1 January 2024, over 1 year ago

Problem/Motivation

I just found this fantastic module and started to convert my form_alter hooks to plugins. However, inside a form_alter hook, I could check for various form ids using PHP comparisons which as far as I know are not applicable to anntotations.

For example, I have a function to hook into delete forms regardless of entity type or bundle. It starts like this:

function MYMODULE_form_alter(&$form, FormStateInterface $form_state, $form_id) {

  if (strpos($form_id, 'delete_form') !== FALSE
    && strpos($form_id, '_type_delete_form') === FALSE) {
[...]

When writing a formalter plugin, I seemingly (according to documentation) need to specify all possible delete form ids, i.e. like this for my NodeDeleteFormAlter plugin:

/**
 * Class NodeDeleteFormAlter.
 *
 * @FormAlter(
 *   id = "MYMODULE_node_delete_form_alter",
 *   label = @Translation("Altering node delete forms."),
 *   form_id = {
 *    "node_page_delete_form",
 *    "node_article_delete_form",
 *    "node_blog_delete_form",
 *    "node_custom_delete_form",
 *   },
 * )
 *
 * @package Drupal\MYMODULE\Plugin\FormAlter
 */
class NodeDeleteFormAlter extends FormAlterBase {
[...]

Maybe I missed something in the documentation or lack experience.
Is there a way to avoid listing a form id for each content type, maybe by adding something like "node_*_delete_form"?
As far as I know, there is no base form id for delete forms which could be used instead. But this is only one use case, there might be others which are more complex.

💬 Support request
Status

Fixed

Version

1.0

Component

Code

Created by

🇩🇪Germany broon Potsdam

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

Comments & Activities

Production build 0.71.5 2024