Skip to content

Commit 9fba523

Browse files
authored
Merge pull request #21 from digitalutsc/refactor/remove-phpcs-ignore-comments
refactor: remove phpcs errors and implement dependency injection for all services
2 parents 288b98f + ae7b47a commit 9fba523

2 files changed

Lines changed: 121 additions & 27 deletions

File tree

islandora_breadcrumbs.services.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
services:
22
islandora_breadcrumbs.breadcrumb:
33
class: Drupal\islandora_breadcrumbs\IslandoraBreadcrumbBuilder
4-
arguments: ["@entity_type.manager", "@config.factory"]
4+
arguments: ["@entity_type.manager", "@config.factory", "@path.matcher", "@path.current", "@router.admin_context", "@title_resolver", "@request_stack", "@entity.repository", "@language_manager", "@path_alias.manager"]
55
tags:
66
- { name: breadcrumb_builder, priority: 10001 }

src/IslandoraBreadcrumbBuilder.php

Lines changed: 120 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
<?php
22

3-
// phpcs:disable DrupalPractice.Objects.GlobalDrupal
4-
// phpcs:disable DrupalPractice.Objects.GlobalClass
5-
63
namespace Drupal\islandora_breadcrumbs;
74

85
use Drupal\views\Views;
@@ -13,8 +10,15 @@
1310
use Drupal\Core\Link;
1411
use Drupal\Core\Routing\RouteMatchInterface;
1512
use Drupal\Core\StringTranslation\StringTranslationTrait;
16-
use Drupal\taxonomy\Entity\Term;
1713
use Drupal\node\Entity\Node;
14+
use Drupal\Core\Path\PathMatcherInterface;
15+
use Drupal\Core\Path\CurrentPathStack;
16+
use Drupal\Core\Routing\AdminContext;
17+
use Drupal\Core\Controller\TitleResolverInterface;
18+
use Symfony\Component\HttpFoundation\RequestStack;
19+
use Drupal\Core\Entity\EntityRepositoryInterface;
20+
use Drupal\Core\Language\LanguageManagerInterface;
21+
use Drupal\path_alias\AliasManagerInterface;
1822

1923
/**
2024
* Provides breadcrumbs for nodes using a configured entity reference field.
@@ -36,24 +40,123 @@ class IslandoraBreadcrumbBuilder implements BreadcrumbBuilderInterface {
3640
*/
3741
protected $nodeStorage;
3842

43+
/**
44+
* Storage to load taxonomy terms.
45+
*
46+
* @var \Drupal\Core\Entity\EntityStorageInterface
47+
*/
48+
protected $termStorage;
49+
3950
/**
4051
* Check whether is type islandora object.
4152
*
4253
* @var bool
4354
*/
4455
public $isIslandora;
4556

