This repository has been archived by the owner on Aug 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
rdf_entity.module
executable file
·179 lines (162 loc) · 5.78 KB
/
rdf_entity.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
<?php
/**
* @file
* Main functions and hook implementations of the RDF Entity module.
*/
declare(strict_types = 1);
use Drupal\Core\Render\Element;
use Drupal\Core\Url;
use Drupal\rdf_entity\RdfInterface;
use Drupal\sparql_entity_storage\Entity\Query\Sparql\SparqlQueryInterface;
/**
* Implements hook_theme().
*/
function rdf_entity_theme() {
return [
'rdf_entity' => [
'render element' => 'elements',
],
'rdf_add_list' => [
'variables' => ['content' => NULL],
],
];
}
/**
* Prepares variables for rdf entity templates.
*
* Default template: rdf-entity.html.twig.
*
* Themes should use their own copy of rdf-entity.html.twig.
* The default is located inside "templates/rdf-entity.html.twig".
*
* @param array $variables
* An associative array containing:
* - elements: An array of elements to display in view mode.
* - rdf_entity: The rdf_entity object.
* - view_mode: View mode; e.g., 'full', 'teaser', etc.
*/
function template_preprocess_rdf_entity(array &$variables) {
$renderer = \Drupal::getContainer()->get('renderer');
$variables['view_mode'] = $variables['elements']['#view_mode'];
// Provide a distinct $teaser boolean.
$variables['teaser'] = $variables['view_mode'] == 'teaser';
$variables['rdf_entity'] = $variables['elements']['#rdf_entity'];
/** @var \Drupal\rdf_entity\RdfInterface $rdf_entity */
$rdf_entity = $variables['rdf_entity'];
$variables['date'] = $renderer->render($variables['elements']['created']);
unset($variables['elements']['created']);
$variables['author_name'] = $renderer->render($variables['elements']['uid']);
unset($variables['elements']['uid']);
$variables['url'] = $rdf_entity->toUrl('canonical', [
'language' => $rdf_entity->language(),
]);
$variables['label'] = !empty($variables['elements']['label']) ? $variables['elements']['label'] : ['#markup' => $rdf_entity->label()];
unset($variables['elements']['label']);
// The view mode is 'full' and we are on the 'rdf_entity.view' route.
$variables['page'] = ($variables['view_mode'] == 'full' && (rdf_entity_is_page($rdf_entity)));
// Helpful $content variable for templates.
$variables += ['content' => []];
foreach (Element::children($variables['elements']) as $key) {
$variables['content'][$key] = $variables['elements'][$key];
}
// Add article ARIA role.
$variables['attributes']['role'] = 'article';
}
/**
* Prepares variables for list of available RDF type templates.
*
* Default template: rdf-add-list.html.twig.
*
* @param array $variables
* An associative array containing:
* - content: An array of RDF entity bundles.
*/
function template_preprocess_rdf_add_list(array &$variables) {
$variables['types'] = [];
if (!empty($variables['content'])) {
foreach ($variables['content'] as $type) {
$variables['types'][$type->id()] = [
'type' => $type->id(),
'add_link' => [
'#type' => 'link',
'#title' => $type->label(),
'#url' => new Url('rdf_entity.rdf_add', ['rdf_type' => $type->id()]),
],
'description' => [
'#markup' => $type->getDescription(),
],
];
}
}
}
/**
* Is the current page the full page view of the passed-in RDF entity?
*
* @param \Drupal\rdf_entity\RdfInterface $rdf_entity
* An RDF entity.
*
* @return int|false
* The ID of the RDF entity if this is a full page view, otherwise FALSE.
*/
function rdf_entity_is_page(RdfInterface $rdf_entity) {
$route_match = \Drupal::routeMatch();
if ($route_match->getRouteName() == 'entity.rdf_entity.canonical') {
$page_rdf_entity = $route_match->getParameter('rdf_entity');
}
return (!empty($page_rdf_entity) ? $page_rdf_entity->id() == $rdf_entity->id() : FALSE);
}
/**
* Implements hook_theme_suggestions_HOOK().
*/
function rdf_entity_theme_suggestions_rdf_entity(array $variables) {
$suggestions = [];
$rdf_entity = $variables['elements']['#rdf_entity'];
$sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_');
$suggestions[] = 'rdf_entity__' . $sanitized_view_mode;
$suggestions[] = 'rdf_entity__' . $rdf_entity->bundle();
$suggestions[] = 'rdf_entity__' . $rdf_entity->bundle() . '__' . $sanitized_view_mode;
$suggestions[] = 'rdf_entity__' . $rdf_entity->id();
$suggestions[] = 'rdf_entity__' . $rdf_entity->id() . '__' . $sanitized_view_mode;
return $suggestions;
}
/**
* Implements hook_pathauto_alias_types_alter().
*/
function rdf_entity_pathauto_alias_types_alter(array &$definitions) {
// Remove the automatically generated Pathauto alias type, we offer our own
// alias type.
// @see \Drupal\pathauto\Plugin\Deriver\EntityAliasTypeDeriver::getDerivativeDefinitions()
// @see \Drupal\rdf_entity\Plugin\pathauto\AliasType\RdfEntityAliasType
unset($definitions['canonical_entities:rdf_entity']);
}
/**
* Implements hook_query_TAG_alter().
*
* If UUID is queried, just swap it with the ID. They are the same but UUID is
* not stored, while on ID we can rely.
*
* @todo For now, this handles also taxonomy_term. To be copied in rdf_taxonomy
* when that module will not depend on rdf_entity.
*/
function rdf_entity_query_sparql_alter(SparqlQueryInterface $query) {
$id_key = $query->getEntityType()->getKey('id');
$uuid_key = $query->getEntityType()->getKey('uuid');
$id_field = $uuid_field = NULL;
$conditions =& $query->conditions();
foreach ($conditions as $delta => &$condition) {
if ($condition['field'] === $id_key) {
$id_field = $delta;
}
if ($condition['field'] === $uuid_key) {
$uuid_field = $delta;
}
}
if ($id_field === NULL && $uuid_field !== NULL) {
$condition = ['field' => $id_key] + $conditions[$uuid_field];
$conditions[] = $condition;
}
if ($uuid_field !== NULL) {
unset($conditions[$uuid_field]);
}
$conditions = array_values($conditions);
}