You are here

public function LibraryCheckoutForm::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.

string|null $action: Relevant action.

string|null $item: Relevant item.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

src/Form/LibraryCheckoutForm.php, line 44

Class

LibraryCheckoutForm
Library checkout form.

Namespace

Drupal\library\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $action = NULL, $item = NULL) : array {
  if ($action == NULL || $item == NULL) {
    $form_state
      ->setErrorByName('action', $this
      ->t('Required parameters missing'));
    return $form;
  }
  $form['action'] = [
    '#type' => 'hidden',
    '#value' => $action,
  ];
  $form['library_item'] = [
    '#type' => 'hidden',
    '#value' => $item,
  ];
  $itemEntity = LibraryItem::load($item);
  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;
  }
  $actionEntity = LibraryAction::load($action);
  if ($actionEntity
    ->action() == LibraryAction::CHANGE_TO_UNAVAILABLE) {
    $form['patron'] = [
      '#type' => 'entity_autocomplete',
      '#target_type' => 'user',
      '#tags' => TRUE,
      '#required' => TRUE,
    ];
  }
  $form['notes'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Message'),
    '#required' => FALSE,
    '#maxlength' => 250,
    '#default_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' => $actionEntity
      ->label(),
  ];
  return $form;
}