-
Notifications
You must be signed in to change notification settings - Fork 3
/
hs_config_prefix.module
132 lines (118 loc) · 4.25 KB
/
hs_config_prefix.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
/**
* @file
* Contains hs_config_prefix.module.
*/
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Installer\InstallerKernel;
/**
* Implements hook_help().
*/
function hs_config_prefix_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the hs_config_prefix module.
case 'help.page.hs_config_prefix':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Prefix any configuration entities with a specified string') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_form_alter().
*/
function hs_config_prefix_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$form_ids = [
'node_type_add_form' => 'type',
'ds_field_form' => 'id',
'webform_add_form' => 'id',
'menu_position_rule_form' => 'id',
];
if (!isset($form_ids[$form_id])) {
return;
}
// Special assistance is needed for a few forms so we'll modify the form state
// values during validation. For example:
// * Adding a node type redirects to the manage fields route based on the form
// state value.
// * DS fields don't fire the config entity hook.
// * Adding webform redirects to build page based on the form state values.
// * Adding menu position rules creates a menu item with the ID of the entity.
$form_state->set('hs_config_prefix_key', $form_ids[$form_id]);
array_unshift($form['#validate'], 'hs_config_prefix_custom_form_validate');
}
/**
* Custom validation for node type add form to change the form state values.
*
* @param array $form
* Complete form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* Current form state.
*/
function hs_config_prefix_custom_form_validate(array &$form, FormStateInterface $form_state) {
$id = $form_state->getValue($form_state->get('hs_config_prefix_key'));
$prefix = \Drupal::config('hs_config_prefix.settings')->get('prefix');
// Check if manually entered prefix exists.
if ($prefix && strpos($id, $prefix) !== 0) {
$form_state->setValue($form_state->get('hs_config_prefix_key'), "{$prefix}{$id}");
}
}
/**
* Implements hook_entity_presave().
*/
function hs_config_prefix_entity_presave(EntityInterface $entity) {
if (InstallerKernel::installationAttempted() || empty($GLOBALS['_REQUEST']) || PHP_SAPI === 'cli') {
return;
}
if ($entity->isNew() && $entity instanceof ConfigEntityBase) {
// Form and view displays are namespace by the view modes and form modes.
// Field storage is managed by the field_ui.settings config.
$exclude_config_types = [
'entity_form_display',
'entity_view_display',
'field_storage_config',
'behavior_settings',
];
if (!in_array($entity->getEntityTypeId(), $exclude_config_types)) {
try {
hs_config_prefix_set_entity_id($entity);
}
catch (Exception $e) {
\Drupal::logger('hs_config_prefix')
->error('Unable to modify entity id on %type: %id', [
'%type' => $entity->getEntityTypeId(),
'%id' => $entity->id(),
]);
}
}
}
}
/**
* Modify the id of the entity to have the prefix form the config.
*
* @param \Drupal\Core\Config\Entity\ConfigEntityBase $entity
* Entity to modify.
*
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
function hs_config_prefix_set_entity_id(ConfigEntityBase $entity) {
$entity_definition = \Drupal::entityTypeManager()
->getDefinition($entity->getEntityTypeId());
// Different entity types have different keys for the id attribute.
$id_key = $entity_definition->getKey('id');
$prefix = \Drupal::config('hs_config_prefix.settings')->get('prefix');
// Some entity ID's have a prefix. Like view modes, when created through the
// UI the entity id is `node.machine_name` so we split up any entities so that
// we dont modify the prefix.
$entity_id = explode('.', $entity->id());
$item_id = array_pop($entity_id);
// Check if manually entered prefix exists.
if ($prefix && strpos($item_id, $prefix) !== 0) {
$entity_id[] = $prefix . $item_id;
$entity->set($id_key, implode('.', $entity_id));
}
}