Commit bc30f53e authored by Jordan Barnes's avatar Jordan Barnes Committed by Sang Lostrie
Browse files

Issue #3258058 by j-barnes, hmendes, tmaiochi, baikho: Fix Dependency...

Issue #3258058 by j-barnes, hmendes, tmaiochi, baikho: Fix Dependency Injection problems in the module
parent aeb6295a
Loading
Loading
Loading
Loading
+56 −5
Original line number Diff line number Diff line
@@ -3,8 +3,12 @@
namespace Drupal\entity_delete\Form;

use Drupal\Core\Database\Database;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Entity\EntityDefinitionUpdateManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;

/**
 * Class EntityDeleteConfirmationForm.
@@ -12,6 +16,53 @@ use Drupal\Core\Form\FormStateInterface;
 * @package Drupal\entity_delete\Form
 */
class EntityDeleteConfirmationForm extends FormBase {
  /**
   * The entity definition update manager.
   *
   * @var \Drupal\Core\Entity\EntityDefinitionUpdateManagerInterface
   */
  private $entityDefinitionUpdateManager;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The messenger.
   *
   * @var \Drupal\Core\Messenger\MessengerInterface
   */
  protected $messenger;

  /**
   * Constructs a new EntityDeleteConfirmationForm object.
   *
   * @param \Drupal\Core\Entity\EntityDefinitionUpdateManagerInterface $entity_definition_update_manager
   *   The entity definition update manager.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\Messenger\MessengerInterface $messenger
   *   The messenger.
   */
  public function __construct(EntityDefinitionUpdateManagerInterface $entity_definition_update_manager, EntityTypeManagerInterface $entity_type_manager, MessengerInterface $messenger) {
    $this->entityDefinitionUpdateManager = $entity_definition_update_manager;
    $this->entityTypeManager = $entity_type_manager;
    $this->messenger = $messenger;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('entity.definition_update_manager'),
      $container->get('entity_type.manager'),
      $container->get('messenger')
    );
  }

  /**
   * {@inheritdoc}
@@ -43,7 +94,7 @@ class EntityDeleteConfirmationForm extends FormBase {
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $query = \Drupal::request()->query;
    $query = $this->getRequest()->query;
    $entity_type = $query->get('entity_type');
    $bundle = $query->get('bundle');

@@ -57,7 +108,7 @@ class EntityDeleteConfirmationForm extends FormBase {
        $conn = Database::getConnection();
        $truncate = $conn->truncate('watchdog');
        $truncate->execute();
        \Drupal::messenger()
        $this->messenger
          ->addMessage($this->t('Log Entries Cleared Successfully'));
      }
      else {
@@ -71,7 +122,7 @@ class EntityDeleteConfirmationForm extends FormBase {
          }
        }
        // Build entity query.
        $entity_query = \Drupal::entityQuery($entity_type);
        $entity_query = $this->entityTypeManager->getStorage($entity_type)->getQuery();
        $batch = [
          'title' => $this->t('Deleting @entity_type...', [
            '@entity_type' => $entity_type,
@@ -89,7 +140,7 @@ class EntityDeleteConfirmationForm extends FormBase {
          // Get entity bundle.
          $exclude_entities = ['file', 'comment', 'user', 'watchdog'];
          if (!in_array($entity_type, $exclude_entities)) {
            $manager = \Drupal::entityDefinitionUpdateManager();
            $manager = $this->entityDefinitionUpdateManager;
            $entity_type_load = $manager->getEntityType($entity_type);
            $entity_keys = $entity_type_load->getKeys();
            $bundle_type = $entity_keys['bundle'];
@@ -125,7 +176,7 @@ class EntityDeleteConfirmationForm extends FormBase {
          $form_state->setRedirect('entity_delete.entity_delete_bulk');
        }
        else {
          \Drupal::messenger()
          $this->messenger
            ->addMessage($this->t('No @entity(s) found to delete.', [
              '@bundle' => $bundle,
              '@entity' => $entity_type,
+28 −5
Original line number Diff line number Diff line
@@ -4,9 +4,11 @@ namespace Drupal\entity_delete\Form;

use Drupal\Core\Access\CsrfTokenGenerator;
use Drupal\Core\Entity\ContentEntityType;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;

@@ -38,6 +40,20 @@ class EntityDeleteForm extends FormBase {
   */
  protected $csrfToken;

  /**
   * The entity type bundle info.
   *
   * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
   */
  protected $entityTypeBundleInfo;

  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * EntityDeleteForm constructor.
   *
@@ -45,10 +61,16 @@ class EntityDeleteForm extends FormBase {
   *   Entity Delete Constructor.
   * @param \Drupal\Core\Access\CsrfTokenGenerator $csrf_token
   *   CSRF token generator.
   * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
   *   The entity type bundle info.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, CsrfTokenGenerator $csrf_token) {
  public function __construct(EntityTypeManagerInterface $entity_type_manager, CsrfTokenGenerator $csrf_token, EntityTypeBundleInfoInterface $entity_type_bundle_info, ModuleHandlerInterface $module_handler) {
    $this->entityTypeManager = $entity_type_manager;
    $this->csrfToken = $csrf_token;
    $this->entityTypeBundleInfo = $entity_type_bundle_info;
    $this->moduleHandler = $module_handler;
  }

  /**
@@ -63,7 +85,9 @@ class EntityDeleteForm extends FormBase {
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('entity_type.manager'),
      $container->get('csrf_token')
      $container->get('csrf_token'),
      $container->get('entity_type.bundle.info'),
      $container->get('module_handler')
    );
  }

@@ -111,8 +135,7 @@ class EntityDeleteForm extends FormBase {
      '#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']);
      $default_bundles = $this->entityTypeBundleInfo->getBundleInfo($input['show']['entity_type']);
      /*If the current base table support bundles and has more than one (like user).*/
      if (!empty($default_bundles)) {
        // Get all bundles and their human readable names.
@@ -134,7 +157,7 @@ class EntityDeleteForm extends FormBase {
    $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')) {
    if ($this->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>'),
      ];