You are here

abstract class AccountForm in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/user/src/AccountForm.php \Drupal\user\AccountForm

Form controller for the user account forms.

Hierarchy

Expanded class hierarchy of AccountForm

File

core/modules/user/src/AccountForm.php, line 26
Contains \Drupal\user\AccountForm.

Namespace

Drupal\user
View source
abstract class AccountForm extends ContentEntityForm {

  /**
   * The language manager.
   *
   * @var \Drupal\Core\Language\LanguageManagerInterface
   */
  protected $languageManager;

  /**
   * The entity query factory service.
   *
   * @var \Drupal\Core\Entity\Query\QueryFactory
   */
  protected $entityQuery;

  /**
   * Constructs a new EntityForm object.
   *
   * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
   *   The entity manager.
   * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
   *   The language manager.
   * @param \Drupal\Core\Entity\Query\QueryFactory $entity_query
   *   The entity query factory.
   */
  public function __construct(EntityManagerInterface $entity_manager, LanguageManagerInterface $language_manager, QueryFactory $entity_query) {
    parent::__construct($entity_manager);
    $this->languageManager = $language_manager;
    $this->entityQuery = $entity_query;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('entity.manager'), $container
      ->get('language_manager'), $container
      ->get('entity.query'));
  }

  /**
   * {@inheritdoc}
   */
  public function form(array $form, FormStateInterface $form_state) {

    /** @var \Drupal\user\UserInterface $account */
    $account = $this->entity;
    $user = $this
      ->currentUser();
    $config = \Drupal::config('user.settings');
    $form['#cache']['tags'] = $config
      ->getCacheTags();
    $language_interface = \Drupal::languageManager()
      ->getCurrentLanguage();
    $register = $account
      ->isAnonymous();
    $admin = $user
      ->hasPermission('administer users');

    // Account information.
    $form['account'] = array(
      '#type' => 'container',
      '#weight' => -10,
    );

    // The mail field is NOT required if account originally had no mail set
    // and the user performing the edit has 'administer users' permission.
    // This allows users without email address to be edited and deleted.
    // Also see \Drupal\user\Plugin\Validation\Constraint\UserMailRequired.
    $form['account']['mail'] = array(
      '#type' => 'email',
      '#title' => $this
        ->t('Email address'),
      '#description' => $this
        ->t('A valid email address. All emails from the system will be sent to this address. The email address is not made public and will only be used if you wish to receive a new password or wish to receive certain news or notifications by email.'),
      '#required' => !(!$account
        ->getEmail() && $user
        ->hasPermission('administer users')),
      '#default_value' => !$register ? $account
        ->getEmail() : '',
    );

    // Only show name field on registration form or user can change own username.
    $form['account']['name'] = array(
      '#type' => 'textfield',
      '#title' => $this
        ->t('Username'),
      '#maxlength' => USERNAME_MAX_LENGTH,
      '#description' => $this
        ->t("Several special characters are allowed, including space, period (.), hyphen (-), apostrophe ('), underscore (_), and the @ sign."),
      '#required' => TRUE,
      '#attributes' => array(
        'class' => array(
          'username',
        ),
        'autocorrect' => 'off',
        'autocapitalize' => 'off',
        'spellcheck' => 'false',
      ),
      '#default_value' => !$register ? $account
        ->getUsername() : '',
      '#access' => $register || $user
        ->id() == $account
        ->id() && $user
        ->hasPermission('change own username') || $admin,
    );

    // Display password field only for existing users or when user is allowed to
    // assign a password during registration.
    if (!$register) {
      $form['account']['pass'] = array(
        '#type' => 'password_confirm',
        '#size' => 25,
        '#description' => $this
          ->t('To change the current user password, enter the new password in both fields.'),
      );

      // To skip the current password field, the user must have logged in via a
      // one-time link and have the token in the URL. Store this in $form_state
      // so it persists even on subsequent Ajax requests.
      if (!$form_state
        ->get('user_pass_reset')) {
        $user_pass_reset = isset($_SESSION['pass_reset_' . $account
          ->id()]) && Crypt::hashEquals($_SESSION['pass_reset_' . $account
          ->id()], \Drupal::request()->query
          ->get('pass-reset-token'));
        $form_state
          ->set('user_pass_reset', $user_pass_reset);
      }

      // The user must enter their current password to change to a new one.
      if ($user
        ->id() == $account
        ->id()) {
        $form['account']['current_pass'] = array(
          '#type' => 'password',
          '#title' => $this
            ->t('Current password'),
          '#size' => 25,
          '#access' => !$form_state
            ->get('user_pass_reset'),
          '#weight' => -5,
          // Do not let web browsers remember this password, since we are
          // trying to confirm that the person submitting the form actually
          // knows the current one.
          '#attributes' => array(
            'autocomplete' => 'off',
          ),
        );
        $form_state
          ->set('user', $account);

        // The user may only change their own password without their current
        // password if they logged in via a one-time login link.
        if (!$form_state
          ->get('user_pass_reset')) {
          $form['account']['current_pass']['#description'] = $this
            ->t('Required if you want to change the %mail or %pass below. <a href=":request_new_url" title="Send password reset instructions via email.">Reset your password</a>.', array(
            '%mail' => $form['account']['mail']['#title'],
            '%pass' => $this
              ->t('Password'),
            ':request_new_url' => $this
              ->url('user.pass'),
          ));
        }
      }
    }
    elseif (!$config
      ->get('verify_mail') || $admin) {
      $form['account']['pass'] = array(
        '#type' => 'password_confirm',
        '#size' => 25,
        '#description' => $this
          ->t('Provide a password for the new account in both fields.'),
        '#required' => TRUE,
      );
    }

    // When not building the user registration form, prevent web browsers from
    // autofilling/prefilling the email, username, and password fields.
    if ($this
      ->getOperation() != 'register') {
      foreach (array(
        'mail',
        'name',
        'pass',
      ) as $key) {
        if (isset($form['account'][$key])) {
          $form['account'][$key]['#attributes']['autocomplete'] = 'off';
        }
      }
    }
    if ($admin || !$register) {
      $status = $account
        ->get('status')->value;
    }
    else {
      $status = $config
        ->get('register') == USER_REGISTER_VISITORS ? 1 : 0;
    }
    $form['account']['status'] = array(
      '#type' => 'radios',
      '#title' => $this
        ->t('Status'),
      '#default_value' => $status,
      '#options' => array(
        $this
          ->t('Blocked'),
        $this
          ->t('Active'),
      ),
      '#access' => $admin,
    );
    $roles = array_map(array(
      '\\Drupal\\Component\\Utility\\Html',
      'escape',
    ), user_role_names(TRUE));
    $form['account']['roles'] = array(
      '#type' => 'checkboxes',
      '#title' => $this
        ->t('Roles'),
      '#default_value' => !$register ? $account
        ->getRoles() : array(),
      '#options' => $roles,
      '#access' => $roles && $user
        ->hasPermission('administer permissions'),
    );

    // Special handling for the inevitable "Authenticated user" role.
    $form['account']['roles'][RoleInterface::AUTHENTICATED_ID] = array(
      '#default_value' => TRUE,
      '#disabled' => TRUE,
    );
    $form['account']['notify'] = array(
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Notify user of new account'),
      '#access' => $register && $admin,
    );
    $user_preferred_langcode = $register ? $language_interface
      ->getId() : $account
      ->getPreferredLangcode();
    $user_preferred_admin_langcode = $register ? $language_interface
      ->getId() : $account
      ->getPreferredAdminLangcode(FALSE);

    // Is the user preferred language added?
    $user_language_added = FALSE;
    if ($this->languageManager instanceof ConfigurableLanguageManagerInterface) {
      $negotiator = $this->languageManager
        ->getNegotiator();
      $user_language_added = $negotiator && $negotiator
        ->isNegotiationMethodEnabled(LanguageNegotiationUser::METHOD_ID, LanguageInterface::TYPE_INTERFACE);
    }
    $form['language'] = array(
      '#type' => $this->languageManager
        ->isMultilingual() ? 'details' : 'container',
      '#title' => $this
        ->t('Language settings'),
      '#open' => TRUE,
      // Display language selector when either creating a user on the admin
      // interface or editing a user account.
      '#access' => !$register || $user
        ->hasPermission('administer users'),
    );
    $form['language']['preferred_langcode'] = array(
      '#type' => 'language_select',
      '#title' => $this
        ->t('Site language'),
      '#languages' => LanguageInterface::STATE_CONFIGURABLE,
      '#default_value' => $user_preferred_langcode,
      '#description' => $user_language_added ? $this
        ->t("This account's preferred language for emails and site presentation.") : $this
        ->t("This account's preferred language for emails."),
      // This is used to explain that user preferred language and entity
      // language are synchronized. It can be removed if a different behavior is
      // desired.
      '#pre_render' => [
        'user_langcode' => [
          $this,
          'alterPreferredLangcodeDescription',
        ],
      ],
    );

    // Only show the account setting for Administration pages language to users
    // if one of the detection and selection methods uses it.
    $show_admin_language = FALSE;
    if ($account
      ->hasPermission('access administration pages') && $this->languageManager instanceof ConfigurableLanguageManagerInterface) {
      $negotiator = $this->languageManager
        ->getNegotiator();
      $show_admin_language = $negotiator && $negotiator
        ->isNegotiationMethodEnabled(LanguageNegotiationUserAdmin::METHOD_ID);
    }
    $form['language']['preferred_admin_langcode'] = array(
      '#type' => 'language_select',
      '#title' => $this
        ->t('Administration pages language'),
      '#languages' => LanguageInterface::STATE_CONFIGURABLE,
      '#default_value' => $user_preferred_admin_langcode,
      '#access' => $show_admin_language,
      '#empty_option' => $this
        ->t('- No preference -'),
      '#empty_value' => '',
    );

    // User entities contain both a langcode property (for identifying the
    // language of the entity data) and a preferred_langcode property (see
    // above). Rather than provide a UI forcing the user to choose both
    // separately, assume that the user profile data is in the user's preferred
    // language. This entity builder provides that synchronization. For
    // use-cases where this synchronization is not desired, a module can alter
    // or remove this item.
    $form['#entity_builders']['sync_user_langcode'] = [
      $this,
      'syncUserLangcode',
    ];
    return parent::form($form, $form_state, $account);
  }

  /**
   * Alters the preferred language widget description.
   *
   * @param array $element
   *   The preferred language form element.
   *
   * @return array
   *   The preferred language form element.
   */
  public function alterPreferredLangcodeDescription(array $element) {

    // Only add to the description if the form element has a description.
    if (isset($element['#description'])) {
      $element['#description'] .= ' ' . $this
        ->t("This is also assumed to be the primary language of this account's profile information.");
    }
    return $element;
  }

  /**
   * Synchronizes preferred language and entity language.
   *
   * @param string $entity_type_id
   *   The entity type identifier.
   * @param \Drupal\user\UserInterface $user
   *   The entity updated with the submitted values.
   * @param array $form
   *   The complete form array.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   */
  public function syncUserLangcode($entity_type_id, UserInterface $user, array &$form, FormStateInterface &$form_state) {
    $user
      ->getUntranslated()->langcode = $user->preferred_langcode;
  }

  /**
   * {@inheritdoc}
   */
  public function buildEntity(array $form, FormStateInterface $form_state) {

    // Change the roles array to a list of enabled roles.
    // @todo: Alter the form state as the form values are directly extracted and
    //   set on the field, which throws an exception as the list requires
    //   numeric keys. Allow to override this per field. As this function is
    //   called twice, we have to prevent it from getting the array keys twice.
    if (is_string(key($form_state
      ->getValue('roles')))) {
      $form_state
        ->setValue('roles', array_keys(array_filter($form_state
        ->getValue('roles'))));
    }

    /** @var \Drupal\user\UserInterface $account */
    $account = parent::buildEntity($form, $form_state);

    // Translate the empty value '' of language selects to an unset field.
    foreach (array(
      'preferred_langcode',
      'preferred_admin_langcode',
    ) as $field_name) {
      if ($form_state
        ->getValue($field_name) === '') {
        $account->{$field_name} = NULL;
      }
    }

    // Set existing password if set in the form state.
    $current_pass = trim($form_state
      ->getValue('current_pass'));
    if (strlen($current_pass) > 0) {
      $account
        ->setExistingPassword($current_pass);
    }

    // Skip the protected user field constraint if the user came from the
    // password recovery page.
    $account->_skipProtectedUserFieldConstraint = $form_state
      ->get('user_pass_reset');
    return $account;
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditedFieldNames(FormStateInterface $form_state) {
    return array_merge(array(
      'name',
      'pass',
      'mail',
      'timezone',
      'langcode',
      'preferred_langcode',
      'preferred_admin_langcode',
    ), parent::getEditedFieldNames($form_state));
  }

  /**
   * {@inheritdoc}
   */
  protected function flagViolations(EntityConstraintViolationListInterface $violations, array $form, FormStateInterface $form_state) {

    // Manually flag violations of fields not handled by the form display. This
    // is necessary as entity form displays only flag violations for fields
    // contained in the display.
    $field_names = array(
      'name',
      'pass',
      'mail',
      'timezone',
      'langcode',
      'preferred_langcode',
      'preferred_admin_langcode',
    );
    foreach ($violations
      ->getByFields($field_names) as $violation) {
      list($field_name) = explode('.', $violation
        ->getPropertyPath(), 2);
      $form_state
        ->setErrorByName($field_name, $violation
        ->getMessage());
    }
    parent::flagViolations($violations, $form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    parent::submitForm($form, $form_state);
    $user = $this
      ->getEntity($form_state);

    // If there's a session set to the users id, remove the password reset tag
    // since a new password was saved.
    if (isset($_SESSION['pass_reset_' . $user
      ->id()])) {
      unset($_SESSION['pass_reset_' . $user
        ->id()]);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AccountForm::$entityQuery protected property The entity query factory service.
AccountForm::$languageManager protected property The language manager.
AccountForm::alterPreferredLangcodeDescription public function Alters the preferred language widget description.
AccountForm::buildEntity public function Builds an updated entity object based upon the submitted form values. Overrides ContentEntityForm::buildEntity
AccountForm::create public static function Instantiates a new instance of this class. Overrides ContentEntityForm::create
AccountForm::flagViolations protected function Flags violations for the current form. Overrides ContentEntityForm::flagViolations
AccountForm::form public function Gets the actual form array to be built. Overrides ContentEntityForm::form 1
AccountForm::getEditedFieldNames protected function Gets the names of all fields edited in the form. Overrides ContentEntityForm::getEditedFieldNames
AccountForm::submitForm public function This is the default entity object builder function. It is called before any other submit handler to build the new entity object to be used by the following submit handlers. At this point of the form workflow the entity is validated and the form state… Overrides ContentEntityForm::submitForm 1
AccountForm::syncUserLangcode public function Synchronizes preferred language and entity language.
AccountForm::__construct public function Constructs a new EntityForm object. Overrides ContentEntityForm::__construct 2
ContentEntityForm::$entityManager protected property The entity manager. Overrides EntityForm::$entityManager
ContentEntityForm::copyFormValuesToEntity protected function Copies top-level form values to entity properties Overrides EntityForm::copyFormValuesToEntity
ContentEntityForm::getFormDisplay public function Gets the form display. Overrides ContentEntityFormInterface::getFormDisplay
ContentEntityForm::getFormLangcode public function Gets the code identifying the active form language. Overrides ContentEntityFormInterface::getFormLangcode
ContentEntityForm::init protected function Initializes the form state and the entity before the first form build. Overrides EntityForm::init
ContentEntityForm::initFormLangcodes protected function Initializes form language code values.
ContentEntityForm::isDefaultFormLangcode public function Checks whether the current form language matches the entity one. Overrides ContentEntityFormInterface::isDefaultFormLangcode
ContentEntityForm::setFormDisplay public function Sets the form display. Overrides ContentEntityFormInterface::setFormDisplay
ContentEntityForm::updateChangedTime public function Updates the changed time of the entity.
ContentEntityForm::updateFormLangcode public function Updates the form language to reflect any change to the entity language.
ContentEntityForm::validateForm public function Button-level validation handlers are highly discouraged for entity forms, as they will prevent entity validation from running. If the entity is going to be saved during the form submission, this method should be manually invoked from the button-level… Overrides FormBase::validateForm 3
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
EntityForm::$entity protected property The entity being used by this form. 15
EntityForm::$entityTypeManager protected property The entity type manager.
EntityForm::$moduleHandler protected property The module handler service.
EntityForm::$operation protected property The name of the current operation.
EntityForm::actions protected function Returns an array of supported actions for the current entity form. 30
EntityForm::actionsElement protected function Returns the action form element for the current entity form.
EntityForm::afterBuild public function Form element #after_build callback: Updates the entity with submitted data.
EntityForm::buildForm public function Form constructor. Overrides FormInterface::buildForm 7
EntityForm::getBaseFormId public function Returns a string identifying the base form. Overrides BaseFormIdInterface::getBaseFormId 4
EntityForm::getEntity public function Gets the form entity. Overrides EntityFormInterface::getEntity
EntityForm::getEntityFromRouteMatch public function Determines which entity will be used by this form from a RouteMatch object. Overrides EntityFormInterface::getEntityFromRouteMatch 2
EntityForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId 9
EntityForm::getOperation public function Gets the operation identifying the form. Overrides EntityFormInterface::getOperation
EntityForm::prepareEntity protected function Prepares the entity object before the form is built first. 5
EntityForm::prepareInvokeAll protected function Invokes the specified prepare hook variant.
EntityForm::processForm public function Process callback: assigns weights and hides extra fields.
EntityForm::save public function Form submission handler for the 'save' action. Overrides EntityFormInterface::save 36
EntityForm::setEntity public function Sets the form entity. Overrides EntityFormInterface::setEntity
EntityForm::setEntityManager public function Sets the entity manager for this form. Overrides EntityFormInterface::setEntityManager
EntityForm::setEntityTypeManager public function Sets the entity type manager for this form. Overrides EntityFormInterface::setEntityTypeManager
EntityForm::setModuleHandler public function Sets the module handler for this form. Overrides EntityFormInterface::setModuleHandler
EntityForm::setOperation public function Sets the operation for this form. Overrides EntityFormInterface::setOperation
FormBase::$configFactory protected property The config factory. 3
FormBase::$loggerFactory protected property The logger factory.
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 3
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator protected function Returns the link generator.
LinkGeneratorTrait::l protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator public function Sets the link generator service.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service.
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service.
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator protected function Returns the URL generator service.
UrlGeneratorTrait::redirect protected function Returns a redirect response object for the specified route.
UrlGeneratorTrait::setUrlGenerator public function Sets the URL generator service.
UrlGeneratorTrait::url protected function Generates a URL or path for a specific route based on the given parameters.