-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnodekey.module
More file actions
executable file
·51 lines (47 loc) · 1.63 KB
/
nodekey.module
File metadata and controls
executable file
·51 lines (47 loc) · 1.63 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
<?php
use Drupal\nodekey\Entity\NodeKeyEntity;
function _nodekey_check_content_type($node) {
$content_types = \Drupal::config('nodekey.settings')->get('content_types');
if ($content_types) {
foreach ($content_types as $key => $bundle) {
if ($bundle === 0) {
continue;
}
if ($bundle == $node->getType()) {
return true;
}
}
}
return false;
}
function nodekey_node_presave($node) {
if (_nodekey_check_content_type($node)) {
$nodekey = $node->get('nodekey')->value;
$existing = NodeKeyEntity::count($nodekey);
if ($existing == 0) {
$nodekey = NodeKeyEntity::create($node);
$node->set('nodekey', $nodekey);
\Drupal::messenger()->addMessage(sprintf(t('The node key « %s » has been created.', array(), array('context' => 'nodekey')), $nodekey));
} elseif ($existing > 1) {
\Drupal::messenger()->addMessage(sprintf(t('The node key « %s » already exists. Please fix it in your database.', array(), array('context' => 'nodekey')), $nodekey), 'warning');
}
}
}
function nodekey_theme_suggestions_page_alter(array &$suggestions)
{
if ($node = \Drupal::routeMatch()->getParameter('node')) {
if ($node->hasField('nodekey')) {
$nodekey = $node->get('nodekey')->value;
$suggestions[] = 'page__' . $nodekey;
}
}
}
function nodekey_theme_suggestions_node_alter(array &$suggestions)
{
if ($node = \Drupal::routeMatch()->getParameter('node')) {
if ($node->hasField('nodekey')) {
$nodekey = $node->get('nodekey')->value;
$suggestions[] = 'node__' . $nodekey;
}
}
}