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.

CommentFileSizeAuthor
#4 3400099-4.patch2.63 KBaronne

Comments

broon created an issue. See original summary.

uridrupal’s picture

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.

aronne’s picture

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

aronne’s picture

Status: Active » Needs review
StatusFileSize
new2.63 KB

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

  • aronne authored 9fc10fb7 on 8.x-1.x
    Issue #3400099 by aronne, broon: Wildcards in form_id?
    
aronne’s picture

Status: Needs review » Fixed
broon’s picture

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: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.