You are here

public function FormAlter::formAlter in Form mode manager 8.2

Same name in this branch
  1. 8.2 src/FormAlter.php \Drupal\form_mode_manager\FormAlter::formAlter()
  2. 8.2 modules/form_mode_user_roles_assign/src/FormAlter.php \Drupal\form_mode_user_roles_assign\FormAlter::formAlter()

Automatically assign user roles of register/create routes using FMM.

This is an alter hook bridge.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $formState: The current state of the form.

string $formId: The Form ID.

See also

hook_form_alter()

File

modules/form_mode_user_roles_assign/src/FormAlter.php, line 95

Class

FormAlter
Manipulates Form Alter information.

Namespace

Drupal\form_mode_user_roles_assign

Code

public function formAlter(array &$form, FormStateInterface $formState, $formId) {
  if (!$this
    ->candidateToAlterForm()) {
    return;
  }

  /** @var \Symfony\Component\Routing\Route $routeObject */
  $routeObject = $this->routeMatch
    ->getRouteObject();
  $operationName = $routeObject
    ->getDefault('_entity_form');
  $dynamicFormId = str_replace('.', '_', $operationName) . '_form';
  if (!$this
    ->isUserRegistrationRoute()) {
    return;
  }
  $form_mode_name = $this->formModeManager
    ->getFormModeMachineName($operationName);
  $roles = $this->configFactory
    ->get('form_mode_user_roles_assign.settings')
    ->get("form_modes.user_{$form_mode_name}.assign_roles");
  if (empty($roles)) {
    return;
  }
  if ($this
    ->doPopulateUserRoles($formId, $dynamicFormId, $roles)) {
    foreach ($roles as $roles_key => $roles_value) {
      $form['account']['roles']['#default_value'][] = $roles_key;
    }
  }
}