-
Notifications
You must be signed in to change notification settings - Fork 0
/
contextual_aliases.module
110 lines (102 loc) · 3.86 KB
/
contextual_aliases.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\contextual_aliases\ContextualPathAliasListBuilder;
use Drupal\contextual_aliases\Entity\ContextualRedirect;
use Drupal\contextual_aliases\Form\ContextualRedirectForm;
use Drupal\redirect\Plugin\Field\FieldWidget\RedirectSourceWidget;
/**
* Implements hook_entity_type_alter().
*/
function contextual_aliases_entity_type_alter(array &$entity_types) {
/** @var \Drupal\Core\Entity\EntityType[] $entity_types */
if (array_key_exists('redirect', $entity_types)) {
$entity_types['redirect']->setClass(ContextualRedirect::class);
$entity_types['redirect']->setFormClass('default', ContextualRedirectForm::class);
$entity_types['redirect']->setFormClass('edit', ContextualRedirectForm::class);
}
if (isset($entity_types['path_alias'])) {
$entity_types['path_alias']->setListBuilderClass(ContextualPathAliasListBuilder::class);
$constraints = $entity_types['path_alias']->getConstraints();
unset($constraints['UniquePathAlias']);
$entity_types['path_alias']->setConstraints($constraints);
$entity_types['path_alias']->addConstraint('ContextualUniquePathAlias', []);
}
}
function contextual_aliases_module_implements_alter(&$implementations, $hook) {
if ($hook == 'entity_type_alter') {
$group = $implementations['contextual_aliases'];
unset($implementations['contextual_aliases']);
$implementations['contextual_aliases'] = $group;
}
}
/**
* Collect all context options for displaying a select box.
*/
function contextual_aliases_context_options() {
return \Drupal::service('contextual_aliases.context_manager')->getContextOptions();
}
/**
* Implements hook_field_widget_form_alter().
*/
function contextual_aliases_field_widget_form_alter(&$element, FormStateInterface $form_state, $context) {
if ($context['widget'] instanceof RedirectSourceWidget) {
// Disable ajax functionality, since it doesn't know the context and
// results in false positives.
unset($element['path']['#ajax']);
}
}
/**
* Implements hook_entity_base_field_info().
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
*/
function contextual_aliases_entity_base_field_info(EntityTypeInterface $entity_type) {
$fields = [];
if ($entity_type->id() === 'path_alias') {
$fields['context'] = BaseFieldDefinition::create('list_string')
->setLabel(new TranslatableMarkup('Path Alias context'))
->setTranslatable(TRUE)
->setRevisionable(TRUE)
->setDefaultValue('')
->setSetting('allowed_values_function', 'contextual_aliases_context_options')
->setDisplayOptions('form', [
'type' => 'options_select',
'weight' => 10,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE)
->setTranslatable(TRUE);
}
return $fields;
}
/**
* Implements hook_ENTITY_TYPE_create().
*
* @param \Drupal\path_alias\Entity\PathAlias $pathAlias
*/
function contextual_aliases_path_alias_create($pathAlias) {
if ($pathAlias->get('context')->isEmpty()) {
/** @var \Drupal\contextual_aliases\ContextualAliasesContextManager $manager */
$manager = \Drupal::service('contextual_aliases.context_manager');
$context = $manager->getCurrentContext();
if ($context) {
$pathAlias->get('context')->setValue($context);
}
}
}
/**
* Implements hook_ENTITY_TYPE_create().
*
* @param \Drupal\path_alias\Entity\PathAlias $pathAlias
*/
function contextual_aliases_path_alias_presave($pathAlias) {
/** @var \Drupal\contextual_aliases\ContextualAliasesContextManager $manager */
$manager = \Drupal::service('contextual_aliases.context_manager');
$context = $manager->getSourceContext($pathAlias->getPath());
if ($context) {
$pathAlias->get('context')->setValue($context);
}
}