You are here

class OrderReassignForm in Commerce Core 8.2

Provides a form for assigning orders to a different customer.

Hierarchy

Expanded class hierarchy of OrderReassignForm

1 string reference to 'OrderReassignForm'
commerce_order.routing.yml in modules/order/commerce_order.routing.yml
modules/order/commerce_order.routing.yml

File

modules/order/src/Form/OrderReassignForm.php, line 15

Namespace

Drupal\commerce_order\Form
View source
class OrderReassignForm extends FormBase {
  use CustomerFormTrait;

  /**
   * The current order.
   *
   * @var \Drupal\commerce_order\Entity\OrderInterface
   */
  protected $order;

  /**
   * The order assignment service.
   *
   * @var \Drupal\commerce_order\OrderAssignmentInterface
   */
  protected $orderAssignment;

  /**
   * The user storage.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $userStorage;

  /**
   * Constructs a new OrderReassignForm object.
   *
   * @param \Drupal\Core\Routing\CurrentRouteMatch $current_route_match
   *   The current route match.
   * @param \Drupal\commerce_order\OrderAssignmentInterface $order_assignment
   *   The order assignment service.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   */
  public function __construct(CurrentRouteMatch $current_route_match, OrderAssignmentInterface $order_assignment, EntityTypeManagerInterface $entity_type_manager) {
    $this->order = $current_route_match
      ->getParameter('commerce_order');
    $this->orderAssignment = $order_assignment;
    $this->userStorage = $entity_type_manager
      ->getStorage('user');
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('current_route_match'), $container
      ->get('commerce_order.order_assignment'), $container
      ->get('entity_type.manager'));
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'commerce_order_reassign_form';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $customer = $this->order
      ->getCustomer();
    if ($customer
      ->isAnonymous()) {
      $current_customer = $this
        ->t('anonymous user with the email %email', [
        '%email' => $this->order
          ->getEmail(),
      ]);
    }
    else {

      // If the display name has been altered to not be the email address,
      // show the email as well.
      if ($customer
        ->getDisplayName() != $customer
        ->getEmail()) {
        $customer_link_text = $this
          ->t('@display (@email)', [
          '@display' => $customer
            ->getDisplayName(),
          '@email' => $customer
            ->getEmail(),
        ]);
      }
      else {
        $customer_link_text = $customer
          ->getDisplayName();
      }
      $current_customer = $this->order
        ->getCustomer()
        ->toLink($customer_link_text)
        ->toString();
    }
    $form['current_customer'] = [
      '#type' => 'item',
      '#markup' => $this
        ->t('The order is currently assigned to @customer.', [
        '@customer' => $current_customer,
      ]),
    ];
    $form += $this
      ->buildCustomerForm($form, $form_state, $this->order);
    $form['actions']['#type'] = 'actions';
    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Reassign order'),
      '#button_type' => 'primary',
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this
      ->submitCustomerForm($form, $form_state);
    $values = $form_state
      ->getValues();

    /** @var \Drupal\user\UserInterface $user */
    $user = $this->userStorage
      ->load($values['uid']);
    $this->orderAssignment
      ->assign($this->order, $user);
    $this
      ->messenger()
      ->addMessage($this
      ->t('The order %label has been assigned to customer %customer.', [
      '%label' => $this->order
        ->label(),
      '%customer' => $this->order
        ->getCustomer()
        ->label(),
    ]));
    $form_state
      ->setRedirectUrl($this->order
      ->toUrl('collection'));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CustomerFormTrait::buildCustomerForm public function Builds the customer form.
CustomerFormTrait::customerFormAjax public function Ajax callback.
CustomerFormTrait::submitCustomerForm public function Submit handler for the customer select form.
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
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
OrderReassignForm::$order protected property The current order.
OrderReassignForm::$orderAssignment protected property The order assignment service.
OrderReassignForm::$userStorage protected property The user storage.
OrderReassignForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
OrderReassignForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
OrderReassignForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
OrderReassignForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
OrderReassignForm::__construct public function Constructs a new OrderReassignForm object.
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.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.