TransactionTypeDeleteForm.php in Transaction 8
File
src/Form/TransactionTypeDeleteForm.php
View source
<?php
namespace Drupal\transaction\Form;
use Drupal\Core\Entity\EntityConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
class TransactionTypeDeleteForm extends EntityConfirmFormBase {
public function buildForm(array $form, FormStateInterface $form_state) {
$transaction_count = $this->entityTypeManager
->getStorage('transaction')
->getQuery()
->condition('type', $this->entity
->id())
->count()
->execute();
if ($transaction_count) {
$form['#title'] = $this
->getQuestion();
$caption = '<p>' . $this
->formatPlural($transaction_count, 'There is 1 transaction of type %type that prevents it from being deleted.', 'There are @count transactions of type %type that prevents it from being deleted.', [
'%type' => $this->entity
->label(),
]) . '</p>';
$form['description'] = [
'#markup' => $caption,
];
return $form;
}
return parent::buildForm($form, $form_state);
}
public function getQuestion() {
return $this
->t('Are you sure you want to delete %name?', [
'%name' => $this->entity
->label(),
]);
}
public function getCancelUrl() {
return new Url('entity.transaction_type.collection');
}
public function getConfirmText() {
return $this
->t('Delete');
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->entity
->delete();
$this
->messenger()
->addStatus($this
->t('Transaction type @label deleted.', [
'@label' => $this->entity
->label(),
]));
$form_state
->setRedirectUrl($this
->getCancelUrl());
}
}