class Login in Commerce Core 8.2
Provides the login pane.
Plugin annotation
@CommerceCheckoutPane(
id = "login",
label = @Translation("Login or continue as guest"),
default_step = "login",
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\commerce_checkout\Plugin\Commerce\CheckoutPane\CheckoutPaneBase implements CheckoutPaneInterface, ContainerFactoryPluginInterface
- class \Drupal\commerce_checkout\Plugin\Commerce\CheckoutPane\Login implements CheckoutPaneInterface, ContainerFactoryPluginInterface
- class \Drupal\commerce_checkout\Plugin\Commerce\CheckoutPane\CheckoutPaneBase implements CheckoutPaneInterface, ContainerFactoryPluginInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of Login
6 string references to 'Login'
- CheckoutOrderTest::testCacheMetadata in modules/
checkout/ tests/ src/ Functional/ CheckoutOrderTest.php - Tests checkout flow cache metadata.
- CheckoutOrderTest::testCheckout in modules/
checkout/ tests/ src/ Functional/ CheckoutOrderTest.php - Tests anonymous and authenticated checkout.
- CheckoutOrderTest::testRedirectAfterRegistrationOnCheckout in modules/
checkout/ tests/ src/ Functional/ CheckoutOrderTest.php - Tests redirection after registering at the end of checkout.
- CheckoutOrderTest::testRegistrationAfterGuestOrderCheckout in modules/
checkout/ tests/ src/ Functional/ CheckoutOrderTest.php - Tests that you can register after completing guest checkout.
- CheckoutOrderTest::testRegistrationAfterGuestOrderCheckoutWithCustomUserFields in modules/
checkout/ tests/ src/ Functional/ CheckoutOrderTest.php - Tests custom user fields are respected on registration after checkout.
File
- modules/
checkout/ src/ Plugin/ Commerce/ CheckoutPane/ Login.php, line 28
Namespace
Drupal\commerce_checkout\Plugin\Commerce\CheckoutPaneView source
class Login extends CheckoutPaneBase implements CheckoutPaneInterface, ContainerFactoryPluginInterface {
/**
* The credentials check flood controller.
*
* @var \Drupal\commerce\CredentialsCheckFloodInterface
*/
protected $credentialsCheckFlood;
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $currentUser;
/**
* The user authentication object.
*
* @var \Drupal\user\UserAuthInterface
*/
protected $userAuth;
/**
* The client IP address.
*
* @var string
*/
protected $clientIp;
/**
* The language manager.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;
/**
* Constructs a new Login 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\Language\LanguageManagerInterface $language_manager
* The language manager.
*/
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, LanguageManagerInterface $language_manager = NULL) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $checkout_flow, $entity_type_manager);
$this->credentialsCheckFlood = $credentials_check_flood;
$this->currentUser = $current_user;
$this->userAuth = $user_auth;
$this->clientIp = $request_stack
->getCurrentRequest()
->getClientIp();
if (!$language_manager) {
@trigger_error('Calling ' . __METHOD__ . '() without the $language_manager argument is deprecated in commerce:8.x-2.25 and is removed from commerce:3.x.');
$language_manager = \Drupal::languageManager();
}
$this->languageManager = $language_manager;
}
/**
* {@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('language_manager'));
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'allow_guest_checkout' => TRUE,
'allow_registration' => FALSE,
] + parent::defaultConfiguration();
}
/**
* {@inheritdoc}
*/
public function buildConfigurationSummary() {
if (!empty($this->configuration['allow_guest_checkout'])) {
$summary = $this
->t('Guest checkout: Allowed') . '<br>';
}
else {
$summary = $this
->t('Guest checkout: Not allowed') . '<br>';
}
if (!empty($this->configuration['allow_registration'])) {
$summary .= $this
->t('Registration: Allowed');
}
else {
$summary .= $this
->t('Registration: Not allowed');
}
return $summary;
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$form['allow_guest_checkout'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Allow guest checkout'),
'#default_value' => $this->configuration['allow_guest_checkout'],
];
$form['allow_registration'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Allow registration'),
'#default_value' => $this->configuration['allow_registration'],
];
return $form;
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::submitConfigurationForm($form, $form_state);
if (!$form_state
->getErrors()) {
$values = $form_state
->getValue($form['#parents']);
$this->configuration['allow_guest_checkout'] = !empty($values['allow_guest_checkout']);
$this->configuration['allow_registration'] = !empty($values['allow_registration']);
}
}
/**
* {@inheritdoc}
*/
public function isVisible() {
return $this->currentUser
->isAnonymous();
}
/**
* {@inheritdoc}
*/
public function buildPaneForm(array $pane_form, FormStateInterface $form_state, array &$complete_form) {
$pane_form['#attached']['library'][] = 'commerce_checkout/login_pane';
$pane_form['returning_customer'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Returning Customer'),
'#attributes' => [
'class' => [
'form-wrapper__login-option',
'form-wrapper__returning-customer',
],
],
];
$pane_form['returning_customer']['name'] = [
'#type' => 'textfield',
'#title' => $this
->t('Username'),
'#size' => 60,
'#maxlength' => UserInterface::USERNAME_MAX_LENGTH,
'#attributes' => [
'autocorrect' => 'none',
'autocapitalize' => 'none',
'spellcheck' => 'false',
'autofocus' => 'autofocus',
],
];
$pane_form['returning_customer']['password'] = [
'#type' => 'password',
'#title' => $this
->t('Password'),
'#size' => 60,
];
$pane_form['returning_customer']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Log in'),
'#op' => 'login',
'#attributes' => [
'formnovalidate' => 'formnovalidate',
],
'#limit_validation_errors' => [
array_merge($pane_form['#parents'], [
'returning_customer',
]),
],
'#submit' => [],
];
$pane_form['returning_customer']['forgot_password'] = [
'#type' => 'link',
'#title' => $this
->t('Forgot password?'),
'#url' => Url::fromRoute('user.pass'),
];
$pane_form['guest'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Guest Checkout'),
'#access' => $this->configuration['allow_guest_checkout'],
'#attributes' => [
'class' => [
'form-wrapper__login-option',
'form-wrapper__guest-checkout',
],
],
];
$pane_form['guest']['text'] = [
'#prefix' => '<p>',
'#suffix' => '</p>',
'#markup' => $this
->t('Proceed to checkout. You can optionally create an account at the end.'),
'#access' => $this
->canRegisterAfterCheckout(),
];
$pane_form['guest']['continue'] = [
'#type' => 'submit',
'#value' => $this
->t('Continue as Guest'),
'#op' => 'continue',
'#attributes' => [
'formnovalidate' => 'formnovalidate',
],
'#limit_validation_errors' => [],
'#submit' => [],
];
$pane_form['register'] = [
'#parents' => array_merge($pane_form['#parents'], [
'register',
]),
'#type' => 'fieldset',
'#title' => $this
->t('New Customer'),
'#access' => $this->configuration['allow_registration'],
'#attributes' => [
'class' => [
'form-wrapper__login-option',
'form-wrapper__guest-checkout',
],
],
];
$pane_form['register']['mail'] = [
'#type' => 'email',
'#title' => $this
->t('Email address'),
'#required' => FALSE,
];
$pane_form['register']['name'] = [
'#type' => 'textfield',
'#title' => $this
->t('Username'),
'#maxlength' => UserInterface::USERNAME_MAX_LENGTH,
'#description' => $this
->t("Several special characters are allowed, including space, period (.), hyphen (-), apostrophe ('), underscore (_), and the @ sign."),
'#required' => FALSE,
'#attributes' => [
'class' => [
'username',
],
'autocorrect' => 'off',
'autocapitalize' => 'off',
'spellcheck' => 'false',
],
'#default_value' => '',
];
$pane_form['register']['password'] = [
'#type' => 'password_confirm',
'#size' => 60,
'#description' => $this
->t('Provide a password for the new account in both fields.'),
'#required' => FALSE,
];
$pane_form['register']['register'] = [
'#type' => 'submit',
'#value' => $this
->t('Create account and continue'),
'#op' => 'register',
'#weight' => 50,
];
/** @var \Drupal\user\UserInterface $account */
$account = $this->entityTypeManager
->getStorage('user')
->create([]);
$form_display = EntityFormDisplay::collectRenderDisplay($account, 'register');
$form_display
->buildForm($account, $pane_form['register'], $form_state);
return $pane_form;
}
/**
* {@inheritdoc}
*/
public function validatePaneForm(array &$pane_form, FormStateInterface $form_state, array &$complete_form) {
$values = $form_state
->getValue($pane_form['#parents']);
$triggering_element = $form_state
->getTriggeringElement();
$trigger = !empty($triggering_element['#op']) ? $triggering_element['#op'] : 'continue';
switch ($trigger) {
case 'continue':
return;
case 'login':
$name_element = $pane_form['returning_customer']['name'];
$username = $values['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 (empty($username) || empty($password)) {
$form_state
->setError($pane_form['returning_customer'], $this
->t('Unrecognized username or password. <a href=":url">Have you forgotten your password?</a>', [
':url' => $password_url,
]));
return;
}
if (user_is_blocked($username)) {
$form_state
->setError($name_element, $this
->t('The username %name has not been activated or is blocked.', [
'%name' => $username,
]));
return;
}
if (!$this->credentialsCheckFlood
->isAllowedHost($this->clientIp)) {
$form_state
->setError($name_element, $this
->t('Too many failed login attempts from your IP address. This IP address is temporarily blocked. Try again later or <a href=":url">request a new password</a>.', [
':url' => $password_url,
]));
$this->credentialsCheckFlood
->register($this->clientIp, $username);
return;
}
elseif (!$this->credentialsCheckFlood
->isAllowedAccount($this->clientIp, $username)) {
$form_state
->setError($name_element, $this
->t('Too many failed login attempts for this account. It is temporarily blocked. Try again later or <a href=":url">request a new password</a>.', [
':url' => $password_url,
]));
$this->credentialsCheckFlood
->register($this->clientIp, $username);
return;
}
$uid = $this->userAuth
->authenticate($username, $password);
if (!$uid) {
$this->credentialsCheckFlood
->register($this->clientIp, $username);
$form_state
->setError($name_element, $this
->t('Unrecognized username or password. <a href=":url">Have you forgotten your password?</a>', [
':url' => $password_url,
]));
}
$form_state
->set('logged_in_uid', $uid);
break;
case 'register':
$email = $values['register']['mail'];
$username = $values['register']['name'];
$password = trim($values['register']['password']);
if (empty($email)) {
$form_state
->setError($pane_form['register']['mail'], $this
->t('Email field is required.'));
return;
}
if (empty($username)) {
$form_state
->setError($pane_form['register']['name'], $this
->t('Username field is required.'));
return;
}
if (empty($password)) {
$form_state
->setError($pane_form['register']['password'], $this
->t('Password field is required.'));
return;
}
/** @var \Drupal\user\UserInterface $account */
$account = $this->entityTypeManager
->getStorage('user')
->create([
'mail' => $email,
'name' => $username,
'pass' => $password,
'status' => TRUE,
'langcode' => $this->languageManager
->getCurrentLanguage()
->getId(),
'preferred_langcode' => $this->languageManager
->getCurrentLanguage()
->getId(),
'preferred_admin_langcode' => $this->languageManager
->getCurrentLanguage()
->getId(),
]);
$form_display = EntityFormDisplay::collectRenderDisplay($account, 'register');
$form_display
->extractFormValues($account, $pane_form['register'], $form_state);
$form_display
->validateFormValues($account, $pane_form['register'], $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.
// @see \Drupal\user\AccountForm::flagViolations
$violations = $account
->validate();
foreach ($violations
->getByFields([
'name',
'pass',
'mail',
]) as $violation) {
list($field_name) = explode('.', $violation
->getPropertyPath(), 2);
$form_state
->setError($pane_form['register'][$field_name], $violation
->getMessage());
}
if (!$form_state
->hasAnyErrors()) {
$account
->save();
$form_state
->set('logged_in_uid', $account
->id());
}
break;
}
}
/**
* {@inheritdoc}
*/
public function submitPaneForm(array &$pane_form, FormStateInterface $form_state, array &$complete_form) {
$triggering_element = $form_state
->getTriggeringElement();
$trigger = !empty($triggering_element['#op']) ? $triggering_element['#op'] : 'continue';
switch ($trigger) {
case 'continue':
break;
case 'login':
case 'register':
$storage = $this->entityTypeManager
->getStorage('user');
/** @var \Drupal\user\UserInterface $account */
$account = $storage
->load($form_state
->get('logged_in_uid'));
user_login_finalize($account);
$this->order
->setCustomer($account);
$this->credentialsCheckFlood
->clearAccount($this->clientIp, $account
->getAccountName());
break;
}
$form_state
->setRedirect('commerce_checkout.form', [
'commerce_order' => $this->order
->id(),
'step' => $this->checkoutFlow
->getNextStepId($this
->getStepId()),
]);
}
/**
* Checks whether guests can register after checkout is complete.
*
* @return bool
* TRUE if guests can register after checkout is complete, FALSE otherwise.
*/
protected function canRegisterAfterCheckout() {
$completion_register_pane = $this->checkoutFlow
->getPane('completion_register');
return $completion_register_pane
->getStepId() != '_disabled';
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CheckoutPaneBase:: |
protected | property | The parent checkout flow. | |
CheckoutPaneBase:: |
protected | property | The entity type manager. | |
CheckoutPaneBase:: |
protected | property | The current order. | |
CheckoutPaneBase:: |
public | function |
Builds a summary of the pane values. Overrides CheckoutPaneInterface:: |
3 |
CheckoutPaneBase:: |
public | function |
Calculates dependencies for the configured plugin. Overrides DependentPluginInterface:: |
|
CheckoutPaneBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
|
CheckoutPaneBase:: |
public | function |
Gets the pane display label. Overrides CheckoutPaneInterface:: |
|
CheckoutPaneBase:: |
public | function |
Gets the pane ID. Overrides CheckoutPaneInterface:: |
|
CheckoutPaneBase:: |
public | function |
Gets the pane label. Overrides CheckoutPaneInterface:: |
|
CheckoutPaneBase:: |
public | function |
Gets the pane step ID. Overrides CheckoutPaneInterface:: |
|
CheckoutPaneBase:: |
public | function |
Gets the pane weight. Overrides CheckoutPaneInterface:: |
|
CheckoutPaneBase:: |
public | function |
Gets the pane wrapper element. Overrides CheckoutPaneInterface:: |
|
CheckoutPaneBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: |
|
CheckoutPaneBase:: |
public | function |
Sets the current order. Overrides CheckoutPaneInterface:: |
|
CheckoutPaneBase:: |
public | function |
Sets the pane step ID. Overrides CheckoutPaneInterface:: |
|
CheckoutPaneBase:: |
public | function |
Sets the pane weight. Overrides CheckoutPaneInterface:: |
|
CheckoutPaneBase:: |
public | function |
Form validation handler. Overrides PluginFormInterface:: |
|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
Login:: |
protected | property | The client IP address. | |
Login:: |
protected | property | The credentials check flood controller. | |
Login:: |
protected | property | The current user. | |
Login:: |
protected | property | The language manager. | |
Login:: |
protected | property | The user authentication object. | |
Login:: |
public | function |
Form constructor. Overrides CheckoutPaneBase:: |
|
Login:: |
public | function |
Builds a summary of the pane configuration. Overrides CheckoutPaneBase:: |
|
Login:: |
public | function |
Builds the pane form. Overrides CheckoutPaneInterface:: |
|
Login:: |
protected | function | Checks whether guests can register after checkout is complete. | |
Login:: |
public static | function |
Creates an instance of the plugin. Overrides CheckoutPaneBase:: |
|
Login:: |
public | function |
Gets default configuration for this plugin. Overrides CheckoutPaneBase:: |
|
Login:: |
public | function |
Determines whether the pane is visible. Overrides CheckoutPaneBase:: |
|
Login:: |
public | function |
Form submission handler. Overrides CheckoutPaneBase:: |
|
Login:: |
public | function |
Handles the submission of an pane form. Overrides CheckoutPaneBase:: |
|
Login:: |
public | function |
Validates the pane form. Overrides CheckoutPaneBase:: |
|
Login:: |
public | function |
Constructs a new Login object. Overrides CheckoutPaneBase:: |
|
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |