RelationTypeDeleteConfirm.php in Relation 8
File
src/Form/RelationTypeDeleteConfirm.php
View source
<?php
namespace Drupal\relation\Form;
use Drupal\Core\Entity\EntityConfirmFormBase;
use Drupal\Core\Database\Connection;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
class RelationTypeDeleteConfirm extends EntityConfirmFormBase {
protected $database;
public function __construct(Connection $database) {
$this->database = $database;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('database'));
}
public function getQuestion() {
return t('Are you sure you want to delete the relation type %type?', array(
'%type' => $this->entity
->label(),
));
}
public function getCancelUrl() {
return new Url('entity.relation.collection');
}
public function getConfirmText() {
return t('Delete');
}
public function buildForm(array $form, FormStateInterface $form_state) {
$num_relations = $this->database
->query("SELECT COUNT(*) FROM {relation} WHERE relation_type = :type", array(
':type' => $this->entity
->id(),
))
->fetchField();
if ($num_relations) {
$caption = '<p>' . \Drupal::translation()
->formatPlural($num_relations, '%relation_type is used by 1 relation. You can not remove this relation type until you have removed all %relation_type relations.', '%relation_type is used by @count relations. You can not remove %relation_type until you have removed all %relation_type relations.', array(
'%relation_type' => $this->entity
->label(),
)) . '</p>';
$form['#title'] = $this
->getQuestion();
$form['description'] = array(
'#markup' => $caption,
);
return $form;
}
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->entity
->delete();
$t_args = array(
'%relation_type' => $this->entity
->label(),
);
drupal_set_message(t('The relation type %relation_type has been deleted.', $t_args));
$this
->logger('relation')
->notice('Deleted relation type %relation_type.', $t_args);
$form_state
->setRedirect('entity.relation_type.collection');
}
}