Commit 6241c57f authored by catch's avatar catch
Browse files

Issue #3162827 by Berdir, Spokje, claudiu.cristea, daffie: Do not instantiate...

Issue #3162827 by Berdir, Spokje, claudiu.cristea, daffie: Do not instantiate entity storages in constructors of services that do not always need them
parent 685419fa
Loading
Loading
Loading
Loading
+5 −5
Original line number Diff line number Diff line
@@ -16,11 +16,11 @@ class CommentBreadcrumbBuilder implements BreadcrumbBuilderInterface {
  use StringTranslationTrait;

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

  /**
   * Constructs the CommentBreadcrumbBuilder.
@@ -29,7 +29,7 @@ class CommentBreadcrumbBuilder implements BreadcrumbBuilderInterface {
   *   The entity type manager.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager) {
    $this->storage = $entity_type_manager->getStorage('comment');
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
@@ -51,7 +51,7 @@ public function build(RouteMatchInterface $route_match) {
    $breadcrumb->addLink(new Link($entity->label(), $entity->toUrl()));
    $breadcrumb->addCacheableDependency($entity);

    if (($pid = $route_match->getParameter('pid')) && ($comment = $this->storage->load($pid))) {
    if (($pid = $route_match->getParameter('pid')) && ($comment = $this->entityTypeManager->getStorage('comment')->load($pid))) {
      /** @var \Drupal\comment\CommentInterface $comment */
      $breadcrumb->addCacheableDependency($comment);
      // Display link to parent comment.
+1 −29
Original line number Diff line number Diff line
@@ -2,8 +2,6 @@

namespace Drupal\node\Plugin\views\row;

use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\views\Plugin\views\row\RssPluginBase;

/**
@@ -35,32 +33,6 @@ class Rss extends RssPluginBase {
   */
  protected $entityTypeId = 'node';

  /**
   * The node storage.
   *
   * @var \Drupal\node\NodeStorageInterface
   */
  protected $nodeStorage;

  /**
   * Constructs the Rss object.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\Entity\EntityDisplayRepositoryInterface $entity_display_repository
   *   The entity display repository.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityDisplayRepositoryInterface $entity_display_repository = NULL) {
    parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager, $entity_display_repository);
    $this->nodeStorage = $entity_type_manager->getStorage('node');
  }

  /**
   * {@inheritdoc}
   */
@@ -82,7 +54,7 @@ public function preRender($values) {
      $nids[] = $row->{$this->field_alias};
    }
    if (!empty($nids)) {
      $this->nodes = $this->nodeStorage->loadMultiple($nids);
      $this->nodes = $this->entityTypeManager->getStorage('node')->loadMultiple($nids);
    }
  }

+1 −9
Original line number Diff line number Diff line
@@ -30,13 +30,6 @@ class TermBreadcrumbBuilder implements BreadcrumbBuilderInterface {
   */
  protected $entityTypeManager;

  /**
   * The taxonomy storage.
   *
   * @var \Drupal\taxonomy\TermStorageInterface
   */
  protected $termStorage;

  /**
   * Constructs the TermBreadcrumbBuilder.
   *
@@ -47,7 +40,6 @@ class TermBreadcrumbBuilder implements BreadcrumbBuilderInterface {
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityRepositoryInterface $entity_repository) {
    $this->entityTypeManager = $entity_type_manager;
    $this->termStorage = $entity_type_manager->getStorage('taxonomy_term');
    $this->entityRepository = $entity_repository;
  }

@@ -73,7 +65,7 @@ public function build(RouteMatchInterface $route_match) {
    // @todo This overrides any other possible breadcrumb and is a pure
    //   hard-coded presumption. Make this behavior configurable per
    //   vocabulary or term.
    $parents = $this->termStorage->loadAllParents($term->id());
    $parents = $this->entityTypeManager->getStorage('taxonomy_term')->loadAllParents($term->id());
    // Remove current term being accessed.
    array_shift($parents);
    foreach (array_reverse($parents) as $term) {
+8 −4
Original line number Diff line number Diff line
@@ -95,7 +95,6 @@ function taxonomy_tokens($type, $tokens, array $data, array $options, Bubbleable
  $token_service = \Drupal::token();

  $replacements = [];
  $taxonomy_storage = \Drupal::entityTypeManager()->getStorage('taxonomy_term');
  if ($type == 'term' && !empty($data['term'])) {
    $term = $data['term'];

@@ -134,6 +133,7 @@ function taxonomy_tokens($type, $tokens, array $data, array $options, Bubbleable
          break;

        case 'parent':
          $taxonomy_storage = \Drupal::entityTypeManager()->getStorage('taxonomy_term');
          if ($parents = $taxonomy_storage->loadParents($term->id())) {
            $parent = array_pop($parents);
            $bubbleable_metadata->addCacheableDependency($parent);
@@ -148,11 +148,14 @@ function taxonomy_tokens($type, $tokens, array $data, array $options, Bubbleable
      $replacements += $token_service->generate('vocabulary', $vocabulary_tokens, ['vocabulary' => $vocabulary], $options, $bubbleable_metadata);
    }

    if (($vocabulary_tokens = $token_service->findWithPrefix($tokens, 'parent')) && $parents = $taxonomy_storage->loadParents($term->id())) {
    if (($vocabulary_tokens = $token_service->findWithPrefix($tokens, 'parent'))) {
      $taxonomy_storage = \Drupal::entityTypeManager()->getStorage('taxonomy_term');
      if ($parents = $taxonomy_storage->loadParents($term->id())) {
        $parent = array_pop($parents);
        $replacements += $token_service->generate('term', $vocabulary_tokens, ['term' => $parent], $options, $bubbleable_metadata);
      }
    }
  }

  elseif ($type == 'vocabulary' && !empty($data['vocabulary'])) {
    $vocabulary = $data['vocabulary'];
@@ -182,6 +185,7 @@ function taxonomy_tokens($type, $tokens, array $data, array $options, Bubbleable
          break;

        case 'node-count':
          $taxonomy_storage = \Drupal::entityTypeManager()->getStorage('taxonomy_term');
          $replacements[$original] = $taxonomy_storage->nodeCount($vocabulary->id());
          break;
      }