You are here

public function OrderComments::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/OrderComments.php, line 24

Class

OrderComments
View the order comments, used for communicating with customers.

Namespace

Drupal\uc_order\Plugin\Ubercart\OrderPane

Code

public function view(OrderInterface $order, $view_mode) {

  // @todo Simplify this or replace with Views
  if ($view_mode == 'customer') {
    $comments = uc_order_comments_load($order
      ->id());
    $statuses = OrderStatus::loadMultiple();
    $header = [
      [
        'data' => $this
          ->t('Date'),
        'class' => [
          'date',
        ],
      ],
      [
        'data' => $this
          ->t('Status'),
        'class' => [
          'status',
        ],
      ],
      [
        'data' => $this
          ->t('Message'),
        'class' => [
          'message',
        ],
      ],
    ];
    $rows[] = [
      [
        'data' => \Drupal::service('date.formatter')
          ->format($order->created->value, 'short'),
        'class' => [
          'date',
        ],
      ],
      [
        'data' => '-',
        'class' => [
          'status',
        ],
      ],
      [
        'data' => $this
          ->t('Order created.'),
        'class' => [
          'message',
        ],
      ],
    ];
    if (count($comments) > 0) {
      foreach ($comments as $comment) {
        $rows[] = [
          [
            'data' => \Drupal::service('date.formatter')
              ->format($comment->created, 'short'),
            'class' => [
              'date',
            ],
          ],
          [
            'data' => [
              '#plain_text' => $statuses[$comment->order_status]
                ->getName(),
            ],
            'class' => [
              'status',
            ],
          ],
          [
            'data' => [
              '#markup' => $comment->message,
            ],
            'class' => [
              'message',
            ],
          ],
        ];
      }
    }
    $build = [
      '#theme' => 'table',
      '#header' => $header,
      '#rows' => $rows,
      '#attributes' => [
        'class' => [
          'uc-order-comments',
        ],
      ],
    ];
  }
  else {
    $build = [
      '#theme' => 'table',
      '#header' => [
        [
          'data' => $this
            ->t('Date'),
          'class' => [
            'date',
          ],
        ],
        [
          'data' => $this
            ->t('User'),
          'class' => [
            'user',
            RESPONSIVE_PRIORITY_LOW,
          ],
        ],
        [
          'data' => $this
            ->t('Notified'),
          'class' => [
            'notified',
          ],
        ],
        [
          'data' => $this
            ->t('Status'),
          'class' => [
            'status',
            RESPONSIVE_PRIORITY_LOW,
          ],
        ],
        [
          'data' => $this
            ->t('Comment'),
          'class' => [
            'message',
          ],
        ],
      ],
      '#rows' => [],
      '#attributes' => [
        'class' => [
          'order-pane-table uc-order-comments',
        ],
      ],
      '#empty' => $this
        ->t('This order has no comments associated with it.'),
    ];
    $comments = uc_order_comments_load($order
      ->id());
    $statuses = OrderStatus::loadMultiple();
    foreach ($comments as $comment) {
      $icon = $comment->notified ? 'true-icon.gif' : 'false-icon.gif';
      $build['#rows'][] = [
        [
          'data' => \Drupal::service('date.formatter')
            ->format($comment->created, 'short'),
          'class' => [
            'date',
          ],
        ],
        [
          'data' => [
            '#theme' => 'username',
            '#account' => User::load($comment->uid),
          ],
          'class' => [
            'user',
          ],
        ],
        [
          'data' => [
            '#theme' => 'image',
            '#uri' => drupal_get_path('module', 'uc_order') . '/images/' . $icon,
          ],
          'class' => [
            'notified',
          ],
        ],
        [
          'data' => [
            '#plain_text' => $statuses[$comment->order_status]
              ->getName(),
          ],
          'class' => [
            'status',
          ],
        ],
        [
          'data' => [
            '#markup' => $comment->message,
          ],
          'class' => [
            'message',
          ],
        ],
      ];
    }
  }
  return $build;
}