57+
/**
58+
* The path matcher.
59+
*
60+
* @var \Drupal\Core\Path\PathMatcherInterface
61+
*/
62+
protected $pathMatcher;
63+
64+
/**
65+
* The current path service.
66+
*
67+
* @var \Drupal\Core\Path\CurrentPathStack
68+
*/
69+
protected $currentPath;
70+
71+
/**
72+
* The router admin context.
73+
*
74+
* @var \Drupal\Core\Routing\AdminContext
75+
*/
76+
protected $adminContext;
77+
78+
/**
79+
* The title resolver.
80+
*
81+
* @var \Drupal\Core\Controller\TitleResolverInterface
82+
*/
83+
protected $titleResolver;
84+
85+
/**
86+
* The request stack.
87+
*
88+
* @var \Symfony\Component\HttpFoundation\RequestStack
89+
*/
90+
protected $requestStack;
91+
92+
/**
93+
* The entity repository.
94+
*
95+
* @var \Drupal\Core\Entity\EntityRepositoryInterface
96+
*/
97+
protected $entityRepository;
98+
99+
/**
100+
* The language manager.
101+
*
102+
* @var \Drupal\Core\Language\LanguageManagerInterface
103+
*/
104+
protected $languageManager;
105+
106+
/**
107+
* The path alias manager.
108+
*
109+
* @var \Drupal\path_alias\AliasManagerInterface
110+
*/
111+
protected $pathAliasManager;
112+
46113
/**
47114
* Constructs a breadcrumb builder.
48115
*
49116
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_manager
50117
* Storage to load nodes.
51118
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
52119
* The configuration factory.
120+
* @param \Drupal\Core\Path\PathMatcherInterface $path_matcher
121+
* The path matcher.
122+
* @param \Drupal\Core\Path\CurrentPathStack $current_path
123+
* The current path service.
124+
* @param \Drupal\Core\Routing\AdminContext $admin_context
125+
* The router admin context.
126+
* @param \Drupal\Core\Controller\TitleResolverInterface $title_resolver
127+
* The title resolver.
128+
* @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
129+
* The request stack.
130+
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
131+
* The entity repository.
132+
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
133+
* The language manager.
134+
* @param \Drupal\path_alias\AliasManagerInterface $path_alias_manager
135+
* The path alias manager.
53136
*/
54-
public function __construct(EntityTypeManagerInterface $entity_manager, ConfigFactoryInterface $config_factory) {
137+
public function __construct(
138+
EntityTypeManagerInterface $entity_manager,
139+
ConfigFactoryInterface $config_factory,
140+
PathMatcherInterface $path_matcher,
141+
CurrentPathStack $current_path,
142+
AdminContext $admin_context,
143+
TitleResolverInterface $title_resolver,
144+
RequestStack $request_stack,
145+
EntityRepositoryInterface $entity_repository,
146+
LanguageManagerInterface $language_manager,
147+
AliasManagerInterface $path_alias_manager,
148+
) {
55149
$this->nodeStorage = $entity_manager->getStorage('node');
150+
$this->termStorage = $entity_manager->getStorage('taxonomy_term');
56151
$this->config = $config_factory->get('islandora_breadcrumbs.breadcrumbs');
152+
$this->pathMatcher = $path_matcher;
153+
$this->currentPath = $current_path;
154+
$this->adminContext = $admin_context;
155+
$this->titleResolver = $title_resolver;
156+
$this->requestStack = $request_stack;
157+
$this->entityRepository = $entity_repository;
158+
$this->languageManager = $language_manager;
159+
$this->pathAliasManager = $path_alias_manager;
57160
}
58161

59162
/**
@@ -63,7 +166,7 @@ public function applies(RouteMatchInterface $attributes) {
63166
// Using getRawParameters for consistency (always gives a
64167
// node ID string) because getParameters sometimes returns
65168
// a node ID string and sometimes returns a node object.
66-
if (\Drupal::service('path.matcher')->isFrontPage()) {
169+
if ($this->pathMatcher->isFrontPage()) {
67170
return FALSE;
68171
}
69172
$parameters = $attributes->getParameters()->all();
@@ -108,10 +211,7 @@ public function build(RouteMatchInterface $route_match) {
108211

109212
}
110213
elseif (isset($parameters['view_id'])) {
111-
$path = \Drupal::service('path.current')->getPath();
112-
$url_object = \Drupal::service('path.validator')->getUrlIfValid($path);
113-
// phpcs:ignore -- Unused variable $route_name.
114-
$route_name = $url_object->getRouteName();
214+
$path = $this->currentPath->getPath();
115215
$title = '';
116216
$path_elements = explode('/', $path);
117217
$nid = "";
@@ -120,7 +220,7 @@ public function build(RouteMatchInterface $route_match) {
120220

121221
if (intval($pe)) {
122222
// If it's node id.
123-
$node = $this->getTranslatedNode(Node::load($pe));
223+
$node = $this->getTranslatedNode($this->nodeStorage->load($pe));
124224
if (!is_null($node) && $this->nodeHasReferenceFields($node)) {
125225
$nid = $pe;
126226
// If islandora object.
@@ -129,8 +229,8 @@ public function build(RouteMatchInterface $route_match) {
129229
}
130230
}
131231

132-
// phpcs:ignore -- Line exceeds 80 characters; contains 86 characters
133-
// $title = str_replace(['-', '_'], ' ', Unicode::ucwords(end($path_elements)));
232+
// $title = str_replace(['-', '_'], ' ',
233+
// Unicode::ucwords(end($path_elements)));
134234
$view = Views::getView($parameters['view_id']);
135235
$view->setDisplay($parameters['display_id']);
136236
$view_title = $view->getTitle();
@@ -153,8 +253,8 @@ public function build(RouteMatchInterface $route_match) {
153253
}
154254

155255
// Add current page title to the breadcrumb.
156-
if ($this->config->get('includeSelf') && $breadcrumb && !\Drupal::service('router.admin_context')->isAdminRoute() && !\Drupal::service('path.matcher')->isFrontPage()) {
157-
$title = \Drupal::service('title_resolver')->getTitle(\Drupal::request(), $route_match->getRouteObject());
256+
if ($this->config->get('includeSelf') && $breadcrumb && !$this->adminContext->isAdminRoute() && !$this->pathMatcher->isFrontPage()) {
257+
$title = $this->titleResolver->getTitle($this->requestStack->getCurrentRequest(), $route_match->getRouteObject());
158258
if (!empty($title)) {
159259
$breadcrumb->addLink(Link::createFromRoute($title, '<none>'));
160260
}
@@ -183,8 +283,6 @@ protected function setReferenceBreadcrumbs(IslandoraBreadcrumb &$breadcrumb, ?No
183283

184284
// Check referenced fields for members.
185285
foreach ($referenced_entities as $referenced_entity) {
186-
// phpcs:ignore -- Unused variable $link.
187-
$link = $referenced_entity->toLink()->toString()->getGeneratedLink();
188286
$node = $this->extractNode($referenced_entity);
189287
$refs = $this->getReferencedEntities($node);
190288
if (count($refs) > 0) {
@@ -198,9 +296,7 @@ protected function setReferenceBreadcrumbs(IslandoraBreadcrumb &$breadcrumb, ?No
198296
$breadcrumb->addLinkSet();
199297
}
200298
foreach ($referenced_entities as $referenced_entity) {
201-
$referenced_entity = \Drupal::service('entity.repository')->getTranslationFromContext($referenced_entity);
202-
// phpcs:ignore -- Unused variable $link.
203-
$link = $referenced_entity->toLink()->toString()->getGeneratedLink();
299+
$referenced_entity = $this->entityRepository->getTranslationFromContext($referenced_entity);
204300
$node = $this->extractNode($referenced_entity);
205301

206302
if ($node != NULL) {
@@ -261,9 +357,7 @@ protected function nodeHasReferenceFields(Node $node) {
261357
* Link representing node.
262358
*/
263359
protected function getViewLink(Node $node) {
264-
// phpcs:ignore -- Unused variable $nid.
265-
$nid = $node->id();
266-
if (Term::load($node->get('field_model')->target_id)->get('name')->value === "Paged Content") {
360+
if ($this->termStorage->load($node->get('field_model')->target_id)->get('name')->value === "Paged Content") {
267361
return Link::createFromRoute($node->getTitle(), "entity.node.canonical", ['node' => $node->id()]);
268362
}
269363
else {
@@ -284,7 +378,7 @@ protected function getTranslatedNode(?Node $node = NULL) {
284378
if (is_null($node)) {
285379
return NULL;
286380
}
287-
$langcode = \Drupal::languageManager()->getCurrentLanguage()->getId();
381+
$langcode = $this->languageManager->getCurrentLanguage()->getId();
288382
if ($node->hasTranslation($langcode)) {
289383
$node = $node->getTranslation($langcode);
290384
}
@@ -312,13 +406,13 @@ protected function extractNode(EntityInterface $entity) {
312406
$node_matched = preg_match('/node\/(\d+)/', $node_url, $matches);
313407
if ($node_matched === 0) {
314408
// Add to handle node id with alias (ark url)
315-
$path = \Drupal::service('path_alias.manager')->getPathByAlias(urldecode($node_url));
409+
$path = $this->pathAliasManager->getPathByAlias(urldecode($node_url));
316410
$node_matched = preg_match('/node\/(\d+)/', $path, $matches);
317411
}
318412

319413
if ($node_matched) {
320414
$nid = $matches[1];
321-
$node = Node::load($nid);
415+
$node = $this->nodeStorage->load($nid);
322416
return $this->getTranslatedNode($node);
323417
}
324418
}

0 commit comments

Comments
 (0)