-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDomainPathautoGenerator.php
More file actions
executable file
·207 lines (180 loc) · 6.68 KB
/
Copy pathDomainPathautoGenerator.php
File metadata and controls
executable file
·207 lines (180 loc) · 6.68 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
namespace Drupal\domain_path_pathauto;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityMalformedException;
use Drupal\Core\Entity\Exception\UndefinedLinkTemplateException;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\pathauto\PathautoGenerator;
use Drupal\pathauto\PathautoGeneratorInterface;
/**
* Provides methods for generating domain path aliases.
* @see \Drupal\pathauto\PathautoGenerator
*
* For now, only op "return" is supported.
*/
class DomainPathautoGenerator extends PathautoGenerator {
/**
* {@inheritdoc}
*/
public function createEntityAlias(EntityInterface $entity, $op, $domain_id = '') {
// Retrieve and apply the pattern for this content type.
$pattern = $this->getPatternByEntity($entity);
if (empty($pattern)) {
// No pattern? Do nothing (otherwise we may blow away existing aliases...)
return NULL;
}
try {
$internalPath = $entity->toUrl()->getInternalPath();
}
// @todo convert to multi-exception handling in PHP 7.1.
catch (EntityMalformedException $exception) {
return NULL;
}
catch (UndefinedLinkTemplateException $exception) {
return NULL;
}
catch (\UnexpectedValueException $exception) {
return NULL;
}
$source = '/' . $internalPath;
$config = $this->configFactory->get('pathauto.settings');
$langcode = $entity->language()->getId();
// Core does not handle aliases with language Not Applicable.
if ($langcode == LanguageInterface::LANGCODE_NOT_APPLICABLE) {
$langcode = LanguageInterface::LANGCODE_NOT_SPECIFIED;
}
// Build token data.
$data = [
$this->tokenEntityMapper->getTokenTypeForEntityType($entity->getEntityTypeId()) => $entity,
];
// Allow other modules to alter the pattern.
$context = [
'module' => $entity->getEntityType()->getProvider(),
'op' => $op,
'source' => $source,
'data' => $data,
'bundle' => $entity->bundle(),
'language' => &$langcode,
'domain_id' => $domain_id,
];
$pattern_original = $pattern->getPattern();
$this->moduleHandler->alter('pathauto_pattern', $pattern, $context);
$pattern_altered = $pattern->getPattern();
// Special handling when updating an item which is already aliased.
$existing_alias = NULL;
if ($op == 'update' || $op == 'bulkupdate') {
if ($existing_alias = $this->aliasStorageHelper->loadBySource($source, $langcode)) {
switch ($config->get('update_action')) {
case PathautoGeneratorInterface::UPDATE_ACTION_NO_NEW:
// If an alias already exists,
// and the update action is set to do nothing,
// then gosh-darn it, do nothing.
return NULL;
}
}
}
// Replace any tokens in the pattern.
// Uses callback option to clean replacements. No sanitization.
// Pass empty BubbleableMetadata object to explicitly ignore cacheablity,
// as the result is never rendered.
$alias = $this->token->replace($pattern->getPattern(), $data, [
'clear' => TRUE,
'callback' => [$this->aliasCleaner, 'cleanTokenValues'],
'langcode' => $langcode,
'pathauto' => TRUE,
], new BubbleableMetadata());
// Check if the token replacement has not actually replaced any values. If
// that is the case, then stop because we should not generate an alias.
// @see token_scan()
$pattern_tokens_removed = preg_replace('/\[[^\s\]:]*:[^\s\]]*\]/', '', $pattern->getPattern());
if ($alias === $pattern_tokens_removed) {
return NULL;
}
$alias = $this->aliasCleaner->cleanAlias($alias);
// Allow other modules to alter the alias.
$context['source'] = &$source;
$context['pattern'] = $pattern;
$this->moduleHandler->alter('pathauto_alias', $alias, $context);
// If we have arrived at an empty string, discontinue.
if (!mb_strlen($alias)) {
return NULL;
}
// If the alias already exists, generate a new, hopefully unique, variant.
$original_alias = $alias;
$this->aliasUniquifier->uniquify($alias, $source, $langcode, $domain_id);
if ($original_alias != $alias) {
// Alert the user why this happened.
$this->pathautoMessenger->addMessage($this->t('The automatically generated alias %original_alias conflicted with an existing alias. Alias changed to %alias.', [
'%original_alias' => $original_alias,
'%alias' => $alias,
]), $op);
}
// Return the generated alias if requested.
if ($op == 'return') {
return $alias;
}
// Build the new path alias array and send it off to be created.
$path = [
'source' => $source,
'alias' => $alias,
'language' => $langcode,
];
// TODO: allow
$return = $this->aliasStorageHelper->save($path, $existing_alias, $op);
// Because there is no way to set an altered pattern to not be cached,
// change it back to the original value.
if ($pattern_altered !== $pattern_original) {
$pattern->setPattern($pattern_original);
}
return $return;
}
/**
* Check if "Generate URL alias" is enabled for entity/domain combination.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* @param string $domain_id
*
* @return boolean
* Return TRUE or FALSE.
*/
public function domainPathPathautoGenerationIsEnabled(EntityInterface $entity, $domain_id) {
$collection = $this->getDomainPathPathautoStateCollection($entity, $domain_id);
$value = \Drupal::keyValue($collection)->get($entity->id());
if($value === NULL) {
return FALSE;
}
return $value;
}
/**
* Returns the key value collection for the given entity/domain combination.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* @param string $domain_id
*
* @return string
*/
protected function getDomainPathPathautoStateCollection(EntityInterface $entity, $domain_id) {
return 'domain_path_pathauto_state.' . $domain_id . '.' . $entity->getEntityTypeId();
}
/**
* Set the stored state.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* @param string $domain_id
*/
public function setDomainPathPathautoState($entity, $domain_id, $value) {
$collection = $this->getDomainPathPathautoStateCollection($entity, $domain_id);
\Drupal::keyValue($collection)->set($entity->id(), $value);
}
/**
* Deletes the stored state.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* @param string $domain_id
*/
public function deleteDomainPathPathautoState($entity, $domain_id) {
$collection = $this->getDomainPathPathautoStateCollection($entity, $domain_id);
\Drupal::keyValue($collection)->delete($entity->id());
}
}