-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDomainAliasUniquifier.php
More file actions
executable file
·84 lines (69 loc) · 2.57 KB
/
Copy pathDomainAliasUniquifier.php
File metadata and controls
executable file
·84 lines (69 loc) · 2.57 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
<?php
namespace Drupal\domain_path_pathauto;
use Drupal\Core\Language\LanguageInterface;
use Drupal\pathauto\AliasUniquifier;
use Drupal\Component\Utility\Unicode;
/**
* Provides a utility for creating a unique domain path alias.
*/
class DomainAliasUniquifier extends AliasUniquifier {
/**
* {@inheritdoc}
*/
public function uniquify(&$alias, $source, $langcode, $domain_id = '') {
$config = $this->configFactory->get('pathauto.settings');
if (!$this->isReserved($alias, $source, $langcode, $domain_id)) {
return;
}
// If the alias already exists, generate a new, hopefully unique, variant.
$maxlength = min($config->get('max_length'), $this->aliasStorageHelper->getAliasSchemaMaxlength());
$separator = $config->get('separator');
$original_alias = $alias;
$i = 0;
do {
// Append an incrementing numeric suffix until we find a unique alias.
$unique_suffix = $separator . $i;
$alias = Unicode::truncate($original_alias, $maxlength - mb_strlen($unique_suffix), TRUE) . $unique_suffix;
$i++;
} while ($this->isReserved($alias, $source, $langcode));
}
/**
* {@inheritdoc}
*/
public function isReserved($alias, $source, $langcode = LanguageInterface::LANGCODE_NOT_SPECIFIED, $domain_id = '') {
// Check if this domain alias already exists.
$query = \Drupal::database()->select('domain_path', 'domain_path')
->fields('domain_path', ['language', 'source', 'alias'])
->condition('domain_id', $domain_id)
->condition('alias', $alias);
$result = $query->execute()->fetchAssoc();
if(isset($result['source'])) {
$existing_source = $result["source"];
if ($existing_source != $alias) {
// If it is an alias for the provided source, it is allowed to keep using
// it. If not, then it is reserved.
return $existing_source != $source;
}
}
// Then check if there is a route with the same path.
if ($this->isRoute($alias)) {
return TRUE;
}
// Finally check if any other modules have reserved the alias.
$args = [
$alias,
$source,
$langcode,
];
$implementations = $this->moduleHandler->getImplementations('pathauto_is_alias_reserved');
foreach ($implementations as $module) {
$result = $this->moduleHandler->invoke($module, 'pathauto_is_alias_reserved', $args);
if (!empty($result)) {
// As soon as the first module says that an alias is in fact reserved,
// then there is no point in checking the rest of the modules.
return TRUE;
}
}
return FALSE;
}
}