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.
| Comment | File | Size | Author |
|---|---|---|---|
| #4 | 3400099-4.patch | 2.63 KB | aronne |
Comments
Comment #2
uridrupal commentedWould 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.
Comment #3
aronne commentedHi 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
Comment #4
aronne commentedHi guys,
please feel free to try this patch and let me know if it works for you as well.
This definition should alter any existing form. I also tried using "user_*" as form_id.
Anyway, let me know.
Regards,
aronne
Comment #6
aronne commentedComment #7
broonSorry 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. ;)