You are here

class EmailRegistrationLogin in Email Registration 8

Provides the email registration login pane.

Plugin annotation


@CommerceCheckoutPane(
  id = "email_registration_login",
  label = @Translation("Login with email registration or continue as guest"),
  default_step = "_disabled",
)

Hierarchy

Expanded class hierarchy of EmailRegistrationLogin

File

src/Plugin/Commerce/CheckoutPane/EmailRegistrationLogin.php, line 27

Namespace

Drupal\email_registration\Plugin\Commerce\CheckoutPane
View source
class EmailRegistrationLogin extends Login {

  /**
   * Constructs a new EmailRegistrationLogin object.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\commerce_checkout\Plugin\Commerce\CheckoutFlow\CheckoutFlowInterface $checkout_flow
   *   The parent checkout flow.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\commerce\CredentialsCheckFloodInterface $credentials_check_flood
   *   The credentials check flood controller.
   * @param \Drupal\Core\Session\AccountInterface $current_user
   *   The current user.
   * @param \Drupal\user\UserAuthInterface $user_auth
   *   The user authentication object.
   * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
   *   The request stack.
   * @param \Drupal\Core\Config\ImmutableConfig $config
   *   The email registration settings.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, CheckoutFlowInterface $checkout_flow, EntityTypeManagerInterface $entity_type_manager, CredentialsCheckFloodInterface $credentials_check_flood, AccountInterface $current_user, UserAuthInterface $user_auth, RequestStack $request_stack, ImmutableConfig $config) {
    parent::__construct($configuration, $plugin_id, $plugin_definition, $checkout_flow, $entity_type_manager, $credentials_check_flood, $current_user, $user_auth, $request_stack);
    $this->config = $config;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, CheckoutFlowInterface $checkout_flow = NULL) {
    return new static($configuration, $plugin_id, $plugin_definition, $checkout_flow, $container
      ->get('entity_type.manager'), $container
      ->get('commerce.credentials_check_flood'), $container
      ->get('current_user'), $container
      ->get('user.auth'), $container
      ->get('request_stack'), $container
      ->get('config.factory')
      ->get('email_registration.settings'));
  }

  /**
   * {@inheritdoc}
   */
  public function buildPaneForm(array $pane_form, FormStateInterface $form_state, array &$complete_form) {
    $pane_form = parent::buildPaneForm($pane_form, $form_state, $complete_form);
    $login_with_username = $this->config
      ->get('login_with_username');
    $pane_form['returning_customer']['name']['#title'] = $login_with_username ? $this
      ->t('Email address or username') : $this
      ->t('Email address');
    $pane_form['returning_customer']['name']['#description'] = $login_with_username ? $this
      ->t('Enter your email address or username.') : $this
      ->t('Enter your email address.');
    $pane_form['returning_customer']['name']['#element_validate'][] = 'email_registration_user_login_validate';
    $pane_form['returning_customer']['name']['#type'] = $login_with_username ? 'textfield' : 'email';
    $pane_form['returning_customer']['name']['#maxlength'] = Email::EMAIL_MAX_LENGTH;
    $pane_form['returning_customer']['password']['#description'] = $this
      ->t('Enter the password that accompanies your email address.');
    $complete_form['#cache']['tags'][] = 'config:email_registration.settings';
    $pane_form['register']['name']['#type'] = 'value';
    $pane_form['register']['name']['#value'] = 'email_registration_' . user_password();
    $pane_form['register']['mail']['#title'] = $this
      ->t('Email');
    return $pane_form;
  }

