0

I am trying to assign a user role at registration using Webforms in Drupal 8. Is this possible or is there another form module that would do it better? I have two user roles 'Venue' and Artist' which will be used throughout the site still using Webforms to enter data specific to user role. New to D8. Thank you.

  • You manually create the user with data submitted trough a webform? – h3rj4n May 28 '17 at 10:44
  • Thank you. I want the user role (either Venue or Artist) to be assigned automatically when the Webforms form has been submitted during registration. Thanks – Al Nicholls May 28 '17 at 11:34
0

Oneway you can do is using hook_form_alter()

function MYMODULE_form_user_register_form_alter(&$form, FormStateInterface $form_state, $form_id) {
     foreach (array_keys($form['actions']) as $action) {
        if ($action != 'preview' && isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] === 'submit') {
          $form['actions'][$action]['#submit'][] = '_add_role_submit';
        }
      }
}
/**
 * Implements submit to set role to user.
 */
function _add_role_submit(array &$form, FormStateInterface $form_state) {
  $values = $form_state->getValues();
// HERE YOU CAN SET ANY ROLE FOR USER.
}

Another way is using hook_user_insert()

use Drupal\user\UserInterface;
function MYMODULE_user_insert(UserInterface $user) {
  // HERE YOU CAN SET ANY ROLE FOR USER.
}

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.