OrderForm.php in Ubercart 8.4
File
uc_order/src/OrderForm.php
View source
<?php
namespace Drupal\uc_order;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\uc_order\Plugin\OrderPaneManager;
use Drupal\uc_store\AjaxAttachTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
class OrderForm extends ContentEntityForm {
use AjaxAttachTrait;
protected $orderPaneManager;
public function __construct(EntityManagerInterface $entity_manager, OrderPaneManager $order_pane_manager) {
parent::__construct($entity_manager);
$this->orderPaneManager = $order_pane_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity.manager'), $container
->get('plugin.manager.uc_order.order_pane'));
}
public function form(array $form, FormStateInterface $form_state) {
$order = $this->entity;
$form['#order'] = $order;
$form['order_modified'] = [
'#type' => 'value',
'#value' => $form_state
->getValue('order_modified') ?: $order
->getChangedTime(),
];
$panes = $this->orderPaneManager
->getPanes();
$components = $this
->getFormDisplay($form_state)
->getComponents();
foreach ($panes as $id => $pane) {
if ($pane instanceof EditableOrderPanePluginInterface) {
$form[$id] = $pane
->buildForm($order, [], $form_state);
$form[$id]['#prefix'] = '<div class="order-pane ' . implode(' ', $pane
->getClasses()) . '" id="order-pane-' . $id . '">';
if ($title = $pane
->getTitle()) {
$form[$id]['#prefix'] .= '<div class="order-pane-title">' . $title . ':' . '</div>';
}
$form[$id]['#suffix'] = '</div>';
$form[$id]['#weight'] = $components[$id]['weight'];
}
}
$form = parent::form($form, $form_state);
$form['#process'][] = [
$this,
'ajaxProcessForm',
];
return $form;
}
protected function actions(array $form, FormStateInterface $form_state) {
$element = parent::actions($form, $form_state);
$element['submit']['#value'] = $this
->t('Save changes');
$element['delete']['#access'] = $this->entity
->access('delete');
return $element;
}
public function validate(array $form, FormStateInterface $form_state) {
$order = $this
->buildEntity($form, $form_state);
if ($form_state
->getValue('order_modified') != $order
->getChangedTime()) {
$form_state
->setErrorByName('order_modified', $this
->t('This order has been modified by another user, changes cannot be saved.'));
}
parent::validate($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$order = $this->entity;
$original = clone $order;
$panes = $this->orderPaneManager
->getPanes();
foreach ($panes as $pane) {
if ($pane instanceof EditableOrderPanePluginInterface) {
$pane
->submitForm($order, $form, $form_state);
}
}
$log = [];
foreach (array_keys($order
->getFieldDefinitions()) as $key) {
if ($order->{$key}->value != $original->{$key}->value) {
if (!is_array($order->{$key}->value)) {
$log[$key] = [
'old' => $original->{$key}->value,
'new' => $order->{$key}->value,
];
}
}
}
$order->line_items = $order
->getLineItems();
$order
->logChanges($log);
$order
->save();
$this
->messenger()
->addMessage($this
->t('Order changes saved.'));
}
}
Classes
Name |
Description |
OrderForm |
Form controller for the Ubercart order form. |