View source
<?php
namespace Drupal\commerce_checkout\Plugin\Commerce\CheckoutPane;
use Drupal\commerce\CredentialsCheckFloodInterface;
use Drupal\commerce_checkout\Plugin\Commerce\CheckoutFlow\CheckoutFlowInterface;
use Drupal\commerce_checkout\Event\CheckoutCompletionRegisterEvent;
use Drupal\commerce_checkout\Event\CheckoutEvents;
use Drupal\commerce_order\OrderAssignmentInterface;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\user\UserAuthInterface;
use Drupal\user\UserInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class CompletionRegister extends CheckoutPaneBase implements CheckoutPaneInterface, ContainerFactoryPluginInterface {
protected $credentialsCheckFlood;
protected $currentUser;
protected $userAuth;
protected $clientIp;
protected $eventDispatcher;
protected $orderAssignment;
protected $userStorage;
public function __construct(array $configuration, $plugin_id, $plugin_definition, CheckoutFlowInterface $checkout_flow, CredentialsCheckFloodInterface $credentials_check_flood, AccountInterface $current_user, EntityTypeManagerInterface $entity_type_manager, EventDispatcherInterface $event_dispatcher, OrderAssignmentInterface $order_assignment, RequestStack $request_stack, UserAuthInterface $user_auth) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $checkout_flow, $entity_type_manager);
$this->credentialsCheckFlood = $credentials_check_flood;
$this->currentUser = $current_user;
$this->clientIp = $request_stack
->getCurrentRequest()
->getClientIp();
$this->eventDispatcher = $event_dispatcher;
$this->orderAssignment = $order_assignment;
$this->userAuth = $user_auth;
$this->userStorage = $entity_type_manager
->getStorage('user');
}
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('commerce.credentials_check_flood'), $container
->get('current_user'), $container
->get('entity_type.manager'), $container
->get('event_dispatcher'), $container
->get('commerce_order.order_assignment'), $container
->get('request_stack'), $container
->get('user.auth'));
}
public function isVisible() {
if ($this->order
->getState()->value == 'draft') {
return FALSE;
}
if ($this->currentUser
->isAuthenticated()) {
return FALSE;
}
$mail = $this->order
->getEmail();
if (!$mail) {
return FALSE;
}
$existing_user = $this->userStorage
->loadByProperties([
'mail' => $mail,
]);
if ($existing_user) {
return FALSE;
}
return TRUE;
}
public function buildPaneForm(array $pane_form, FormStateInterface $form_state, array &$complete_form) {
$pane_form['#theme'] = 'commerce_checkout_completion_register';
$pane_form['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['pass'] = [
'#type' => 'password_confirm',
'#size' => 60,
'#description' => $this
->t('Provide a password for the new account.'),
'#required' => TRUE,
];
$pane_form['actions'] = [
'#type' => 'actions',
];
$pane_form['actions']['register'] = [
'#type' => 'submit',
'#value' => $this
->t('Create account'),
'#name' => 'checkout_completion_register',
];
$account = $this->entityTypeManager
->getStorage('user')
->create([]);
$form_display = EntityFormDisplay::collectRenderDisplay($account, 'register');
$form_display
->buildForm($account, $pane_form, $form_state);
return $pane_form;
}
public function validatePaneForm(array &$pane_form, FormStateInterface $form_state, array &$complete_form) {
$values = $form_state
->getValue($pane_form['#parents']);
$account = $this->userStorage
->create([
'mail' => $this->order
->getEmail(),
'name' => $values['name'],
'pass' => $values['pass'],
'status' => TRUE,
]);
$form_display = EntityFormDisplay::collectRenderDisplay($account, 'register');
$form_display
->extractFormValues($account, $pane_form, $form_state);
$form_display
->validateFormValues($account, $pane_form, $form_state);
$violations = $account
->validate();
foreach ($violations
->getByFields([
'name',
'pass',
]) as $violation) {
list($field_name) = explode('.', $violation
->getPropertyPath(), 2);
$form_state
->setError($pane_form[$field_name], $violation
->getMessage());
}
}
public function submitPaneForm(array &$pane_form, FormStateInterface $form_state, array &$complete_form) {
$values = $form_state
->getValue($pane_form['#parents']);
$account = $this->userStorage
->create([
'pass' => $values['pass'],
'mail' => $this->order
->getEmail(),
'name' => $values['name'],
'status' => TRUE,
]);
$form_display = EntityFormDisplay::collectRenderDisplay($account, 'register');
$form_display
->extractFormValues($account, $pane_form, $form_state);
$account
->save();
user_login_finalize($account);
$this->credentialsCheckFlood
->clearAccount($this->clientIp, $account
->getAccountName());
$this->orderAssignment
->assign($this->order, $account);
$event = new CheckoutCompletionRegisterEvent($account, $this->order);
$this->eventDispatcher
->dispatch(CheckoutEvents::COMPLETION_REGISTER, $event);
if ($url = $event
->getRedirectUrl()) {
$form_state
->setRedirectUrl($url);
}
$this
->messenger()
->addStatus($this
->t('Registration successful. You are now logged in.'));
}
}