ClusterDeleteForm.php in Elasticsearch Connector 8.2
File
src/Form/ClusterDeleteForm.php
View source
<?php
namespace Drupal\elasticsearch_connector\Form;
use Drupal\Core\Entity\EntityConfirmFormBase;
use Drupal\Core\Entity\EntityTypeManager;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\elasticsearch_connector\ElasticSearch\ClientManagerInterface;
use Drupal\elasticsearch_connector\Entity\Cluster;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ClusterDeleteForm extends EntityConfirmFormBase {
private $clientManager;
protected $entityManager;
public function __construct(EntityTypeManager $entity_manager, ClientManagerInterface $client_manager) {
$this->entityManager = $entity_manager;
$this->clientManager = $client_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'), $container
->get('elasticsearch_connector.client_manager'));
}
public function getQuestion() {
return t('Are you sure you want to delete the cluster %title?', array(
'%title' => $this->entity
->label(),
));
}
public function getDescription() {
return $this
->t('Deleting a cluster will disable all its indexes and their searches.');
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$storage = $this->entityManager
->getStorage('elasticsearch_index');
$indices = $storage
->loadByProperties(array(
'server' => $this->entity->cluster_id,
));
if (count($indices)) {
drupal_set_message($this
->t('The cluster %title cannot be deleted as it still has indices.', array(
'%title' => $this->entity
->label(),
)), 'error');
return;
}
if ($this->entity
->id() == Cluster::getDefaultCluster()) {
drupal_set_message($this
->t('The cluster %title cannot be deleted as it is set as the default cluster.', array(
'%title' => $this->entity
->label(),
)), 'error');
}
else {
$this->entity
->delete();
drupal_set_message($this
->t('The cluster %title has been deleted.', array(
'%title' => $this->entity
->label(),
)));
$form_state
->setRedirect('elasticsearch_connector.config_entity.list');
}
}
public function getConfirmText() {
return $this
->t('Delete');
}
public function getCancelUrl() {
return new Url('elasticsearch_connector.config_entity.list');
}
}