I need to output the form id of a webform entity. I load a chunk of enabled webforms directly from the database by using


Afterwards I have to output the form_id's of the webform entities.

How to achieve this? I cannot see any "form_id" stored in the returned entity object.


I can use the following script to receive a static version of the form id:

$target_form_id = str_replace('email_confirm_redirect_2_', '', $entity->getSubmissionForm()['#form_id']);

Still not really professional

  • "contact" is the "webform id". The final form_id should be "webform_submission_contact_add_form". Of course I can use this a pattern... just prefix "webform_submission_" and suffix "_add_form". But I thought there would be a cleaner more precise way. – mogio Jul 8 '18 at 12:58
  • The webform entity itself doesn't have a unique form ID. The actual form is an entity form for the webform_submission entity, and it can have different IDs for different operations (eg. add and edit). The logic's in Drupal\webform\WebformSubmissionForm\getFormId() and it doesn't look like you can cleanly generate the ID without the actual form object, but you can expect the pattern to remain stable for BC with existing form alter hooks. What's the overall goal here? – Andy Jul 10 '18 at 12:47
  • I have a backend form that lets users pick an already setup webform. Later then I will react on the form_alter hook to the form id to alter this form. For this scenario I have to save the form id in the database. – mogio Jul 11 '18 at 11:36

Rather than storing the form ID, you could just store the webform ID and check that in a form alter. Webform provides its own form alter hook that only fires for webforms and lets you put the hook in a dedicated include file.

See honeypot_webform_submission_form_alter() in third_party_settings/webform.honeypot.inc for an example of getting the webform from the hook:

 * Implements hook_webform_submission_form_alter().
function honeypot_webform_submission_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  // <snip>

  /** @var \Drupal\webform\WebformSubmissionInterface $webform_submission */
  $webform_submission = $form_state->getFormObject()->getEntity();
  $webform = $webform_submission->getWebform();

From there you can use $webform->id() to get the webform ID.

  • I know. But my logic does also react to drupal on core contact forms and custom forms. Maybe I should refactor my module and have a hook for every type which then calls a function. Wouldn' the webform id miss the type of view of the form (for example "add")? – mogio Jul 12 '18 at 14:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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