  /**
   * {@inheritdoc}
   */
  public function validatePaneForm(array &$pane_form, FormStateInterface $form_state, array &$complete_form) {
    $subformId = $pane_form['#parents'][0];
    $values = $form_state
      ->getValue($pane_form['#parents']);
    $triggering_element = $form_state
      ->getTriggeringElement();
    if ($triggering_element['#op'] === 'login') {
      $mail = $values['returning_customer']['name'];
      if (!empty($mail)) {

        // Try to load the user by mail.
        $user = user_load_by_mail($mail);
      }
      if (empty($user)) {

        // Check if users are allowed to login with username as well.
        if (!$this->config
          ->get('login_with_username')) {

          // Users are not allowed to login with username. Since no user was
          // found with the specified mail address, fail with an error and
          // bail out.
          $user_input = $form_state
            ->getUserInput();
          $query = isset($user_input[$subformId]['returning_customer']['name']) ? [
            'name' => $user_input[$subformId]['returning_customer']['name'],
          ] : [];
          $form_state
            ->setError($pane_form['returning_customer'], $this
            ->t('Unrecognized email address or password. <a href=":password">Forgot your password?</a>', [
            ':password' => Url::fromRoute('user.pass', [], [
              'query' => $query,
            ])
              ->toString(),
          ]));
          return;
        }
      }
      else {

        // We have found an user! Save username on the form state, as that is
        // what the parent class expects in their submit handler.
        $username = $user
          ->getAccountName();
        $form_state
          ->setValue([
          $subformId,
          'returning_customer',
          'name',
        ], $username);

        // Perform several checks for this user account
        // Copied from parent to override error messages.
        $name_element = $pane_form['returning_customer']['name'];
        $password = trim($values['returning_customer']['password']);

        // Generate the "reset password" url.
        $query = !empty($username) ? [
          'name' => $username,
        ] : [];
        $password_url = Url::fromRoute('user.pass', [], [
          'query' => $query,
        ])
          ->toString();
        if (user_is_blocked($username)) {
          $form_state
            ->setError($name_element, $this
            ->t('The account with email address %mail has not been activated or is blocked.', [
            '%mail' => $mail,
          ]));
          return;
        }
        $uid = $this->userAuth
          ->authenticate($username, $password);
        if (!$uid) {
          $this->credentialsCheckFlood
            ->register($this->clientIp, $username);

          // Changing the wrong credentials error message.
          if (!$this->config
            ->get('login_with_username')) {
            $form_state
              ->setError($name_element, $this
              ->t('Unrecognized email address or password. <a href=":password">Forgot your password?</a>', [
              ':password' => $password_url,
            ]));

            // Adding return to avoid the parent error when password is empty.
            return;
          }
          else {
            $form_state
              ->setError($name_element, $this
              ->t('Unrecognized username, email, or password. <a href=":url">Have you forgotten your password?</a>', [
              ':url' => $password_url,
            ]));
          }
        }
      }
    }
    parent::validatePaneForm($pane_form, $form_state, $complete_form);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CheckoutPaneBase::$checkoutFlow protected property The parent checkout flow.
CheckoutPaneBase::$entityTypeManager protected property The entity type manager.
CheckoutPaneBase::$order protected property The current order.
CheckoutPaneBase::buildPaneSummary public function Builds a summary of the pane values. Overrides CheckoutPaneInterface::buildPaneSummary 3
CheckoutPaneBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies
CheckoutPaneBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
CheckoutPaneBase::getDisplayLabel public function Gets the pane display label. Overrides CheckoutPaneInterface::getDisplayLabel
CheckoutPaneBase::getId public function Gets the pane ID. Overrides CheckoutPaneInterface::getId
CheckoutPaneBase::getLabel public function Gets the pane label. Overrides CheckoutPaneInterface::getLabel
CheckoutPaneBase::getStepId public function Gets the pane step ID. Overrides CheckoutPaneInterface::getStepId
CheckoutPaneBase::getWeight public function Gets the pane weight. Overrides CheckoutPaneInterface::getWeight
CheckoutPaneBase::getWrapperElement public function Gets the pane wrapper element. Overrides CheckoutPaneInterface::getWrapperElement
CheckoutPaneBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
CheckoutPaneBase::setOrder public function Sets the current order. Overrides CheckoutPaneInterface::setOrder
CheckoutPaneBase::setStepId public function Sets the pane step ID. Overrides CheckoutPaneInterface::setStepId
CheckoutPaneBase::setWeight public function Sets the pane weight. Overrides CheckoutPaneInterface::setWeight
CheckoutPaneBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
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
EmailRegistrationLogin::buildPaneForm public function Builds the pane form. Overrides Login::buildPaneForm
EmailRegistrationLogin::create public static function Creates an instance of the plugin. Overrides Login::create
EmailRegistrationLogin::validatePaneForm public function Validates the pane form. Overrides Login::validatePaneForm
EmailRegistrationLogin::__construct public function Constructs a new EmailRegistrationLogin object. Overrides Login::__construct
Login::$clientIp protected property The client IP address.
Login::$credentialsCheckFlood protected property The credentials check flood controller.
Login::$currentUser protected property The current user.
Login::$languageManager protected property The language manager.
Login::$userAuth protected property The user authentication object.
Login::buildConfigurationForm public function Form constructor. Overrides CheckoutPaneBase::buildConfigurationForm
Login::buildConfigurationSummary public function Builds a summary of the pane configuration. Overrides CheckoutPaneBase::buildConfigurationSummary
Login::canRegisterAfterCheckout protected function Checks whether guests can register after checkout is complete.
Login::defaultConfiguration public function Gets default configuration for this plugin. Overrides CheckoutPaneBase::defaultConfiguration
Login::isVisible public function Determines whether the pane is visible. Overrides CheckoutPaneBase::isVisible
Login::submitConfigurationForm public function Form submission handler. Overrides CheckoutPaneBase::submitConfigurationForm
Login::submitPaneForm public function Handles the submission of an pane form. Overrides CheckoutPaneBase::submitPaneForm
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
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.