forked from localgovdrupal/localgov_guides
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocalgov_guides.module
120 lines (113 loc) · 3.43 KB
/
localgov_guides.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
<?php
/**
* @file
* Localgov Guides module hooks.
*/
use Drupal\Core\Entity\Display\EntityFormDisplayInterface;
use Drupal\node\NodeInterface;
/**
* Implements hook_theme().
*/
function localgov_guides_theme($existing, $type, $theme, $path) {
return [
'guides_contents_block' => [
'variables' => [
'links' => [],
'format' => [],
],
],
'guides_prev_next_block' => [
'variables' => [
'previous_url' => NULL,
'previous_title' => NULL,
'next_url' => NULL,
'next_title' => NULL,
],
],
'node__localgov_guides_overview__full' => [
'template' => 'node--localgov-guides-overview--full',
'base hook' => 'node',
],
'node__localgov_guides_page__full' => [
'template' => 'node--localgov-guides-page--full',
'base hook' => 'node',
],
];
}
/**
* Implements hook_ENTITY_TYPE_insert().
*/
function localgov_guides_node_insert(NodeInterface $node) {
localgov_guides_node_update($node);
}
/**
* Implements hook_ENTITY_TYPE_update().
*
* Implements reference back on overview to page when pages are created.
*/
function localgov_guides_node_update(NodeInterface $node) {
if ($node->bundle() == 'localgov_guides_page') {
if ($parent = $node->localgov_guides_parent->entity) {
if (array_search(['target_id' => $node->id()], $parent->localgov_guides_pages->getValue()) === FALSE) {
$parent->localgov_guides_pages->appendItem(['target_id' => $node->id()]);
$parent->save();
}
}
}
}
/**
* Implements hook_modules_installed().
*/
function localgov_guides_modules_installed($modules) {
$services = in_array('localgov_services_navigation', $modules);
$topics = in_array('localgov_topics', $modules);
if ($services || $topics) {
\Drupal::service('config.installer')->installOptionalConfig();
localgov_guides_optional_fields_settings($services, $topics);
}
}
/**
* Set form settings for optional services and topic fields on installation.
*
* @param bool $services
* If localgov_services is (being) installed.
* @param bool $topics
* If localgov_topics is (being) installed.
*/
function localgov_guides_optional_fields_settings($services, $topics) {
$properties = [
'targetEntityType' => 'node',
'bundle' => 'localgov_guides_overview',
];
if ($form_displays = \Drupal::entityTypeManager()->getStorage('entity_form_display')->loadByProperties($properties)) {
foreach ($form_displays as $form_display) {
assert($form_display instanceof EntityFormDisplayInterface);
if ($services && !$form_display->getComponent('localgov_services_parent')) {
$form_display->setComponent('localgov_services_parent', [
'type' => 'entity_reference_autocomplete',
'region' => 'content',
'settings' => [
'match_operator' => 'CONTAINS',
'size' => '60',
'placeholder' => '',
'match_limit' => 10,
],
'weight' => 51,
])->save();
}
if ($topics && !$form_display->getComponent('localgov_topics')) {
$form_display->setComponent('localgov_topic_classified', [
'type' => 'entity_reference_autocomplete',
'region' => 'content',
'settings' => [
'match_operator' => 'CONTAINS',
'size' => '60',
'placeholder' => '',
'match_limit' => 10,
],
'weight' => 50,
])->save();
}
}
}
}