View source
<?php
namespace Drupal\library\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\library\Entity\LibraryItem;
use Drupal\library\Entity\LibraryTransaction;
use Drupal\library\Event\ActionEvent;
use Drupal\node\Entity\Node;
class LibraryAlterTransactionForm extends FormBase {
public function getFormId() {
return 'library_alter_transaction_form';
}
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;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$transaction = LibraryTransaction::load($form_state
->getValue('transaction'));
$transaction
->set('notes', $form_state
->getValue('notes'));
$transaction
->save();
\Drupal::service('cache_tags.invalidator')
->invalidateTags([
'node:' . $form_state
->getValue('nid'),
]);
$this
->messenger()
->addStatus($this
->t('Transaction updated.'));
\Drupal::service('event_dispatcher')
->dispatch('library.action', new ActionEvent($transaction));
$form_state
->setRedirect('entity.node.canonical', [
'node' => $form_state
->getValue('nid'),
]);
}
}