-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdomain_unique_path_alias.module
89 lines (77 loc) · 2.8 KB
/
domain_unique_path_alias.module
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
<?php
/**
* @file
* Hook implementations for this module.
*/
use Drupal\Core\Entity\ContentEntityType;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Language\LanguageInterface;
use Drupal\domain_unique_path_alias\DomainUniquePathAliasHelper;
use Drupal\node\Entity\Node;
use Drupal\node\NodeInterface;
use Drupal\path_alias\PathAliasInterface;
/**
* Implements hook_ENTITY_TYPE_presave().
*/
function domain_unique_path_alias_entity_presave(EntityInterface $entity) {
if ($entity instanceof PathAliasInterface) {
// Get domains configuration.
$options = \Drupal::entityTypeManager()->getStorage('domain')
->loadOptionsList();
$helper = \Drupal::service('domain_unique_path_alias.helper');
assert($helper instanceof DomainUniquePathAliasHelper);
// Get domain_id from get request param.
$domain_id = $helper->getDomainIdByRequest();
// Get domain_id from field_domain_source field.
if (empty($domain_id) || !in_array($domain_id, $options)) {
// Get field_domain_source data if a new translation.
$node = \Drupal::routeMatch()->getParameter('node');
// Get node from path_alias path.
if (!$node instanceof NodeInterface) {
$path = explode("/", $entity->getPath());
if (
isset($path[1], $path[2])
&& $path[1] === 'node'
&& is_numeric($path[2])
) {
$node = Node::load($path[2]);
}
}
if ($node instanceof NodeInterface) {
$langcode = \Drupal::languageManager()
->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->getId();
$langcode = $entity->get('langcode')->getString() ?? $langcode;
$translation_languages = $node->getTranslationLanguages();
if (in_array($langcode, array_keys($translation_languages))) {
$domain_id = $helper->getDomainIdFromEntity($node);
}
}
}
// Set domain_id to path_alias entity.
if (!empty($domain_id)) {
/** @var \Drupal\path_alias\Entity\PathAlias $entity */
$entity->set('domain_id', $domain_id);
}
}
}
/**
* Implements hook_entity_base_field_info().
*/
function domain_unique_path_alias_entity_base_field_info(ContentEntityType $entity_type) {
$fields = [];
if ($entity_type->id() === 'path_alias') {
$fields['domain_id'] = BaseFieldDefinition::create('string')
->setLabel('Domain Id')
->setDescription('Domain identification.');
}
return $fields;
}
/**
* Implements hook_validation_constraint_alter().
*/
function domain_unique_path_alias_validation_constraint_alter(array &$definitions) {
if (isset($definitions['UniquePathAlias'])) {
$definitions['UniquePathAlias']['class'] = '\\Drupal\\domain_unique_path_alias\\Plugin\\Validation\\Constraints\\DomainUniquePathAliasConstraint';
}
}