0

I have used Webform many times before but this is the first time I am testing Webform Node :

Provides a Webform content type which allows webforms to be integrated into a website as nodes.

Usually, I integrate webforms with blocks.

Everything works fine but I notice that the webform $form_id contains the node id (here nid = 174):

webform_submission_shop_contact_node_174_add_form

This poses a problem for me because I want to make a hook to add a library. The hook will therefore not be able to work on all environments once deployed because here the node is content and the nid will be different.

In hook_form_alter

if ($form_id === 'webform_submission_shop_contact_node_174_add_form') {
    $form['#attached']['library'][] = 'custom_theme/contact';
}

Do you know if I can modify the id of this webform integrated into a node?

I saw this option here /admin/structure/webform/manage/{webform}/settings/form

enter image description here

which allows you to create a custom id but this has no effect (probably because the webform is integrated into a node).

2
  • Are you looking for the base form ID, webform_submission_shop_contact_form in your case, or the webform ID shop_contact?
    – 4uk4
    Commented Feb 20 at 12:53
  • Thanks @4uk4 , I hadn't even thought to look at this solution. if I do dump($form), I can clearly see the #webform_id key : shop_contact. I can now add my library like this in hook_form_alter : if (isset($form['#webform_id']) && $form['#webform_id'] === 'shop_contact') { ... } So simple, it works. Do you want to answer the question so I can vote? Commented Feb 20 at 13:34

1 Answer 1

1

Setting a breakpoint in a form alter hook I see at least two ways to get the info you are looking for:

$form['#webform_id']

or the base form ID (without the node details) in

$form_state->getBuildInfo()

in your case webform_submission_shop_contact_form, which can also be used in hook_form_BASE_FORM_ID_alter().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.