View source
<?php
namespace Drupal\entity_delete\Form;
use Drupal\Core\Access\CsrfTokenGenerator;
use Drupal\Core\Entity\ContentEntityType;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
class EntityDeleteForm extends FormBase {
public function getFormId() {
return 'entity_delete_form';
}
protected $entityTypeManager;
protected $csrfToken;
public function __construct(EntityTypeManagerInterface $entity_type_manager, CsrfTokenGenerator $csrf_token) {
$this->entityTypeManager = $entity_type_manager;
$this->csrfToken = $csrf_token;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'), $container
->get('csrf_token'));
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form['displays'] = [];
$input =& $form_state
->getUserInput();
$wrapper = 'entity-wrapper';
$form['displays']['show'] = [
'#type' => 'fieldset',
'#title' => t('Entity Delete Settings'),
'#tree' => TRUE,
'#attributes' => [
'class' => [
'container-inline',
],
],
];
$content_entity_types = [];
$entity_type_definations = $this->entityTypeManager
->getDefinitions();
foreach ($entity_type_definations as $definition) {
if ($definition instanceof ContentEntityType) {
$content_entity_types[$definition
->id()] = $definition
->getLabel();
}
}
$form['displays']['show']['entity_type'] = [
'#type' => 'select',
'#title' => $this
->t('Select Entity Type'),
'#options' => $content_entity_types,
'#empty_option' => $this
->t('-select-'),
'#size' => 1,
'#required' => TRUE,
'#ajax' => [
'callback' => [
$this,
'ajaxCallChangeEntity',
],
'wrapper' => $wrapper,
],
];
$type_options = [
'all' => $this
->t('All'),
];
$form['displays']['show']['type'] = [
'#type' => 'select',
'#title' => $this
->t('of type'),
'#options' => $type_options,
'#prefix' => '<div id="' . $wrapper . '">',
'#suffix' => '</div>',
];
if (isset($input['show']['entity_type']) && $input['show']['entity_type'] != 'comment') {
$default_bundles = \Drupal::service('entity_type.bundle.info')
->getBundleInfo($input['show']['entity_type']);
if (!empty($default_bundles)) {
foreach ($default_bundles as $type => $bundle) {
$type_options[$type] = $bundle['label'];
}
$form['displays']['show']['type']['#options'] = $type_options;
}
}
$form['displays']['show']['comment_message'] = [
'#type' => 'fieldset',
'#markup' => $this
->t('<br>Note: bundle. (not supported in comment entity types) Refer this <a target="_blank" href="https://www.drupal.org/node/1343708">How to use EntityFieldQuery</a>.<br>'),
'#states' => [
'visible' => [
'select[name="show[entity_type]"]' => [
'value' => 'comment',
],
],
],
];
$form['message'] = [
'#markup' => $this
->t('Note: Use <b>ENTITY DELETE</b> only to delete Comment, Content, Log Entries, Taxonomy, User(s).<br>'),
];
if (\Drupal::moduleHandler()
->moduleExists('commerce')) {
$form['commerce_message'] = [
'#markup' => $this
->t('<br>And Also supports Commerce - Line Item, Product, Order, Product Attribute, Product Variation, Profile, Store</br>'),
];
}
$form['submit'] = [
'#type' => 'submit',
'#value' => 'Delete',
];
return $form;
}
public function ajaxCallChangeEntity(array &$form, FormStateInterface $form_state) {
return $form['displays']['show']['type'];
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$values = $form_state
->getValues();
$entity_type = $values['show']['entity_type'];
$bundle = $values['show']['type'];
$url = Url::fromRoute('entity_delete.entity_delete_confirmation', [
'entity_type' => $entity_type,
'bundle' => $bundle,
]);
$token = $this->csrfToken
->get($url
->getInternalPath());
$url
->setOptions([
'query' => [
'token' => $token,
],
]);
$form_state
->setRedirectUrl($url);
}
}