You are here

public function AccountForm::buildEntity in Zircon Profile 8

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

Builds an updated entity object based upon the submitted form values.

For building the updated entity object the form's entity is cloned and the submitted form values are copied to entity properties. The form's entity remains unchanged.

Parameters

array $form: A nested array form elements comprising the form.

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

Return value

\Drupal\Core\Entity\EntityInterface An updated copy of the form's entity object.

Overrides ContentEntityForm::buildEntity

See also

\Drupal\Core\Entity\EntityFormInterface::getEntity()

File

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

Class

AccountForm
Form controller for the user account forms.

Namespace

Drupal\user

Code

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