-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbookmark.module
117 lines (104 loc) · 3.37 KB
/
bookmark.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
<?php
/**
* @file
* Contains bookmark.module.
*/
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
/**
* Implements hook_help().
*/
function bookmark_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the bookmark module.
case 'help.page.bookmark':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Bookmark') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_theme().
*/
function bookmark_theme() {
$theme = [];
$theme['bookmarks_list'] = [
'variables' => ['bookmarks' => NULL],
];
$theme['bookmark'] = [
'render element' => 'elements',
'template' => 'bookmarks',
];
$theme['bookmarks_content_add_list'] = [
'render element' => 'content',
'variables' => ['content' => NULL],
];
$theme['bookmark_link'] = [
'variables' => ['link' => NULL],
];
return $theme;
}
/**
* Implements hook_theme_suggestions_HOOK().
*/
function bookmark_theme_suggestions_bookmark(array $variables) {
$suggestions = [];
$entity = $variables['elements']['#bookmark'];
$sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_');
$suggestions[] = 'bookmark__' . $sanitized_view_mode;
$suggestions[] = 'bookmark__' . $entity->bundle();
$suggestions[] = 'bookmark__' . $entity->bundle() . '__' . $sanitized_view_mode;
$suggestions[] = 'bookmark__' . $entity->id();
$suggestions[] = 'bookmark__' . $entity->id() . '__' . $sanitized_view_mode;
return $suggestions;
}
/**
* Implements hook_entity_extra_field_info().
*/
function bookmark_entity_extra_field_info() {
$bookmark_types = \Drupal::service('bookmark')->getAllBookmarkTypes();
$extra = [];
foreach ($bookmark_types as $bookmark_type) {
/** @var \Drupal\bookmark\Entity\BookmarkTypeInterface $bookmark */
$bundles = $bookmark_type->getApplicableBundles();
foreach ($bundles as $bundle) {
$extra['node'][$bundle]['display']['bookmark_' . $bookmark_type->id()] = [
'label' => t('Bookmark: %title', [
'%title' => $bookmark_type->label(),
]),
'description' => t('Bookmark link'),
'weight' => 10,
];
}
}
return $extra;
}
/**
* Implements hook_entity_view().
*
* Display the link to bookmark a page.
*/
function bookmark_entity_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
// Don't show on previews.
if ($entity->isNew()) {
return;
}
$build['#cache']['contexts'][] = 'user.permissions';
if (empty($build['#cache']['tags'])) {
$build['#cache']['tags'] = [];
}
// Get all possible bookmark types for this entity type.
/** @var \Drupal\bookmark\BookmarkService $bookmark_service */
$bookmark_service = \Drupal::service('bookmark');
$bookmark_types = $bookmark_service->getAllBookmarkTypes($entity->bundle());
$user = \Drupal::currentUser();
foreach ($bookmark_types as $bookmark_type) {
$access = \Drupal::entityTypeManager()->getAccessControlHandler('bookmark')->createAccess($bookmark_type->id(), $user, [], TRUE);
if ($access->isAllowed() && $display->getComponent('bookmark_' . $bookmark_type->id())) {
$build['bookmark_' . $bookmark_type->id()] = $bookmark_service->generateLink($bookmark_type, $entity, $user);
}
}
}