class CustomerInfoPane in Ubercart 8.4
Gets the user's email address for login.
Plugin annotation
@CheckoutPane(
id = "customer",
title = @Translation("Customer information"),
weight = 2,
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\uc_cart\CheckoutPanePluginBase implements CheckoutPanePluginInterface
- class \Drupal\uc_cart\Plugin\Ubercart\CheckoutPane\CustomerInfoPane uses RedirectDestinationTrait
- class \Drupal\uc_cart\CheckoutPanePluginBase implements CheckoutPanePluginInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of CustomerInfoPane
File
- uc_cart/
src/ Plugin/ Ubercart/ CheckoutPane/ CustomerInfoPane.php, line 20
Namespace
Drupal\uc_cart\Plugin\Ubercart\CheckoutPaneView source
class CustomerInfoPane extends CheckoutPanePluginBase {
use RedirectDestinationTrait;
/**
* {@inheritdoc}
*/
public function view(OrderInterface $order, array $form, FormStateInterface $form_state) {
$user = \Drupal::currentUser();
$cart_config = \Drupal::config('uc_cart.settings');
if ($user
->isAuthenticated()) {
$email = $user
->getEmail();
$contents['#description'] = $this
->t('Order information will be sent to your account e-mail listed below.');
$contents['primary_email'] = [
'#type' => 'hidden',
'#value' => $email,
];
$contents['email_text'] = [
'#markup' => '<div>' . $this
->t('<b>E-mail address:</b> @email (<a href=":url">edit</a>)', [
'@email' => $email,
':url' => Url::fromRoute('entity.user.edit_form', [
'user' => $user
->id(),
], [
'query' => $this
->getDestinationArray(),
])
->toString(),
]) . '</div>',
];
}
else {
$email = $order
->getEmail();
$contents['#description'] = $this
->t('Enter a valid email address for this order or <a href=":url">click here</a> to login with an existing account and return to checkout.', [
':url' => Url::fromRoute('user.login', [], [
'query' => $this
->getDestinationArray(),
])
->toString(),
]);
$contents['primary_email'] = [
'#type' => 'email',
'#title' => $this
->t('E-mail address'),
'#default_value' => $email,
'#required' => TRUE,
];
if ($cart_config
->get('email_validation')) {
$contents['primary_email_confirm'] = [
'#type' => 'email',
'#title' => $this
->t('Confirm e-mail address'),
'#default_value' => $email,
'#required' => TRUE,
];
}
$contents['new_account'] = [];
if ($cart_config
->get('new_account_name')) {
$contents['new_account']['name'] = [
'#type' => 'textfield',
'#title' => $this
->t('Username'),
'#default_value' => isset($order->data->new_user_name) ? $order->data->new_user_name : '',
'#maxlength' => 60,
'#size' => 32,
];
}
if ($cart_config
->get('new_account_password')) {
$contents['new_account']['pass'] = [
'#type' => 'password',
'#title' => $this
->t('Password'),
'#maxlength' => 32,
'#size' => 32,
];
$contents['new_account']['pass_confirm'] = [
'#type' => 'password',
'#title' => $this
->t('Confirm password'),
'#description' => $this
->t('Passwords must match to proceed.'),
'#maxlength' => 32,
'#size' => 32,
];
}
if (!empty($contents['new_account'])) {
$contents['new_account'] += [
'#type' => 'details',
'#title' => $this
->t('New account details'),
'#description' => $this
->t('<b>Optional.</b> New customers may supply custom account details.<br />We will create these for you if no values are entered.'),
'#open' => TRUE,
];
}
}
return $contents;
}
/**
* {@inheritdoc}
*/
public function process(OrderInterface $order, array $form, FormStateInterface $form_state) {
if (\Drupal::currentUser()
->isAnonymous()) {
$cart_config = \Drupal::config('uc_cart.settings');
$pane = $form_state
->getValue([
'panes',
'customer',
]);
$order
->setEmail($pane['primary_email']);
// Check if the email address is already taken.
$mail_taken = (bool) \Drupal::entityQuery('user')
->condition('mail', $pane['primary_email'])
->range(0, 1)
->count()
->execute();
if ($cart_config
->get('email_validation') && $pane['primary_email'] !== $pane['primary_email_confirm']) {
$form_state
->setErrorByName('panes][customer][primary_email_confirm', $this
->t('The e-mail address did not match.'));
}
// Invalidate if an account already exists for this e-mail address,
// and the user is not logged into that account.
if (!$cart_config
->get('mail_existing') && !empty($pane['primary_email']) && $mail_taken) {
$form_state
->setErrorByName('panes][customer][primary_email', $this
->t('An account already exists for your e-mail address. You will either need to login with this e-mail address or use a different e-mail address.'));
}
// If new users can specify names or passwords then...
if ($cart_config
->get('new_account_name') || $cart_config
->get('new_account_password')) {
// Skip if an account already exists for this e-mail address.
if ($cart_config
->get('mail_existing') && $mail_taken) {
$this
->messenger()
->addMessage($this
->t('An account already exists for your e-mail address. The new account details you entered will be disregarded.'));
}
else {
// Validate the username.
if ($cart_config
->get('new_account_name') && !empty($pane['new_account']['name'])) {
$message = user_validate_name($pane['new_account']['name']);
$name_taken = (bool) \Drupal::entityQuery('user')
->condition('name', $pane['new_account']['name'])
->range(0, 1)
->count()
->execute();
if (!empty($message)) {
$form_state
->setErrorByName('panes][customer][new_account][name', $message);
}
elseif ($name_taken) {
$form_state
->setErrorByName('panes][customer][new_account][name', $this
->t('The username %name is already taken. Please enter a different name or leave the field blank for your username to be your e-mail address.', [
'%name' => $pane['new_account']['name'],
]));
}
else {
$order->data->new_user_name = $pane['new_account']['name'];
}
}
// Validate the password.
if ($cart_config
->get('new_account_password')) {
if (strcmp($pane['new_account']['pass'], $pane['new_account']['pass_confirm'])) {
$form_state
->setErrorByName('panes][customer][new_account][pass_confirm', $this
->t('The passwords you entered did not match. Please try again.'));
}
if (!empty($pane['new_account']['pass'])) {
$order->data->new_user_hash = \Drupal::service('password')
->hash(trim($pane['new_account']['pass']));
}
}
}
}
}
return TRUE;
}
/**
* {@inheritdoc}
*/
public function review(OrderInterface $order) {
$review[] = [
'title' => $this
->t('E-mail'),
'data' => [
'#plain_text' => $order
->getEmail(),
],
];
return $review;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CheckoutPanePluginBase:: |
protected | property | Whether the pane is enabled or not. | |
CheckoutPanePluginBase:: |
protected | property | The weight of the checkout pane. | |
CheckoutPanePluginBase:: |
public | function |
Gets default configuration for this plugin. Overrides ConfigurableInterface:: |
3 |
CheckoutPanePluginBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
|
CheckoutPanePluginBase:: |
public | function |
Returns the title of the pane, to be displayed on the checkout form. Overrides CheckoutPanePluginInterface:: |
|
CheckoutPanePluginBase:: |
public | function |
Returns the weight of the checkout pane. Overrides CheckoutPanePluginInterface:: |
|
CheckoutPanePluginBase:: |
public | function |
Returns whether the checkout pane is enabled. Overrides CheckoutPanePluginInterface:: |
|
CheckoutPanePluginBase:: |
public | function |
Prepares a pane for display. Overrides CheckoutPanePluginInterface:: |
1 |
CheckoutPanePluginBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: |
|
CheckoutPanePluginBase:: |
public | function |
Returns the settings form for a checkout pane. Overrides CheckoutPanePluginInterface:: |
3 |
CheckoutPanePluginBase:: |
public | function |
Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase:: |
1 |
CustomerInfoPane:: |
public | function |
Processes a checkout pane. Overrides CheckoutPanePluginBase:: |
|
CustomerInfoPane:: |
public | function |
Returns the review contents of a checkout pane. Overrides CheckoutPanePluginInterface:: |
|
CustomerInfoPane:: |
public | function |
Returns the contents of a checkout pane. Overrides CheckoutPanePluginInterface:: |
|
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 | |
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. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
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. |