You are here

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

Expanded class hierarchy of LineItems

File

uc_order/src/Plugin/Ubercart/OrderPane/LineItems.php, line 21

Namespace

Drupal\uc_order\Plugin\Ubercart\OrderPane
View 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

Namesort descending Modifiers Type Description Overrides
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
LineItems::addLineItem public function Order pane submit callback: Add a line item to an order.
LineItems::ajaxCallback public function AJAX callback to render the line items.
LineItems::buildForm public function Form constructor. Overrides EditableOrderPanePluginInterface::buildForm
LineItems::getClasses public function Returns the classes used to wrap an order pane. Overrides OrderPanePluginBase::getClasses
LineItems::getTitle public function Returns the title of an order pane. Overrides OrderPanePluginBase::getTitle
LineItems::removeLineItem public function Order pane submit callback: Remove a line item from an order.
LineItems::submitForm public function Form submission handler. Overrides EditableOrderPanePluginInterface::submitForm
LineItems::view public function Returns the contents of an order pane as a store administrator. Overrides OrderPanePluginInterface::view
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
OrderPanePluginBase::calculateDependencies public function
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.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 92
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.