You are here

public function LibraryAlterTransactionForm::buildForm in Library 8

Form constructor.

Parameters

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

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

null|int $transaction: The transaction.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

src/Form/LibraryAlterTransactionForm.php, line 40

Class

LibraryAlterTransactionForm
Alter the library transaction form.

Namespace

Drupal\library\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $transaction = NULL) : array {
  if ($transaction == NULL) {
    $form_state
      ->setErrorByName('transaction', $this
      ->t('Required parameters missing'));
    return $form;
  }
  $form['transaction'] = [
    '#type' => 'value',
    '#value' => $transaction,
  ];
  $transactionEntity = LibraryTransaction::load($transaction);
  if (!$transactionEntity) {
    $form_state
      ->setErrorByName('transaction', $this
      ->t('Required data missing'));
    return $form;
  }
  $itemEntity = LibraryItem::load($transactionEntity
    ->get('library_item')->value);
  if ($itemEntity
    ->get('nid')
    ->getValue()) {
    $node = Node::load($itemEntity
      ->get('nid')
      ->getValue()[0]['target_id']);
    if ($itemEntity
      ->get('barcode')->value) {
      $format_title = $node
        ->getTitle() . ' (' . $itemEntity
        ->get('barcode')->value . ')';
    }
    else {
      $format_title = $node
        ->getTitle();
    }
    $form['item_display'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Item'),
      '#value' => $format_title,
      '#disabled' => TRUE,
    ];
    $form['nid'] = [
      '#type' => 'value',
      '#value' => $node
        ->id(),
    ];
  }
  else {
    $form_state
      ->setErrorByName('item_display', $this
      ->t('Required parameters missing'));
    return $form;
  }
  $form['notes'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Message'),
    '#required' => FALSE,
    '#maxlength' => 250,
    '#default_value' => $transactionEntity
      ->get('notes')->value,
    '#description' => $this
      ->t('If you are reserving an item, make sure to include the date and time you would like it to be ready.'),
  ];
  $form['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Update'),
  ];
  return $form;
}