-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmachine.module
More file actions
executable file
·167 lines (142 loc) · 4.1 KB
/
machine.module
File metadata and controls
executable file
·167 lines (142 loc) · 4.1 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
<?php
/**
* @file
* Main module file.
*/
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_help().
*/
function machine_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the machine module.
case 'help.page.machine':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Machine - provides configured content entities with <b>machine<b/> base field.') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_entity_base_field_info().
*/
function machine_entity_base_field_info(EntityTypeInterface $entity_type) {
$fields = [];
// Get configured entities.
$types = \Drupal::configFactory()
->get('machine.settings')
->get('types') ?: [];
if (in_array($entity_type->get('id'), $types, TRUE)) {
$fields['machine'] = BaseFieldDefinition::create('string')
->setSettings([
'default_value' => '',
'max_length' => 255,
])
->addConstraint('UniqueField')
->setDisplayConfigurable('form', TRUE)
->setDisplayOptions('form', [
'weight' => 0,
'type' => 'string_textfield',
'settings' => [
'display_label' => TRUE,
],
])
->setRequired(FALSE)
->setTranslatable(FALSE)
->setLabel(t('Machine name'))
->setDescription(t('Machine name for entity. Allowed: lowercase alphanumeric and underscores.'))
->setRevisionable(FALSE)
->setPropertyConstraints('value', [
'Length' => ['max' => 255],
'Regex' => ['pattern' => '/^([a-z\d_]+)$/'],
]);
}
return $fields;
}
/**
* Implements hook_theme_suggestions_HOOK_alter().
*
* 'block_content' is a special case that needs special care.
*/
function machine_theme_suggestions_block_alter(&$suggestions, $variables) {
// Get configured entities.
$types = \Drupal::configFactory()->get('machine.settings')->get('types') ?: [];
if (!in_array('block_content', $types)) {
return;
}
$elements = $variables['elements'];
// Don't process other blocks.
if (!isset($elements['content']['#block_content'])) {
return;
}
$block_content = $elements['content']['#block_content'];
$build_suggestions = _machine_create_build_entity_suggestions(
$block_content,
$elements['content']['#view_mode']
);
$suggestions = array_merge($suggestions, $build_suggestions);
}
/**
* Implements hook_theme_suggestions_alter().
*
* Does our configured types checking on suggestion hook.
* Extends suggestions.
*/
function machine_theme_suggestions_alter(&$suggestions, $variables, $hook) {
// Get configured entities.
$types = \Drupal::configFactory()->get('machine.settings')->get('types') ?: [];
if (!in_array($hook, $types, TRUE)) {
return;
}
$elements = $variables['elements'];
/* @var $entity \Drupal\Core\Entity\EntityInterface */
if (!$entity = $elements['#' . $elements['#entity_type']]) {
return;
}
$build_suggestions = _machine_create_build_entity_suggestions(
$entity,
$elements['#view_mode'],
$entity->getType()
);
$suggestions = array_merge($suggestions, $build_suggestions);
}
/**
* Builds new suggestions on given variables.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* Entity.
* @param string $view_mode
* View mode.
* @param string $bundle
* Bundle.
*
* @return array
* Array of suggestions.
*/
function _machine_create_build_entity_suggestions(
EntityInterface $entity,
$view_mode = NULL,
$bundle = NULL
) {
$suggestions = [];
// Don't do anything for empty 'machine' field value.
if (!$machine = $entity->machine->value) {
return [];
}
$entity_type = $entity->getEntityTypeId();
$entity_base = empty($bundle) ? $entity_type : implode('__',
[$entity_type, $bundle]);
$suggestions[] = implode('__', [$entity_base, $machine]);
if ($view_mode) {
$suggestions[] = implode('__', [
$entity_base,
$view_mode,
$machine,
]);
}
return $suggestions;
}