OrderReassignForm.php in Commerce Core 8.2
File
modules/order/src/Form/OrderReassignForm.php
View source
<?php
namespace Drupal\commerce_order\Form;
use Drupal\commerce_order\OrderAssignmentInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\CurrentRouteMatch;
use Symfony\Component\DependencyInjection\ContainerInterface;
class OrderReassignForm extends FormBase {
use CustomerFormTrait;
protected $order;
protected $orderAssignment;
protected $userStorage;
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');
}
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'));
}
public function getFormId() {
return 'commerce_order_reassign_form';
}
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 ($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;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$this
->submitCustomerForm($form, $form_state);
$values = $form_state
->getValues();
$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'));
}
}