Commit 79199e47 authored by Akshay Kelotra's avatar Akshay Kelotra Committed by NGUYEN Bao
Browse files

Issue #3310638 by akshay.kelotra: \Drupal calls should be avoided in classes,...

Issue #3310638 by akshay.kelotra: \Drupal calls should be avoided in classes, use dependency injection instead
parent 45009d6e
Loading
Loading
Loading
Loading
+25 −6
Original line number Diff line number Diff line
@@ -12,6 +12,7 @@ use Drupal\paragraphs\ParagraphsTypeInterface;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;

/**
 * Class AddComponentForm.
@@ -41,6 +42,22 @@ class AddComponentForm extends FormBase {
   */
  protected $entityTypeManager;

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

  /**
   * AddComponentForm.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager service.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler service.
   */

  /**
   * {@inheritdoc}
   */
@@ -51,8 +68,9 @@ class AddComponentForm extends FormBase {
  /**
   * {@inheritDoc}
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager) {
  public function __construct(EntityTypeManagerInterface $entity_type_manager, ModuleHandlerInterface $module_handler) {
    $this->entityTypeManager = $entity_type_manager;
    $this->moduleHandler = $module_handler;
  }

  /**
@@ -61,6 +79,7 @@ class AddComponentForm extends FormBase {
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('entity_type.manager'),
      $container->get('module_handler')
    );
  }

@@ -78,7 +97,7 @@ class AddComponentForm extends FormBase {
    $form_state->set('field_name', $paragraph_type->id);
    $form_state->set('entity_field', $entity_field);
    if (!$form_state->has('entity')) {
      $entity = \Drupal::entityTypeManager()
      $entity = $this->entityTypeManager()
        ->getStorage($entity_type)
        ->load($entity_id);
      $form_state->set('entity', $entity);
@@ -127,7 +146,7 @@ class AddComponentForm extends FormBase {

    // Support for Field Group module based on Paragraphs module.
    // @todo Remove as part of https://www.drupal.org/node/2640056
    if (\Drupal::service('module_handler')->moduleExists('field_group')) {
    if ($this->moduleHandler('module_handler')->moduleExists('field_group')) {
      $context = [
        'entity_type' => $this->paragraph->getEntityTypeId(),
        'bundle' => $this->paragraph->bundle(),
@@ -224,10 +243,10 @@ class AddComponentForm extends FormBase {
   *   The new paragraph.
   */
  protected function newParagraph(ParagraphsTypeInterface $paragraph_type) {
    $entity_type = \Drupal::entityTypeManager()->getDefinition('paragraph');
    $entity_type = $this->entityTypeManager()->getDefinition('paragraph');
    $bundle_key = $entity_type->getKey('bundle');
    /** @var \Drupal\paragraphs\ParagraphInterface $paragraph_entity */
    return \Drupal::entityTypeManager()->getStorage('paragraph')
    return $this->entityTypeManager()->getStorage('paragraph')
      ->create([$bundle_key => $paragraph_type->id()]);
  }

@@ -280,7 +299,7 @@ class AddComponentForm extends FormBase {
      // language they are displayed. This allows to keep contextual editing
      // working also for multilingual entities.
      $form_state->set('langcode',
        \Drupal::service('entity.repository')
      $this->moduleHandler('entity.repository')
          ->getTranslationFromContext($this->paragraph)
          ->language()
          ->getId());