Score:1

How can I enforce a default machine name prefix?

cn flag

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?

Score:1
cn flag

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);
      }
    }
  }
}
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.