You are here

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

Expanded class hierarchy of CustomerInfoPane

File

uc_cart/src/Plugin/Ubercart/CheckoutPane/CustomerInfoPane.php, line 20

Namespace

Drupal\uc_cart\Plugin\Ubercart\CheckoutPane
View 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

Namesort descending Modifiers Type Description Overrides
CheckoutPanePluginBase::$status protected property Whether the pane is enabled or not.
CheckoutPanePluginBase::$weight protected property The weight of the checkout pane.
CheckoutPanePluginBase::defaultConfiguration public function Gets default configuration for this plugin. Overrides ConfigurableInterface::defaultConfiguration 3
CheckoutPanePluginBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
CheckoutPanePluginBase::getTitle public function Returns the title of the pane, to be displayed on the checkout form. Overrides CheckoutPanePluginInterface::getTitle
CheckoutPanePluginBase::getWeight public function Returns the weight of the checkout pane. Overrides CheckoutPanePluginInterface::getWeight
CheckoutPanePluginBase::isEnabled public function Returns whether the checkout pane is enabled. Overrides CheckoutPanePluginInterface::isEnabled
CheckoutPanePluginBase::prepare public function Prepares a pane for display. Overrides CheckoutPanePluginInterface::prepare 1
CheckoutPanePluginBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
CheckoutPanePluginBase::settingsForm public function Returns the settings form for a checkout pane. Overrides CheckoutPanePluginInterface::settingsForm 3
CheckoutPanePluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct 1
CustomerInfoPane::process public function Processes a checkout pane. Overrides CheckoutPanePluginBase::process
CustomerInfoPane::review public function Returns the review contents of a checkout pane. Overrides CheckoutPanePluginInterface::review
CustomerInfoPane::view public function Returns the contents of a checkout pane. Overrides CheckoutPanePluginInterface::view
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
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.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
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.