FieldConfigDeleteForm.php in Zircon Profile 8
File
core/modules/field_ui/src/Form/FieldConfigDeleteForm.php
View source
<?php
namespace Drupal\field_ui\Form;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Entity\EntityDeleteForm;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\field_ui\FieldUI;
use Symfony\Component\DependencyInjection\ContainerInterface;
class FieldConfigDeleteForm extends EntityDeleteForm {
protected $entityManager;
public function __construct(EntityManagerInterface $entity_manager) {
$this->entityManager = $entity_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity.manager'));
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
if (isset($form['entity_deletes']['field_config']['#items']) && isset($form['entity_deletes']['field_config']['#items'][$this->entity
->id()])) {
unset($form['entity_deletes']['field_config']['#items'][$this->entity
->id()]);
if (empty($form['entity_deletes']['field_config']['#items'])) {
unset($form['entity_deletes']['field_config']);
if (!Element::children($form['entity_deletes'])) {
$form['entity_deletes']['#access'] = FALSE;
}
}
}
return $form;
}
protected function getConfigNamesToDelete(ConfigEntityInterface $entity) {
$field_storage = $entity
->getFieldStorageDefinition();
$config_names = [
$entity
->getConfigDependencyName(),
];
if (count($field_storage
->getBundles()) <= 1) {
$config_names[] = $field_storage
->getConfigDependencyName();
}
return $config_names;
}
public function getCancelUrl() {
return FieldUI::getOverviewRouteInfo($this->entity
->getTargetEntityTypeId(), $this->entity
->getTargetBundle());
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$field_storage = $this->entity
->getFieldStorageDefinition();
$bundles = $this->entityManager
->getBundleInfo($this->entity
->getTargetEntityTypeId());
$bundle_label = $bundles[$this->entity
->getTargetBundle()]['label'];
if ($field_storage && !$field_storage
->isLocked()) {
$this->entity
->delete();
drupal_set_message($this
->t('The field %field has been deleted from the %type content type.', array(
'%field' => $this->entity
->label(),
'%type' => $bundle_label,
)));
}
else {
drupal_set_message($this
->t('There was a problem removing the %field from the %type content type.', array(
'%field' => $this->entity
->label(),
'%type' => $bundle_label,
)), 'error');
}
$form_state
->setRedirectUrl($this
->getCancelUrl());
field_purge_batch(10);
}
}