competition-8.x-1.x-dev/src/Form/CompetitionDeleteForm.php
src/Form/CompetitionDeleteForm.php
<?php
namespace Drupal\competition\Form;
use Drupal\Core\Entity\Query\QueryFactory;
use Drupal\Core\Entity\EntityDeleteForm;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Url;
/**
* Builds the form to delete Competition entities.
*/
class CompetitionDeleteForm extends EntityDeleteForm {
/**
* The query factory to create entity queries.
*
* @var \Drupal\Core\Entity\Query\QueryFactory
*/
protected $queryFactory;
/**
* Constructs a new NodeTypeDeleteConfirm object.
*
* @param \Drupal\Core\Entity\Query\QueryFactory $query_factory
* The entity query object.
*/
public function __construct(QueryFactory $query_factory) {
$this->queryFactory = $query_factory;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity.query')
);
}
/**
* {@inheritdoc}
*/
public function getQuestion() {
return $this->t('Are you sure you want to delete %name?', array('%name' => $this->entity->label()));
}
/**
* {@inheritdoc}
*/
public function getCancelUrl() {
return new Url('entity.competition.collection');
}
/**
* {@inheritdoc}
*/
public function getConfirmText() {
return $this->t('Delete');
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$num_entries = $this->queryFactory->get('competition_entry')
->condition('type', $this->entity->id())
->count()
->execute();
if ($num_entries) {
$caption = '<p>' . $this->formatPlural($num_entries, '%type is used by 1 competition entry on your site. You can not remove this competition until you have removed all of the %type entries.', '%type is used by @count competition entries on your site. You may not remove %type until you have removed all of the %type entries.', array('%type' => $this->entity->label())) . '</p>';
$form['#title'] = $this->getQuestion();
$form['description'] = array('#markup' => $caption);
return $form;
}
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->entity->delete();
drupal_set_message(
$this->t('Deleted @type "@label".',
[
'@type' => $this->entity->bundle(),
'@label' => $this->entity->label(),
]
)
);
$form_state->setRedirectUrl($this->getCancelUrl());
}
}
