forked from localgovdrupal/localgov_campaigns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocalgov_campaigns.module
89 lines (83 loc) · 2.39 KB
/
localgov_campaigns.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
* LocalGov Campaigns module file.
*/
use Drupal\node\NodeInterface;
/**
* Implements hook_theme().
*/
function localgov_campaigns_theme($existing, $type, $theme, $path) {
return [
'campaign_navigation' => [
'variables' => [
'heading' => '',
'links' => [],
'parent_url' => '',
],
],
'campaign_banner' => [
'variables' => [
'tag' => '',
'heading' => '',
'image' => '',
],
],
'campaign_overview_banner' => [
'variables' => [
'heading' => '',
'image' => '',
],
],
'node__localgov_campaigns_page__full' => [
'template' => 'node--localgov-campaigns-page--full',
'base hook' => 'node',
],
];
}
/**
* Implements hook_ENTITY_TYPE_insert().
*/
function localgov_campaigns_node_insert(NodeInterface $node) {
localgov_campaigns_node_update($node);
}
/**
* Implements hook_ENTITY_TYPE_update().
*
* Implements reference back on overview to page when pages are created.
*/
function localgov_campaigns_node_update(NodeInterface $node) {
if ($node->bundle() == 'localgov_campaigns_page') {
if ($parent = $node->localgov_campaigns_parent->entity) {
// Check if referenced campaign has changed.
if (isset($node->original) and $old_parent = $node->original->localgov_campaigns_parent->entity) {
if ($old_parent->id() !== $parent->id()) {
$i = array_search(['target_id' => $node->id()], $old_parent->localgov_campaigns_pages->getValue());
if ($i !== FALSE) {
$old_parent->localgov_campaigns_pages->removeItem($i);
$old_parent->save();
}
}
}
// Add page to parent reference field.
elseif (array_search(['target_id' => $node->id()], $parent->localgov_campaigns_pages->getValue()) === FALSE) {
$parent->localgov_campaigns_pages->appendItem(['target_id' => $node->id()]);
$parent->save();
}
}
}
}
/**
* Implements hook_ENTITY_TYPE_delete().
*/
function localgov_campaigns_node_delete(NodeInterface $node) {
if ($node->bundle() == 'localgov_campaigns_page') {
if ($parent = $node->localgov_campaigns_parent->entity) {
$i = array_search(['target_id' => $node->id()], $parent->localgov_campaigns_pages->getValue());
if ($i !== FALSE) {
$parent->localgov_campaigns_pages->removeItem($i);
$parent->save();
}
}
}
}