forked from VolantisDev/drupal-facets-hardcode
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfacets_hardcode.module
More file actions
146 lines (127 loc) · 4.22 KB
/
facets_hardcode.module
File metadata and controls
146 lines (127 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
/**
* @file
* Contains facets_hardcode.module.
*/
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Url;
use Drupal\facets_hardcode\FacetsHardcodePathHelper;
use Drupal\facets_hardcode\FacetsHardcodeMetatagHelper;
/**
* Implements hook_preprocess_views_pager_with_summary().
*/
function facets_hardcode_preprocess_views_pager_with_summary(array &$variables) {
if (FacetsHardcodePathHelper::isFacetPath()) {
$faceted_path = FacetsHardcodePathHelper::getFacetedPath();
$base_path = FacetsHardcodePathHelper::filterFacetsFromPath($faceted_path);
if ($faceted_path != $base_path) {
if (isset($variables['items']['pages'])) {
foreach ($variables['items']['pages'] as $index => $page) {
$variables['items']['pages'][$index]['href'] = str_replace($base_path, $faceted_path, $page['href']);
}
}
foreach (['next', 'last', 'first', 'previous'] as $key) {
if (isset($variables['items'][$key])) {
$variables['items'][$key]['href'] = str_replace($base_path, $faceted_path, $variables['items'][$key]['href']);
}
}
}
}
}
/**
* Implements hook_entity_view_alter().
*/
function facets_hardcode_entity_view_alter(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display) {
FacetsHardcodeMetatagHelper::updateCanonicalUrls($build);
}
/**
* Implements hook_preprocess_html().
*/
function facets_hardcode_preprocess_html(array &$variables) {
$facet_path = FacetsHardcodePathHelper::getFacetedPath();
FacetsHardcodeMetatagHelper::setRobotsMetatag($variables['page'], $facet_path);
}
/**
* Implements hook_link_alter().
*
* Checks for links starting with /facets-hardcode/ and outputs a valid facet
* URL for the site in the correct language.
*/
function facets_hardcode_link_alter(&$variables) {
/** @var \Drupal\Core\Url $url */
$url = $variables['url'];
if (!$url->isRouted()) {
$path = $url->toString();
if (strpos($path, '/facets-hardcode/') === 0) {
$uri = facets_hardcode_replace_path($path);
$variables['url'] = Url::fromUri($uri);
}
}
}
/**
* Implements hook_preprocess_menu().
*/
function facets_hardcode_preprocess_menu(&$variables) {
if (isset($variables['items'])) {
$variables['items'] = facets_hardcode_replace_menu_items($variables['items']);
}
}
/**
* Replace menu link item below.
*
* @param array $items
* The items to replace the url.
*
* @return array
* The items with the url replaced.
*/
function facets_hardcode_replace_menu_items($items) {
foreach ($items as $key => $item) {
if (isset($item['url']) && !$item['url']->isRouted()) {
$path = $item['url']->toString();
if (strpos($path, '/facets-hardcode/') === 0) {
$uri = facets_hardcode_replace_path($path);
$items[$key]['url'] = Url::fromUri($uri);
}
}
if (!empty($item['below'])) {
$items[$key]['below'] = facets_hardcode_replace_menu_items($item['below']);
}
}
return $items;
}
/**
* Replace 'facets-hardcode' in links.
*
* @param string $path
* The original path.
*
* @return string
* The replaced path.
*/
function facets_hardcode_replace_path(string $path) {
$config = \Drupal::config('facets_hardcode.settings');
$identifier = $config->get('dynamic_facets_url_identifier') ?? 'f';
$path = str_replace('/facets-hardcode', '', $path);
$facets = '';
if (strpos($path, "/$identifier/") !== FALSE) {
[$path, $facets] = explode("/$identifier/", $path);
}
/** @var \Drupal\path_alias\AliasManagerInterface $aliasManager */
$aliasManager = \Drupal::service('path_alias.manager');
$languageManager = \Drupal::languageManager();
$currentLanguage = $languageManager->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->getId();
$path = $aliasManager->getPathByAlias($path, $languageManager->getDefaultLanguage()->getId());
$path = $aliasManager->getAliasByPath($path, $currentLanguage);
$uri = \Drupal::request()->getSchemeAndHttpHost();
if ($languageManager->getDefaultLanguage()->getId() !== $currentLanguage) {
$uri .= "/$currentLanguage";
}
$uri .= $path;
if ($facets) {
$uri .= "/$identifier/$facets";
}
return $uri;
}