You are here

public function LibraryCheckoutForm::submitForm in Library 8

Form submission handler.

Parameters

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

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

Overrides FormInterface::submitForm

File

src/Form/LibraryCheckoutForm.php, line 133

Class

LibraryCheckoutForm
Library checkout form.

Namespace

Drupal\library\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) : void {
  $transaction = LibraryTransaction::create();
  $transaction
    ->set('librarian_id', \Drupal::currentUser()
    ->id());
  $transaction
    ->set('nid', $form_state
    ->getValue('nid'));
  $transaction
    ->set('uid', $form_state
    ->getValue('patron')[0]['target_id']);
  $transaction
    ->set('library_item', $form_state
    ->getValue('library_item'));
  $transaction
    ->set('action', $form_state
    ->getValue('action'));
  $transaction
    ->set('due_date', LibraryItemHelper::computeDueDate($form_state
    ->getValue('action'), $form_state
    ->getValue('nid')));
  $transaction
    ->set('notes', $form_state
    ->getValue('notes'));
  $transaction
    ->save();
  LibraryItemHelper::updateItemAvailability($form_state
    ->getValue('library_item'), $form_state
    ->getValue('action'));
  \Drupal::service('cache_tags.invalidator')
    ->invalidateTags([
    'node:' . $form_state
      ->getValue('nid'),
  ]);
  $this
    ->messenger()
    ->addStatus($this
    ->t('Transaction processed.'));
  \Drupal::service('event_dispatcher')
    ->dispatch('library.action', new ActionEvent($transaction));
  $form_state
    ->setRedirect('entity.node.canonical', [
    'node' => $form_state
      ->getValue('nid'),
  ]);
}