You are here

public function Products::view in Ubercart 8.4

Returns the contents of an order pane as a store administrator.

Parameters

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

string $view_mode: The view mode that is being used to render the order.

Return value

array A render array showing order data.

Overrides OrderPanePluginInterface::view

File

uc_order/src/Plugin/Ubercart/OrderPane/Products.php, line 29

Class

Products
Manage the products an order contains.

Namespace

Drupal\uc_order\Plugin\Ubercart\OrderPane

Code

public function view(OrderInterface $order, $view_mode) {
  $build = [
    '#type' => 'table',
    '#attributes' => [
      'class' => [
        'order-pane-table',
      ],
    ],
    '#header' => [
      'qty' => [
        'data' => $this
          ->t('Qty'),
        'class' => [
          'qty',
        ],
      ],
      'product' => [
        'data' => $this
          ->t('Product'),
        'class' => [
          'product',
        ],
      ],
      'model' => [
        'data' => $this
          ->t('SKU'),
        'class' => [
          'sku',
          RESPONSIVE_PRIORITY_LOW,
        ],
      ],
      'cost' => [
        'data' => $this
          ->t('Cost'),
        'class' => [
          'cost',
          RESPONSIVE_PRIORITY_LOW,
        ],
      ],
      'price' => [
        'data' => $this
          ->t('Price'),
        'class' => [
          'price',
        ],
      ],
      'total' => [
        'data' => $this
          ->t('Total'),
        'class' => [
          'price',
        ],
      ],
    ],
    '#empty' => $this
      ->t('This order contains no products.'),
  ];
  $account = \Drupal::currentUser();
  if (!$account
    ->hasPermission('administer products')) {
    unset($build['#header']['cost']);
  }

  // @todo Replace with Views.
  $rows = [];
  foreach ($order->products as $id => $product) {
    $rows[$id]['qty'] = [
      'data' => [
        '#theme' => 'uc_qty',
        '#qty' => $product->qty->value,
      ],
      'class' => [
        'qty',
      ],
    ];
    if ($product->nid->entity && $product->nid->entity
      ->access('view')) {
      $title = $product->nid->entity
        ->toLink()
        ->toString();
    }
    else {
      $title = $product->title->value;
    }
    $rows[$id]['product'] = [
      'data' => [
        '#markup' => $title . uc_product_get_description($product),
      ],
      'class' => [
        'product',
      ],
    ];
    $rows[$id]['model'] = [
      'data' => [
        '#markup' => $product->model->value,
      ],
      'class' => [
        'sku',
      ],
    ];
    if ($account
      ->hasPermission('administer products')) {
      $rows[$id]['cost'] = [
        'data' => [
          '#theme' => 'uc_price',
          '#price' => $product->cost->value,
        ],
        'class' => [
          'cost',
        ],
      ];
    }
    $rows[$id]['price'] = [
      'data' => [
        '#theme' => 'uc_price',
        '#price' => $product->price->value,
        '#suffixes' => [],
      ],
      'class' => [
        'price',
      ],
    ];
    $rows[$id]['total'] = [
      'data' => [
        '#theme' => 'uc_price',
        '#price' => $product->price->value * $product->qty->value,
        '#suffixes' => [],
      ],
      'class' => [
        'total',
      ],
    ];
  }
  $build['#rows'] = $rows;
  return $build;
}