SavedSearchTypeDeleteConfirmForm.php in Search API Saved Searches 8
File
src/Form/SavedSearchTypeDeleteConfirmForm.php
View source
<?php
namespace Drupal\search_api_saved_searches\Form;
use Drupal\Core\Config\ConfigManager;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Entity\EntityConfirmFormBase;
use Drupal\Core\Entity\EntityDeleteFormTrait;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class SavedSearchTypeDeleteConfirmForm extends EntityConfirmFormBase {
use EntityDeleteFormTrait;
protected $configManager;
public static function create(ContainerInterface $container) {
$form = parent::create($container);
$form
->setConfigManager($container
->get('config.manager'));
return $form;
}
public function getConfigManager() {
return $this->configManager ?: \Drupal::service('config.manager');
}
public function setConfigManager(ConfigManager $config_manager) {
$this->configManager = $config_manager;
return $this;
}
public function buildForm(array $form, FormStateInterface $form_state) {
$num_searches = $this->entityTypeManager
->getStorage('search_api_saved_search')
->getQuery()
->condition('type', $this->entity
->id())
->count()
->accessCheck(FALSE)
->execute();
if ($num_searches) {
$caption = '<p>' . $this
->formatPlural($num_searches, '%type is used by 1 saved search on your site. You cannot remove this saved search type until you have removed all of the %type saved searches.', '%type is used by @count saved searches on your site. You cannot remove this saved search type until you have removed all of the %type saved searches.', [
'%type' => $this->entity
->label(),
]) . '</p>';
$form['#title'] = $this
->getQuestion();
$form['description'] = [
'#markup' => $caption,
];
return $form;
}
$form = parent::buildForm($form, $form_state);
$entity = $this
->getEntity();
$this
->addDependencyListsToForm($form, $entity
->getConfigDependencyKey(), $this
->getConfigNamesToDelete($entity), $this
->getConfigManager(), $this->entityTypeManager);
return $form;
}
protected function getConfigNamesToDelete(ConfigEntityInterface $entity) {
return [
$entity
->getConfigDependencyName(),
];
}
public function getQuestion() {
return $this
->t('Do you really want to delete this saved search type?');
}
public function getCancelUrl() {
return $this->entity
->toUrl('collection');
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->entity
->delete();
$this
->messenger()
->addStatus($this
->t('The saved search type was successfully deleted.'));
$form_state
->setRedirectUrl($this
->getCancelUrl());
}
}