We require site administrators to create new webforms with the machine name beginning with site_. (This is because config ignore is set to ignore webform.webform.site_* for user generated forms while other forms are part of config) How do you validate and set the webform machine name to begin with this prefix value?

1 Answer 1


By adding this code to a module, administrators can create forms that will can be exported to config while other users with permissions to create webforms will automatically have the machine name set with the site_ prefix.

function mymodule_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  // Adds validation to webform in order to check machine name on create.
  if ($form_id == "webform_add_form") {
    $form['#validate'][] = 'webform_create_validation';

function webform_create_validation(&$form, FormStateInterface $form_state) {
  $current_user = \Drupal::currentUser();
  // If user creating the webform is not an administrator.
  if (!in_array('administrator', $current_user->getRoles())) {
    // Get machine name of webform being created.
    $ent = $form_state->getFormObject()->getEntity();
    if (isset($ent)) {
      $id = $ent->id();
      // Check to see if webform machine name starts with "site_".
      // If it does not, add it and save.
      if (substr($id, 0, 5) !== "site_") {
        $new_id = 'site_' . $id;
        $form_state->setValue('id', $new_id);

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.