-
Notifications
You must be signed in to change notification settings - Fork 3
/
dgi_members.module
229 lines (213 loc) · 7.44 KB
/
dgi_members.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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<?php
/**
* @file
* Contains dgi_members.module.
*/
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\node\NodeInterface;
use Drupal\views\ViewExecutable;
/**
* Implements hook_help().
*/
function dgi_members_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the dgi_members module.
case 'help.page.dgi_members':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Facilitates display of compounds objects') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_views_pre_render().
*/
function dgi_members_views_pre_render(ViewExecutable $view) {
// XXX: Messing with some views results for displaying non-compound compound
// objects. This is not generally applicable, but a quick fix kind of
// adjustment to allow some objects to display similarly to functionality
// available in i7 that allows for child objects on non-compound objects,
// where the display allowed one node to behave as the parent and display as
// the first child of its set.
if (
dgi_members_treat_parent_as_first_sibling()
&&
isset($view)
&&
(
(
($view->id() == 'manage_members')
&&
($view->current_display == 'block_1')
)
||
($view->id() == 'compound_navigation')
)
) {
$members = dgi_members_get_members();
$k = 0;
$temp = [];
$entity_id = (int) $view->argument['field_member_of_target_id']->argument;
if (count($members) > count($view->result) && $members[0] == $entity_id) {
foreach ($view->result as $value) {
if ($k == 0) {
$entity = \Drupal::entityTypeManager()
->getStorage('node')
->load($entity_id);
$first_result = new $value();
$first_result->_entity = $entity;
$first_result->index = $k;
$first_result->nid = $entity_id;
$temp[] = $first_result;
$k++;
}
$value->index = $k;
$temp[] = $value;
$k++;
}
$view->result = $temp;
}
}
if (isset($view) && ($view->id() == 'compound_navigation')) {
$members = dgi_members_get_members();
$active = dgi_members_get_active();
if ($active) {
$active_node_id = $active->id();
$view->element['#attached']['library'][] = 'dgi_members/compound_parts';
$view->element['#attached']['drupalSettings']['dgi_members']['has_members']
= (empty($members) || !$members) ? FALSE : TRUE;
$view->element['#attached']['drupalSettings']['dgi_members']['active_nid']
= $active_node_id;
}
}
}
/**
* Implements hook_form_form_id_alter().
*/
function dgi_members_form_block_form_alter(&$form, $form_state, $form_id) {
// In following with how Islandora core is handling block visibility,
// implementing this hook to unset our custom condition. There's too many to
// use well within the core block placement UI, and no other reasonable way to
// filter them out. See https://www.drupal.org/node/2284687. Use
// /admin/structure/context instead if you want to use these conditions
// to alter block layout.
// This hook is also doing the same thing in Islandora core, to hide the
// conditions it provides.
unset($form['visibility']['node_compound_current_has_term']);
}
/**
* Implements hook_entity_view_mode_alter().
*/
function dgi_members_entity_view_mode_alter(&$view_mode, EntityInterface $entity) {
// Change the view mode based on user input from a 'view_mode_alter'.
// This is very similar to the implementation of this hook in the core
// islandora module. The difference here is that we are only allowing the view
// mode to be switched away from a known view mode,
// (repository_item_top_viewer), and if the entity (node) being processed
// differs from the menu route object, evaluations are allowed to continue if
// said node is a member of the menu router object.
if ($view_mode == "repository_item_top_viewer") {
$storage = \Drupal::service('entity_type.manager')->getStorage('entity_view_mode');
$context_manager = \Drupal::service('context.manager');
if (dgi_members_entity_is_member($entity)) {
foreach ($context_manager->getActiveReactions('\Drupal\islandora\Plugin\ContextReaction\ViewModeAlterReaction') as $reaction) {
// Construct the new view mode's machine name.
$entity_type = $entity->getEntityTypeId();
$mode = $reaction->execute();
$machine_name = "$entity_type.$mode";
// Try to load it.
$new_mode = $storage->load($machine_name);
// If successful, alter the view mode.
if ($new_mode) {
$view_mode = $mode;
}
else {
// Otherwise, leave it be, but log a message.
\Drupal::logger('dgi_members')
->info("EntityViewMode $machine_name does not exist. View mode cannot be altered.");
}
}
}
}
}
/**
* Determine if the view mode entity is a member of the menu router object.
*
* @param Drupal\Core\Entity\EntityInterface $entity
* A node to evaluate.
*
* @return bool
* TRUE if is a member, FALSE if not.
*/
function dgi_members_entity_is_member(EntityInterface $entity) {
if ($entity) {
$members = dgi_members_get_members();
if (in_array($entity->id(), $members)) {
return TRUE;
}
}
return FALSE;
}
/**
* Retrieve the active member, if available.
*
* @return bool||Node
* FALSE if no node available, or the active Node.
*/
function dgi_members_get_active() {
$active_member = \Drupal::request()->query->get('active_member');
return \Drupal::service('dgi_members.entity_service')->retrieveActiveMember($active_member);
}
/**
* Return members of the menu route object.
*
* @return bool||array
* FALSE if no member available, indexed array of members otherwise.
*/
function dgi_members_get_members() {
return \Drupal::service('dgi_members.entity_service')->membersQueryExecute();
}
/**
* Return whether or not to treat parent as first child.
*
* This was expected behaviour configurable in i7, and allowing this display
* option is, in some cases, more desirable than a massive restructuring of
* migrated content.
*
* @return bool
* Default to FALSE for standard Compound Object behaviour.
* TRUE if the capability to treat objects as a member of themselves is
* desired.
*/
function dgi_members_treat_parent_as_first_sibling() {
return \Drupal::config('dgi_members.settings')
->get('display_non_compound_compound_as_first_member_of_itself');
}
/**
* Return whether to treat this entity as a Compound Object or not.
*
* This is related to dgi_members_treat_parent_as_first_sibling(), but could be
* expanded to be relevant in a separate use case in the future.
*
* @param Drupal\node\NodeInterface $entity
* Node to evaluate.
*
* @return bool
* Default to FALSE for standard Compound Object behaviour.
* TRUE if object without the 'Compound Object' value in the 'Content Type'
* (model) field should still be treated as a Compound Object.
*/
function dgi_members_entity_understood_as_non_compound_compound(NodeInterface $entity) {
$allow_this_functionality = \Drupal::config('dgi_members.settings')
->get('allow_compound_display_for_non_compound');
if ($allow_this_functionality) {
$utils = \Drupal::service('islandora.utils');
$associated_media = $utils->getMedia($entity);
if (!empty($associated_media)) {
return TRUE;
}
}
return FALSE;
}