-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflat_views.module
More file actions
548 lines (442 loc) · 16.3 KB
/
flat_views.module
File metadata and controls
548 lines (442 loc) · 16.3 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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
<?php
use Drupal\Core\Form\FormStateInterface;
use Drupal\node\NodeInterface;
use Drupal\views\ViewExecutable;
/**
* Implements hook_menu().
*/
function flat_views_menu()
{
$items = array();
$items['dashboard/%user/notifications/delete_all_confirm'] = array(
'title' => 'Delete all notifications',
'type' => MENU_CALLBACK,
'page callback' => 'flat_views_notifications_delete_all',
'page arguments' => array(1),
'access arguments' => array('use deposit module', 1),
);
return $items;
}
function flat_views_notifications_delete_all($form, FormStateInterface $form_state, $user)
{
$a = 2;
return confirm_form(
array(),
t('Are you sure you want to delete all reports?'),
'dashboard/' . $user->getAccountName() . 'notifications',
t('This action cannot be undone.'),
t('Delete'),
t('Cancel')
);
}
function flat_views_notifications_delete_all_submit($user)
{
\Drupal::messenger()->addMessage('Done');
}
function flat_views_views_api()
{
$views_registration = array(
'api' => '3.0',
'path' => drupal_get_path('module', 'flat_views') . '/views',
);
return $views_registration;
}
/**
* Implements hook_block_info().
*/
function flat_views_block_info()
{
$blocks = array();
$blocks['reports_actions'] = array(
'info' => t('FLAT deposit reports actions '),
'status' => TRUE,
'weight' => 999,
'region' => 'content',
'visibility' => BLOCK_VISIBILITY_LISTED,
'pages' => 'dashboard/*/notifications'
);
return $blocks;
}
/**
* Implements hook_block_view().
*/
function flat_views_block_view($delta = '')
{
switch ($delta) {
case 'reports_actions':
if (Drupal::currentUser()->hasPermission('use deposit module')) {
module_load_include('inc', 'flat_views', 'includes/flat_reports.actions_form');
$form = drupal_get_form('flat_reports_action_form');
$rendered_form = drupal_render($form);
$block = array(
'subject' => '',
'content' => $rendered_form,
);
break;
}
}
return $block;
}
/**
* Implements hook_block_info_alter().
*
* Alter visibility of in views defined blocks for report, bundles and collections
*/
function flat_views_block_info_alter(&$blocks, $theme, $code_blocks)
{
foreach ($blocks['views'] as &$view_block) {
$block_description = $view_block['info'];
switch ($block_description) {
case "Flat user notifications":
$view_block['region'] = 'content';
$view_block['visibility'] = BLOCK_VISIBILITY_LISTED;
$view_block['pages'] = 'dashboard/*/notifications';
$view_block['status'] = TRUE;
break;
case "FLAT my bundles":
$view_block['region'] = 'content';
$view_block['visibility'] = BLOCK_VISIBILITY_LISTED;
$view_block['pages'] = "dashboard/*/bundles";
$view_block['status'] = TRUE;
break;
case "FLAT my collections":
$view_block['region'] = 'content';
$view_block['visibility'] = BLOCK_VISIBILITY_LISTED;
$view_block['pages'] = 'dashboard/*/collections';
$view_block['status'] = TRUE;
break;
}
}
}
/**
* Trigger a facet query that filters on descendent_of the current node
* such that all facet values are restricted to descendent of the current node.
*
* @param \Drupal\node\NodeInterface $node
* The node entity to use in the facet query.
*/
function trigger_facet_query(NodeInterface $node)
{
// Get the facet source ID for the facet tied to your Search API index or view.
$facet_source_id = 'search_api:views_page__solr_search_content__page_1'; // Replace with your actual facet source ID.
// Load the Facet Source plugin manager.
/** @var \Drupal\facets\FacetSource\FacetSourcePluginManager $facet_source_manager */
$facet_source_manager = \Drupal::service('plugin.manager.facets.facet_source');
// Load the facet source plugin.
$facet_source = $facet_source_manager->createInstance($facet_source_id);
if ($facet_source) {
// Get the facets tied to this facet source.
$facet_manager = \Drupal::service('facets.manager');
$facets = $facet_manager->getFacetsByFacetSourceId($facet_source_id);
// Loop through facets and find the one matching 'descendent_of_search'.
foreach ($facets as $facet) {
if ($facet->id() === 'descendant_of') {
// Set the current node's ID as the active item
$facet->setActiveItem($node->id());
}
}
} else {
\Drupal::logger('flat_views')->error('Facet source not found for source ID: @facet_source_id', ['@facet_source_id' => $facet_source_id]);
}
}
/**
* Implements hook_node_view().
*
* When on a repository item page, call above function to trigger the facet query for the current node.
*
* @param array $build
* The build array.
* @param \Drupal\node\NodeInterface $node
* The node entity.
* @param \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display
* The entity view display.
* @param string $view_mode
* The view mode.
*/
function flat_views_node_view(array &$build, \Drupal\node\NodeInterface $node, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode)
{
// Check if the node is of the islandora_object content type
if ($node->bundle() == 'islandora_object') {
// Call the function to trigger the facet query programmatically.
trigger_facet_query($node);
}
}
/**
* Implements hook_preprocess_facets_item_list().
*
* @param array $variables
* The variables array.
*
* Adds 'exclude' facet values to an 'include' facet of the same field. Requires the include and exclude facet names to
* be identical apart from '_include' and '_exclude' suffix at the end. Modify the facet source ID if needed.
*
*/
function flat_views_preprocess_facets_item_list(&$variables)
{
$node = \Drupal::routeMatch()->getParameter('node');
if ($node instanceof \Drupal\node\NodeInterface) {
$nid = $node->id();
} else {
$nid = NULL;
}
$facet_id = $variables['facet']->id();
if (str_contains($facet_id, 'include')) {
$exclude_facet_id = str_replace('include', 'exclude', $facet_id);
$facet_source_id = 'search_api:views_page__solr_search_content__page_1';
$facet_source_manager = \Drupal::service('plugin.manager.facets.facet_source');
$facet_source = $facet_source_manager->createInstance($facet_source_id);
if ($facet_source) {
$facet_manager = \Drupal::service('facets.manager');
$facets = $facet_manager->getFacetsByFacetSourceId($facet_source_id);
if (array_key_exists($exclude_facet_id, $facets)) {
$exclude_facet = $facets[$exclude_facet_id];
$active_items = $exclude_facet->getActiveItems();
}
}
$items = &$variables['items'];
if (isset($items)) {
foreach ($items as $key => &$item) {
$include_value = $item['value']['#attributes']['data-drupal-facet-item-value'];
if (in_array($include_value, $active_items) || $item['value']['#title']['#is_active']) {
// if the value is in the active items of the either the 'include' or 'exclude' facet, remove it from the 'include' facet options
unset($items[$key]);
} else {
// use the url and query parameters from the 'include' facet for the 'exclude' facet, replacing the facet id for the parameter with the
// current include value
$include_url = $item['value']['#url'];
if ($nid) {
$include_url->mergeOptions(['query' => ['f' => ['descendant_of:' . $nid]]]);
}
$exclude_url = clone $include_url;
$exclude_query_params = $exclude_url->getOptions()['query'];
foreach ($exclude_query_params['f'] as $key => $query_value) {
if (str_contains($query_value, $facet_id) && str_contains($query_value, $include_value)) {
$exclude_value = str_replace($facet_id, $exclude_facet_id, $query_value);
$exclude_query_params['f'][$key] = $exclude_value;
}
}
$exclude_url->setOptions(['query' => $exclude_query_params]);
$item['include'] = '<span class="facet-sign"><a href="' . $include_url->toString() . '">+</a></span>';
$item['exclude'] = '<span class="facet-sign"><a href="' . $exclude_url->toString() . '">-</a></span>';
}
}
}
}
}
/**
* Implements hook_theme_registry_alter().
*
* Use our own facets_item_list template in order to add the + and - signs for include and exclude facets.
*/
function flat_views_theme_registry_alter(&$theme_registry)
{
if (isset($theme_registry['facets_item_list'])) {
$module_handler = \Drupal::service('extension.list.module');
$module_path = $module_handler->getPath('flat_views');
$theme_registry['facets_item_list']['template'] = 'facets-item-list';
$theme_registry['facets_item_list']['path'] = $module_path . '/templates';
}
if (isset($theme_registry['breadcrumb'])) {
$module_handler = \Drupal::service('extension.list.module');
$module_path = $module_handler->getPath('flat_views');
$theme_registry['breadcrumb']['template'] = 'breadcrumb';
$theme_registry['breadcrumb']['path'] = $module_path . '/templates';
}
}
/**
* Implements hook_block_view_alter().
*
* Remove the block view for facets that have no further choices (number of active items is the same as the number of items).
* The facets module also has a setting to do this, but that will remove the items from the breadcrumb as well
*
* @param array $build
* The renderable array representing the block.
* @param \Drupal\Core\Block\BlockPluginInterface $block
* The block plugin instance.
*/
function flat_views_block_view_alter(array &$build, \Drupal\Core\Block\BlockPluginInterface $block)
{
$plugin_id = $block->getPluginId();
if (str_starts_with($plugin_id, 'facet_block:') && str_contains($plugin_id, '_include')) {
$facet_id = str_replace('facet_block:', '', $plugin_id);
$facet_source_id = 'search_api:views_page__solr_search_content__page_1';
$facet_source_manager = \Drupal::service('plugin.manager.facets.facet_source');
$facet_source = $facet_source_manager->createInstance($facet_source_id);
if ($facet_source) {
$facet_manager = \Drupal::service('facets.manager');
$facets = $facet_manager->getFacetsByFacetSourceId($facet_source_id);
$facet = $facets[$facet_id];
$results = $facet->getResults();
$active = $facet->getActiveItems();
if (sizeof($results) === sizeof($active)) {
$build = [];
}
}
}
}
/**
* Implements hook_preprocess_breadcrumb().
*
* Adding css classes to the breadcrumb items
*
* @param array $variables
* The variables array.
*
* @see template_preprocess_breadcrumb()
*/
function flat_deposit_preprocess_breadcrumb(&$variables)
{
$variables['breadcrumb'] = [];
/** @var \Drupal\Core\Link $link */
foreach ($variables['links'] as $key => $link) {
$class = "";
$options = $link->getUrl()->getOptions() ? $link->getUrl()->getOptions() : [];
if (isset($options['attributes']['class'])) {
$class = implode(' ', $options['attributes']['class']);
}
$remove = NULL;
if (isset($options['attributes']['remove'])) {
$remove = $options['attributes']['remove'];
}
$variables['breadcrumb'][$key] = ['text' => $link->getText(), 'url' => $link->getUrl()->toString(), 'class' => $class, 'remove' => $remove];
}
}
/**
* Implements hook_views_data_alter().
*/
function flat_views_views_data_alter(array &$data)
{
$data['media_field_data']['flat_views_media_download_link'] = [
'title' => t('Download Original File'),
'help' => t('Provides a download link for the Original File in media.'),
'field' => [
'id' => 'flat_views_media_download_link',
'click sortable' => FALSE,
'real field' => 'mid',
'no database field' => TRUE,
],
'entity_type' => 'media',
];
}
/**
* Implements hook_views_pre_view().
*
* Hook to make the compound object members view work on the compound object as well as on any of its children.
*
*/
function flat_views_views_pre_view(\Drupal\views\ViewExecutable $view, $display_id, array &$args)
{
if ($view->id() === 'compound_object_members' && $view->current_display === 'block_1') {
$route_node = \Drupal::routeMatch()->getParameter('node');
if ($route_node instanceof NodeInterface) {
$target_nid = NULL;
// Helper function to check if a node has the "compound object" model
$is_compound_model = function (NodeInterface $node): bool {
if ($node->hasField('field_model') && !$node->get('field_model')->isEmpty()) {
foreach ($node->get('field_model') as $item) {
// Get the target ID (term ID)
$tid = $item->target_id;
if ($tid && ($term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->load($tid))) {
if ($term->label() === 'Compound Object') {
return TRUE;
}
}
}
}
return FALSE;
};
// Case 1: This node is a compound object
if ($is_compound_model($route_node)) {
$target_nid = $route_node->id();
}
// Case 2: This node is a child of a compound object
elseif (
$route_node->hasField('field_member_of') &&
!$route_node->get('field_member_of')->isEmpty()
) {
$parent = $route_node->get('field_member_of')->entity;
if ($parent instanceof NodeInterface && $is_compound_model($parent)) {
$target_nid = $parent->id();
}
}
// Pass the parent NID as the contextual filter argument
if (!empty($target_nid)) {
$args[0] = $target_nid;
}
if (empty($target_nid)) {
// Clear any arguments to prevent fallback rendering.
$view->args = [];
// Force the result set to be empty.
$view->result = [];
$view->executed = TRUE; // Prevents further processing.
return;
}
}
}
}
/**
* Implements hook_preprocess_views_view_table().
*
* Add a custom class to the current child row in the compound object members view, so that it can be highlighted
*
*/
function flat_views_preprocess_views_view_table(&$variables)
{
$view = $variables['view'];
if ($view->id() == 'compound_object_members' && $view->current_display == 'block_1') {
// Get the current node from the route.
$current_node = \Drupal::routeMatch()->getParameter('node');
$current_node_id = null;
if ($current_node instanceof NodeInterface) {
$current_node_id = (int) $current_node->id();
}
foreach ($variables['rows'] as $row_index => &$row) {
$row_node_id = null;
// Check if the 'title' column exists and its content is available.
if (isset($row['columns']['title']['content'][0]['field_output']['#markup'])) {
$title_markup = $row['columns']['title']['content'][0]['field_output']['#markup'];
if (preg_match('/<a[^>]*href="\/node\/(\d+)"/', $title_markup, $matches)) {
if (isset($matches[1])) {
$row_node_id = (int) $matches[1]; // Cast to integer for strict comparison.
}
}
}
// If the current row's node ID matches the current page's node ID, add the class.
if ($row_node_id !== null && $current_node_id !== null && $row_node_id === $current_node_id) {
// Ensure the 'attributes' array exists for the row.
if (!isset($row['attributes'])) {
$row['attributes'] = new \Drupal\Core\Template\Attribute();
}
// Add the custom class.
$row['attributes']->addClass('current-child-row');
}
}
}
}
/**
* Implements hook_views_post_render().
*
* Restrict access to the media display view if user has no access to the media entity itself. Also issue a http 403
*
*/
function flat_views_views_post_render(ViewExecutable $view, array &$output)
{
if ($view->id() === 'media_display_blocks') {
$account = \Drupal::currentUser();
foreach ($view->result as $row) {
if (isset($row->_entity) && !$row->_entity->access('view', $account)) {
// Replace view output with a warning message.
$output = [
'#markup' => '<div class="messages messages--error">You do not have access to this file.</div>',
'#allowed_tags' => ['div', 'span', 'p', 'br', 'strong', 'em'],
'#access' => TRUE,
'#type' => 'markup',
'#cache' => ['max-age' => 0],
];
// Optionally clear the title.
$view->setTitle('');
return;
}
}
}
}