class LineItems in Ubercart 8.4
View and modify an order's line items.
Plugin annotation
@UbercartOrderPane(
id = "line_items",
title = @Translation("Line items"),
weight = 6,
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\uc_order\OrderPanePluginBase implements OrderPanePluginInterface
- class \Drupal\uc_order\EditableOrderPanePluginBase implements EditableOrderPanePluginInterface
- class \Drupal\uc_order\Plugin\Ubercart\OrderPane\LineItems
- class \Drupal\uc_order\EditableOrderPanePluginBase implements EditableOrderPanePluginInterface
- class \Drupal\uc_order\OrderPanePluginBase implements OrderPanePluginInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of LineItems
File
- uc_order/
src/ Plugin/ Ubercart/ OrderPane/ LineItems.php, line 21
Namespace
Drupal\uc_order\Plugin\Ubercart\OrderPaneView source
class LineItems extends EditableOrderPanePluginBase {
/**
* {@inheritdoc}
*/
public function getTitle() {
return '';
}
/**
* {@inheritdoc}
*/
public function getClasses() {
return [
'pos-right',
];
}
/**
* {@inheritdoc}
*/
public function view(OrderInterface $order, $view_mode) {
$rows = [];
foreach ($order
->getDisplayLineItems() as $item) {
$rows[] = [
'data' => [
// Title column.
[
'data' => [
'#markup' => $item['title'],
],
'class' => [
'li-title',
],
],
// Amount column.
[
'data' => [
'#theme' => 'uc_price',
'#price' => $item['amount'],
],
'class' => [
'li-amount',
],
],
],
];
}
$build['line_items'] = [
'#type' => 'table',
'#rows' => $rows,
'#attributes' => [
'class' => [
'line-item-table',
],
],
];
return $build;
}
/**
* {@inheritdoc}
*/
public function buildForm(OrderInterface $order, array $form, FormStateInterface $form_state) {
$options = [];
$line_item_manager = \Drupal::service('plugin.manager.uc_order.line_item');
$definitions = $line_item_manager
->getDefinitions();
foreach ($definitions as $item) {
if ($item['add_list']) {
$options[$item['id']] = $item['title'];
}
}
$form['add_line_item'] = [
'#type' => 'container',
];
$form['add_line_item']['li_type_select'] = [
'#type' => 'select',
'#title' => $this
->t('Add a line item'),
'#options' => $options,
];
$form['add_line_item']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Add line'),
'#submit' => [
[
$this,
'submitForm',
],
[
$this,
'addLineItem',
],
],
'#ajax' => [
'callback' => [
$this,
'ajaxCallback',
],
],
];
$form['line_items'] = [
'#type' => 'table',
'#tree' => TRUE,
'#attributes' => [
'class' => [
'line-item-table',
],
],
'#prefix' => '<div id="order-line-items">',
'#suffix' => '</div>',
];
foreach ($order
->getDisplayLineItems() as $item) {
$id = $item['line_item_id'];
$form['line_items'][$id]['li_id'] = [
'#type' => 'hidden',
'#value' => $id,
];
if (!empty($definitions[$item['type']]['stored'])) {
$form['line_items'][$id]['remove'] = [
'#type' => 'image_button',
'#title' => $this
->t('Remove line item.'),
'#src' => drupal_get_path('module', 'uc_store') . '/images/error.gif',
'#button_type' => 'remove',
'#submit' => [
[
$this,
'submitForm',
],
[
$this,
'removeLineItem',
],
],
'#ajax' => [
'callback' => [
$this,
'ajaxCallback',
],
],
'#return_value' => $id,
];
$form['line_items'][$id]['title'] = [
'#type' => 'textfield',
'#title' => $this
->t('Title'),
'#title_display' => 'invisible',
'#default_value' => $item['title'],
'#size' => 40,
'#maxlength' => 128,
];
$form['line_items'][$id]['amount'] = [
'#type' => 'uc_price',
'#title' => $this
->t('Amount'),
'#title_display' => 'invisible',
'#default_value' => $item['amount'],
'#size' => 6,
'#allow_negative' => TRUE,
'#wrapper_attributes' => [
'class' => [
'li-amount',
],
],
];
}
else {
$form['line_items'][$id]['remove'] = [
'#markup' => '',
];
$form['line_items'][$id]['title'] = [
'#plain_text' => $item['title'],
];
$form['line_items'][$id]['amount'] = [
'#theme' => 'uc_price',
'#price' => $item['amount'],
'#wrapper_attributes' => [
'class' => [
'li-amount',
],
],
];
}
}
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(OrderInterface $order, array &$form, FormStateInterface $form_state) {
$values = $form_state
->getValues();
if (is_array($values['line_items'])) {
foreach ($values['line_items'] as $line) {
if (is_numeric($line['li_id']) && intval($line['li_id']) > 0 && isset($line['title']) && isset($line['amount'])) {
uc_order_update_line_item($line['li_id'], $line['title'], $line['amount']);
}
}
}
}
/**
* Order pane submit callback: Add a line item to an order.
*/
public function addLineItem($form, FormStateInterface $form_state) {
$order =& $form_state
->get('order');
$type = $form_state
->getValue('li_type_select');
$line_item_manager = \Drupal::service('plugin.manager.uc_order.line_item');
uc_order_line_item_add($order
->id(), $type, $line_item_manager
->getDefinition($type)['title'], 0);
$order->line_items = $order
->getLineItems();
$form_state
->setRebuild();
}
/**
* Order pane submit callback: Remove a line item from an order.
*/
public function removeLineItem($form, FormStateInterface $form_state) {
$order =& $form_state
->get('order');
$triggering_element = $form_state
->getTriggeringElement();
$line_item_id = intval($triggering_element['#return_value']);
uc_order_delete_line_item($line_item_id);
$order->line_items = $order
->getLineItems();
$form_state
->setRebuild();
}
/**
* AJAX callback to render the line items.
*/
public function ajaxCallback($form, FormStateInterface $form_state) {
$response = new AjaxResponse();
$response
->addCommand(new ReplaceCommand('#order-line-items', trim(drupal_render($form['line_items']))));
$status_messages = [
'#type' => 'status_messages',
];
$response
->addCommand(new PrependCommand('#order-line-items', drupal_render($status_messages)));
return $response;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
LineItems:: |
public | function | Order pane submit callback: Add a line item to an order. | |
LineItems:: |
public | function | AJAX callback to render the line items. | |
LineItems:: |
public | function |
Form constructor. Overrides EditableOrderPanePluginInterface:: |
|
LineItems:: |
public | function |
Returns the classes used to wrap an order pane. Overrides OrderPanePluginBase:: |
|
LineItems:: |
public | function |
Returns the title of an order pane. Overrides OrderPanePluginBase:: |
|
LineItems:: |
public | function | Order pane submit callback: Remove a line item from an order. | |
LineItems:: |
public | function |
Form submission handler. Overrides EditableOrderPanePluginInterface:: |
|
LineItems:: |
public | function |
Returns the contents of an order pane as a store administrator. Overrides OrderPanePluginInterface:: |
|
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
OrderPanePluginBase:: |
public | function | ||
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginBase:: |
public | function | Constructs a \Drupal\Component\Plugin\PluginBase object. | 92 |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |