You are here

public function LineItems::buildForm in Ubercart 8.4

Form constructor.

Parameters

\Drupal\uc_order\OrderInterface $order: The order that is being viewed.

array $form: An array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides EditableOrderPanePluginInterface::buildForm

File

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

Class

LineItems
View and modify an order's line items.

Namespace

Drupal\uc_order\Plugin\Ubercart\OrderPane

Code

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;
}