Wildcards in form_id?

Created on 8 November 2023, about 1 year ago
Updated 1 January 2024, 11 months 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

  • Issue created by @broon
  • 🇪🇸Spain uridrupal

    Would be great to know, I am also experiencing a similar use case with commerce add to cart. Tried using the base_form_id but it didn't work.

  • 🇮🇹Italy aronne

    Hi guys,
    Yes this feature request is totally legit.
    I’m gonna work on it in the next couple days.
    Stay tuned and text you soon

    aronne

  • Status changed to Needs review 12 months ago
  • 🇮🇹Italy aronne

    Hi guys,
    please feel free to try this patch and let me know if it works for you as well.

    /**
     * Class D10TestingFormAlter
     *
     * @FormAlter(
     *   id = "d10_form_alter",
     *   label = @Translation("Altering forms."),
     *   form_id={"*"}
     * )
     *
     * @package Drupal\d10_testing\Plugin\FormAlter
     */
    

    This definition should alter any existing form. I also tried using "user_*" as form_id.

    Anyway, let me know.

    Regards,
    aronne

  • Status changed to Fixed 11 months ago
  • 🇩🇪Germany broon Potsdam

    Sorry for being late to the after-work party. I did test it with my custom FormAlter plugins and it works like a charm, good job! If it wasn't fixed and already released in 1.6, I would give it an RTBC. ;)

  • Status changed to Fixed 11 months ago
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024