external_entities-8.x-2.x-dev/src/Form/ExternalEntityTypeDeleteForm.php
src/Form/ExternalEntityTypeDeleteForm.php
<?php
namespace Drupal\external_entities\Form;
use Drupal\Core\Entity\EntityConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Builds the form to delete an external entity type.
*/
class ExternalEntityTypeDeleteForm extends EntityConfirmFormBase {
/**
* The messenger service.
*
* @var \Drupal\Core\Messenger\MessengerInterface
*/
protected $messenger;
/**
* External entity type restrictions.
*
* @var array
*/
protected $locks;
/**
* Constructs a new VocabularyResetForm object.
*
* @param \Drupal\Core\Messenger\MessengerInterface $messenger
* The messenger service.
*/
public function __construct(MessengerInterface $messenger) {
$this->messenger = $messenger;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('messenger')
);
}
/**
* {@inheritdoc}
*/
public function getQuestion() {
return $this->t('Are you sure you want to delete the external entity type %name?', ['%name' => $this->entity->label()]);
}
/**
* {@inheritdoc}
*/
public function getCancelUrl() {
return new Url('entity.external_entity_type.collection');
}
/**
* {@inheritdoc}
*/
public function getConfirmText() {
return $this->t('Delete');
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
// Check if external entity type has locks.
$this->locks = $this->entity->getLocks() ?? [];
if (!empty($this->locks['lock_delete'])) {
return [
'#type' => 'markup',
'#markup' => $this->t('This external entity type cannot be deleted.'),
];
}
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
protected function actions(array $form, FormStateInterface $form_state) {
if (!empty($this->locks['lock_delete'])) {
// Not removable.
return [];
}
return parent::actions($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->entity->delete();
$this->messenger->addMessage($this->t('Deleted the %label external entity type.', [
'%label' => $this->entity->label(),
]));
$form_state->setRedirectUrl($this->getCancelUrl());
}
/**
* {@inheritdoc}
*/
public function getEntityFromRouteMatch(RouteMatchInterface $route_match, $entity_type_id) {
if ($route_match->getParameter($entity_type_id) !== NULL) {
$entity_id = $route_match->getParameter($entity_type_id);
$entity = $this->entityTypeManager->getStorage($entity_type_id)->load($entity_id);
if ($entity) {
return $entity;
}
}
return parent::getEntityFromRouteMatch($route_match, $entity_type_id);
}
}
