ContributorCategoryDeleteForm.php in Bibliography & Citation 2.0.x
File
modules/bibcite_entity/src/Form/ContributorCategoryDeleteForm.php
View source
<?php
namespace Drupal\bibcite_entity\Form;
use Drupal\Core\Entity\EntityConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
class ContributorCategoryDeleteForm extends EntityConfirmFormBase {
private $inUse = FALSE;
private function checkInReference() {
$storage = $this->entityTypeManager
->getStorage('bibcite_reference');
$query = $storage
->getQuery();
return !empty($query
->condition('author.category', $this->entity
->id())
->range(0, 1)
->execute());
}
public function init(FormStateInterface $form_state) {
parent::init($form_state);
$this->inUse = $this
->checkInReference();
}
public function getQuestion() {
if (!$this->inUse) {
return $this
->t('Are you sure you want to delete %name?', [
'%name' => $this->entity
->label(),
]);
}
else {
return $this
->t('Category %name used in reference(s) and cannot be deleted.', [
'%name' => $this->entity
->label(),
]);
}
}
public function getCancelUrl() {
return new Url('entity.bibcite_contributor_category.collection');
}
public function getConfirmText() {
return $this
->t('Delete');
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->entity
->delete();
$this
->messenger()
->addStatus($this
->t('content @type: deleted @label.', [
'@type' => $this->entity
->bundle(),
'@label' => $this->entity
->label(),
]));
$form_state
->setRedirectUrl($this
->getCancelUrl());
}
public function validateForm(array &$form, FormStateInterface $form_state) {
if ($this->inUse) {
$form_state
->setError($form, $this
->t('Category %name used in reference(s) and cannot be deleted.', [
'%name' => $this->entity
->label(),
]));
}
parent::validateForm($form, $form_state);
}
protected function actions(array $form, FormStateInterface $form_state) {
$actions = parent::actions($form, $form_state);
$actions['submit']['#disabled'] = $this->inUse;
return $actions;
}
}