Commit 6241c57f authored by catch's avatar catch

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
...@@ -16,11 +16,11 @@ class CommentBreadcrumbBuilder implements BreadcrumbBuilderInterface { ...@@ -16,11 +16,11 @@ class CommentBreadcrumbBuilder implements BreadcrumbBuilderInterface {
use StringTranslationTrait; 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. * Constructs the CommentBreadcrumbBuilder.
...@@ -29,7 +29,7 @@ class CommentBreadcrumbBuilder implements BreadcrumbBuilderInterface { ...@@ -29,7 +29,7 @@ class CommentBreadcrumbBuilder implements BreadcrumbBuilderInterface {
* The entity type manager. * The entity type manager.
*/ */
public function __construct(EntityTypeManagerInterface $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) { ...@@ -51,7 +51,7 @@ public function build(RouteMatchInterface $route_match) {
$breadcrumb->addLink(new Link($entity->label(), $entity->toUrl())); $breadcrumb->addLink(new Link($entity->label(), $entity->toUrl()));
$breadcrumb->addCacheableDependency($entity); $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 */ /** @var \Drupal\comment\CommentInterface $comment */
$breadcrumb->addCacheableDependency($comment); $breadcrumb->addCacheableDependency($comment);
// Display link to parent comment. // Display link to parent comment.
......
...@@ -2,8 +2,6 @@ ...@@ -2,8 +2,6 @@
namespace Drupal\node\Plugin\views\row; namespace Drupal\node\Plugin\views\row;
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\views\Plugin\views\row\RssPluginBase; use Drupal\views\Plugin\views\row\RssPluginBase;
/** /**
...@@ -35,32 +33,6 @@ class Rss extends RssPluginBase { ...@@ -35,32 +33,6 @@ class Rss extends RssPluginBase {
*/ */
protected $entityTypeId = 'node'; 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} * {@inheritdoc}
*/ */
...@@ -82,7 +54,7 @@ public function preRender($values) { ...@@ -82,7 +54,7 @@ public function preRender($values) {
$nids[] = $row->{$this->field_alias}; $nids[] = $row->{$this->field_alias};
} }
if (!empty($nids)) { if (!empty($nids)) {
$this->nodes = $this->nodeStorage->loadMultiple($nids); $this->nodes = $this->entityTypeManager->getStorage('node')->loadMultiple($nids);
} }
} }
......
...@@ -30,13 +30,6 @@ class TermBreadcrumbBuilder implements BreadcrumbBuilderInterface { ...@@ -30,13 +30,6 @@ class TermBreadcrumbBuilder implements BreadcrumbBuilderInterface {
*/ */
protected $entityTypeManager; protected $entityTypeManager;
/**
* The taxonomy storage.
*
* @var \Drupal\taxonomy\TermStorageInterface
*/
protected $termStorage;
/** /**
* Constructs the TermBreadcrumbBuilder. * Constructs the TermBreadcrumbBuilder.
* *
...@@ -47,7 +40,6 @@ class TermBreadcrumbBuilder implements BreadcrumbBuilderInterface { ...@@ -47,7 +40,6 @@ class TermBreadcrumbBuilder implements BreadcrumbBuilderInterface {
*/ */
public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityRepositoryInterface $entity_repository) { public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityRepositoryInterface $entity_repository) {
$this->entityTypeManager = $entity_type_manager; $this->entityTypeManager = $entity_type_manager;
$this->termStorage = $entity_type_manager->getStorage('taxonomy_term');
$this->entityRepository = $entity_repository; $this->entityRepository = $entity_repository;
} }
...@@ -73,7 +65,7 @@ public function build(RouteMatchInterface $route_match) { ...@@ -73,7 +65,7 @@ public function build(RouteMatchInterface $route_match) {
// @todo This overrides any other possible breadcrumb and is a pure // @todo This overrides any other possible breadcrumb and is a pure
// hard-coded presumption. Make this behavior configurable per // hard-coded presumption. Make this behavior configurable per
// vocabulary or term. // vocabulary or term.
$parents = $this->termStorage->loadAllParents($term->id()); $parents = $this->entityTypeManager->getStorage('taxonomy_term')->loadAllParents($term->id());
// Remove current term being accessed. // Remove current term being accessed.
array_shift($parents); array_shift($parents);
foreach (array_reverse($parents) as $term) { foreach (array_reverse($parents) as $term) {
......
...@@ -95,7 +95,6 @@ function taxonomy_tokens($type, $tokens, array $data, array $options, Bubbleable ...@@ -95,7 +95,6 @@ function taxonomy_tokens($type, $tokens, array $data, array $options, Bubbleable
$token_service = \Drupal::token(); $token_service = \Drupal::token();
$replacements = []; $replacements = [];
$taxonomy_storage = \Drupal::entityTypeManager()->getStorage('taxonomy_term');
if ($type == 'term' && !empty($data['term'])) { if ($type == 'term' && !empty($data['term'])) {
$term = $data['term']; $term = $data['term'];
...@@ -134,6 +133,7 @@ function taxonomy_tokens($type, $tokens, array $data, array $options, Bubbleable ...@@ -134,6 +133,7 @@ function taxonomy_tokens($type, $tokens, array $data, array $options, Bubbleable
break; break;
case 'parent': case 'parent':
$taxonomy_storage = \Drupal::entityTypeManager()->getStorage('taxonomy_term');
if ($parents = $taxonomy_storage->loadParents($term->id())) { if ($parents = $taxonomy_storage->loadParents($term->id())) {
$parent = array_pop($parents); $parent = array_pop($parents);
$bubbleable_metadata->addCacheableDependency($parent); $bubbleable_metadata->addCacheableDependency($parent);
...@@ -148,9 +148,12 @@ function taxonomy_tokens($type, $tokens, array $data, array $options, Bubbleable ...@@ -148,9 +148,12 @@ function taxonomy_tokens($type, $tokens, array $data, array $options, Bubbleable
$replacements += $token_service->generate('vocabulary', $vocabulary_tokens, ['vocabulary' => $vocabulary], $options, $bubbleable_metadata); $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'))) {
$parent = array_pop($parents); $taxonomy_storage = \Drupal::entityTypeManager()->getStorage('taxonomy_term');
$replacements += $token_service->generate('term', $vocabulary_tokens, ['term' => $parent], $options, $bubbleable_metadata); if ($parents = $taxonomy_storage->loadParents($term->id())) {
$parent = array_pop($parents);
$replacements += $token_service->generate('term', $vocabulary_tokens, ['term' => $parent], $options, $bubbleable_metadata);
}
} }
} }
...@@ -182,6 +185,7 @@ function taxonomy_tokens($type, $tokens, array $data, array $options, Bubbleable ...@@ -182,6 +185,7 @@ function taxonomy_tokens($type, $tokens, array $data, array $options, Bubbleable
break; break;
case 'node-count': case 'node-count':
$taxonomy_storage = \Drupal::entityTypeManager()->getStorage('taxonomy_term');
$replacements[$original] = $taxonomy_storage->nodeCount($vocabulary->id()); $replacements[$original] = $taxonomy_storage->nodeCount($vocabulary->id());
break; break;
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment