RngContactTypeDeleteForm.php in RNG Contact 8
File
src/Form/RngContactTypeDeleteForm.php
View source
<?php
namespace Drupal\rng_contact\Form;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityDeleteForm;
use Drupal\Core\Url;
class RngContactTypeDeleteForm extends EntityDeleteForm {
public function getQuestion() {
return $this
->t('Are you sure you want to delete contact type %label?', array(
'%label' => $this->entity
->label(),
));
}
public function getDescription() {
return t('Deleting this contact type will also delete the associated contacts.');
}
public function getConfirmText() {
return $this
->t('Delete');
}
public function getCancelUrl() {
return new Url('entity.rng_contact_type.collection');
}
public function buildForm(array $form, FormStateInterface $form_state) {
$rng_contact_type =& $this->entity;
$count = $this->entityTypeManager
->getStorage('rng_contact')
->getQuery()
->condition('type', $rng_contact_type
->id())
->count()
->execute();
if ($count > 0) {
drupal_set_message($this
->t('Cannot delete contact type.'), 'warning');
$form['#title'] = $this
->getQuestion();
$form['description'] = [
'#markup' => $this
->formatPlural($count, 'Unable to delete contact type. It is used by @count contact.', 'Unable to delete contact type. It is used by @count contacts.'),
];
}
else {
$form = parent::buildForm($form, $form_state);
}
return $form;
}
}