You are here

private function LibraryCheckOutBulkForm::processTransaction in Library 8

Process a transaction.

Parameters

\Drupal\library\Entity\LibraryItem $item: Item to process.

array $data: Data context.

1 call to LibraryCheckOutBulkForm::processTransaction()
LibraryCheckOutBulkForm::submitForm in src/Form/LibraryCheckOutBulkForm.php
Form submission handler.

File

src/Form/LibraryCheckOutBulkForm.php, line 134

Class

LibraryCheckOutBulkForm
Bulk checkout form.

Namespace

Drupal\library\Form

Code

private function processTransaction(LibraryItem $item, array $data) {
  $transaction = LibraryTransaction::create();
  $transaction
    ->set('librarian_id', \Drupal::currentUser()
    ->id());
  $transaction
    ->set('nid', $item
    ->get('nid')
    ->getValue());
  if (isset($data['patron'])) {
    $transaction
      ->set('uid', $data['patron']);
  }
  $transaction
    ->set('library_item', $item
    ->id());
  $transaction
    ->set('action', $data['action']);
  $transaction
    ->set('due_date', LibraryItemHelper::computeDueDate($data['action'], $item
    ->get('nid')
    ->getValue()[0]['target_id']));
  $transaction
    ->set('notes', $data['notes']);
  $transaction
    ->save();
  LibraryItemHelper::updateItemAvailability($item
    ->id(), $data['action']);
  \Drupal::service('cache_tags.invalidator')
    ->invalidateTags([
    'node:' . $item
      ->get('nid')
      ->getValue()[0]['target_id'],
  ]);
  $node = Node::load($item
    ->get('nid')
    ->getValue()[0]['target_id']);
  $item_name = $node
    ->getTitle() . ' (' . $item
    ->get('barcode')->value . ')';
  $this
    ->messenger()
    ->addStatus($this
    ->t('Transaction processed for @item.', [
    '@item' => $item_name,
  ]));
  \Drupal::service('event_dispatcher')
    ->dispatch('library.action', new ActionEvent($transaction));
}