You are here

private function ItemHistory::showHistoryForItem in Library 8

History for one item.

Parameters

\Drupal\library\Entity\LibraryItem $item: Item to build list for.

Return value

array Markup to render.

1 call to ItemHistory::showHistoryForItem()
ItemHistory::show in src/Controller/ItemHistory.php
Show item history.

File

src/Controller/ItemHistory.php, line 64

Class

ItemHistory
Item history controller.

Namespace

Drupal\library\Controller

Code

private function showHistoryForItem(LibraryItem $item) {
  $data['heading'] = [
    '#markup' => '<h2>' . $item
      ->get('barcode')->value . '</h2>',
  ];
  $data['elements'] = [
    '#type' => 'table',
    '#title' => $this
      ->t('Item history'),
    '#header' => [
      'Librarian',
      'Patron',
      'Last edited',
      'Action',
      'Due Date',
      'Notes',
    ],
  ];
  $transactions = \Drupal::entityQuery('library_transaction')
    ->condition('library_item', $item
    ->id())
    ->execute();
  foreach ($transactions as $transaction) {
    $transactionEntity = LibraryTransaction::load($transaction);
    $due = '';
    if ($transactionEntity
      ->get('due_date')->value > 0) {
      $due = \Drupal::service('date.formatter')
        ->format($transactionEntity
        ->get('due_date')->value);
    }
    $data['elements']['#rows'][$transactionEntity
      ->get('id')->value] = [
      'librarian' => $this
        ->formatUser($transactionEntity
        ->get('librarian_id')),
      'patron' => $this
        ->formatUser($transactionEntity
        ->get('uid')),
      'date' => \Drupal::service('date.formatter')
        ->format($transactionEntity
        ->get('changed')->value),
      'action' => $this
        ->formatAction($transactionEntity
        ->get('action')->value, $transactionEntity
        ->get('legacy_state_change')->value),
      'due' => $due,
      'notes' => $this
        ->formatNotes($transactionEntity
        ->get('notes')->value),
    ];
  }
  if (isset($data['elements']['#rows'])) {

    // Sort table by transaction date descending.
    $data['elements']['#rows'] = array_reverse($data['elements']['#rows']);
  }
  else {
    $data['elements'] = [
      '#markup' => '<h3>' . $this
        ->t('No transactions recorded.') . '</h3>',
    ];
  }
  return $data;
}