Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /**
|
Chris@0
|
4 * @file
|
Chris@0
|
5 * Enables semantically enriched output for Drupal sites in the form of RDFa.
|
Chris@0
|
6 */
|
Chris@0
|
7
|
Chris@0
|
8 use Drupal\Core\Routing\RouteMatchInterface;
|
Chris@0
|
9 use Drupal\Core\Template\Attribute;
|
Chris@0
|
10 use Drupal\rdf\Entity\RdfMapping;
|
Chris@0
|
11
|
Chris@0
|
12 /**
|
Chris@0
|
13 * Implements hook_help().
|
Chris@0
|
14 */
|
Chris@0
|
15 function rdf_help($route_name, RouteMatchInterface $route_match) {
|
Chris@0
|
16 switch ($route_name) {
|
Chris@0
|
17 case 'help.page.rdf':
|
Chris@0
|
18 $output = '';
|
Chris@0
|
19 $output .= '<h3>' . t('About') . '</h3>';
|
Chris@0
|
20 $output .= '<p>' . t('The RDF module enriches your content with metadata to let other applications (e.g., search engines, aggregators, and so on) better understand its relationships and attributes. This semantically enriched, machine-readable output for your website uses the <a href=":rdfa">RDFa specification</a>, which allows RDF data to be embedded in HTML markup. Other modules can define mappings of their data to RDF terms, and the RDF module makes this RDF data available to the theme. The core modules define RDF mappings for their data model, and the core themes output this RDF metadata information along with the human-readable visual information. For more information, see the <a href=":rdf">online documentation for the RDF module</a>.', [':rdfa' => 'http://www.w3.org/TR/xhtml-rdfa-primer/', ':rdf' => 'https://www.drupal.org/documentation/modules/rdf']) . '</p>';
|
Chris@0
|
21 return $output;
|
Chris@0
|
22 }
|
Chris@0
|
23 }
|
Chris@0
|
24
|
Chris@0
|
25 /**
|
Chris@0
|
26 * @defgroup rdf RDF Mapping API
|
Chris@0
|
27 * @{
|
Chris@0
|
28 * Functions to describe entities and bundles in RDF.
|
Chris@0
|
29 *
|
Chris@0
|
30 * The RDF module introduces RDF and RDFa to Drupal. RDF is a W3C standard to
|
Chris@0
|
31 * describe structured data. RDF can be serialized as RDFa in XHTML attributes
|
Chris@0
|
32 * to augment visual data with machine-readable hints.
|
Chris@0
|
33 * @see http://www.w3.org/RDF/
|
Chris@0
|
34 * @see http://www.w3.org/TR/xhtml-rdfa-primer/
|
Chris@0
|
35 *
|
Chris@0
|
36 * Modules can provide mappings of their bundles' data and metadata to RDF
|
Chris@0
|
37 * classes and properties. This module takes care of injecting these mappings
|
Chris@0
|
38 * into variables available to theme functions and templates. All Drupal core
|
Chris@0
|
39 * themes are coded to be RDFa compatible.
|
Chris@0
|
40 */
|
Chris@0
|
41 /**
|
Chris@0
|
42 * Returns the RDF mapping object associated with a bundle.
|
Chris@0
|
43 *
|
Chris@0
|
44 * The function reads the rdf_mapping object from the current configuration,
|
Chris@0
|
45 * or returns a ready-to-use empty one if no configuration entry exists yet for
|
Chris@0
|
46 * this bundle. This streamlines the manipulation of mapping objects by always
|
Chris@0
|
47 * returning a consistent object that reflects the current state of the
|
Chris@0
|
48 * configuration.
|
Chris@0
|
49 *
|
Chris@0
|
50 * Example usage:
|
Chris@0
|
51 * -Map the 'article' bundle to 'sioc:Post' and the 'title' field to 'dc:title'.
|
Chris@0
|
52 * @code
|
Chris@0
|
53 * rdf_get_mapping('node', 'article')
|
Chris@0
|
54 * ->setBundleMapping(array(
|
Chris@0
|
55 * 'types' => array('sioc:Post'),
|
Chris@0
|
56 * ))
|
Chris@0
|
57 * ->setFieldMapping('title', array(
|
Chris@0
|
58 * 'properties' => array('dc:title')
|
Chris@0
|
59 * ))
|
Chris@0
|
60 * ->save();
|
Chris@0
|
61 * @endcode
|
Chris@0
|
62 *
|
Chris@0
|
63 * @param string $entity_type
|
Chris@0
|
64 * The entity type.
|
Chris@0
|
65 * @param string $bundle
|
Chris@0
|
66 * The bundle.
|
Chris@0
|
67 *
|
Chris@0
|
68 * @return \Drupal\rdf\Entity\RdfMapping
|
Chris@0
|
69 * The RdfMapping object.
|
Chris@0
|
70 */
|
Chris@0
|
71 function rdf_get_mapping($entity_type, $bundle) {
|
Chris@0
|
72 // Try loading the mapping from configuration.
|
Chris@0
|
73 $mapping = RdfMapping::load($entity_type . '.' . $bundle);
|
Chris@0
|
74
|
Chris@0
|
75 // If not found, create a fresh mapping object.
|
Chris@0
|
76 if (!$mapping) {
|
Chris@0
|
77 $mapping = RdfMapping::create([
|
Chris@0
|
78 'targetEntityType' => $entity_type,
|
Chris@0
|
79 'bundle' => $bundle,
|
Chris@0
|
80 ]);
|
Chris@0
|
81 }
|
Chris@0
|
82
|
Chris@0
|
83 return $mapping;
|
Chris@0
|
84 }
|
Chris@0
|
85
|
Chris@0
|
86 /**
|
Chris@0
|
87 * Implements hook_rdf_namespaces().
|
Chris@0
|
88 */
|
Chris@0
|
89 function rdf_rdf_namespaces() {
|
Chris@0
|
90 return [
|
Chris@0
|
91 'content' => 'http://purl.org/rss/1.0/modules/content/',
|
Chris@0
|
92 'dc' => 'http://purl.org/dc/terms/',
|
Chris@0
|
93 'foaf' => 'http://xmlns.com/foaf/0.1/',
|
Chris@0
|
94 'og' => 'http://ogp.me/ns#',
|
Chris@0
|
95 'rdfs' => 'http://www.w3.org/2000/01/rdf-schema#',
|
Chris@0
|
96 'schema' => 'http://schema.org/',
|
Chris@0
|
97 'sioc' => 'http://rdfs.org/sioc/ns#',
|
Chris@0
|
98 'sioct' => 'http://rdfs.org/sioc/types#',
|
Chris@0
|
99 'skos' => 'http://www.w3.org/2004/02/skos/core#',
|
Chris@0
|
100 'xsd' => 'http://www.w3.org/2001/XMLSchema#',
|
Chris@0
|
101 ];
|
Chris@0
|
102 }
|
Chris@0
|
103
|
Chris@0
|
104 /**
|
Chris@0
|
105 * Retrieves RDF namespaces.
|
Chris@0
|
106 *
|
Chris@0
|
107 * Invokes hook_rdf_namespaces() and collects RDF namespaces from modules that
|
Chris@0
|
108 * implement it.
|
Chris@0
|
109 */
|
Chris@0
|
110 function rdf_get_namespaces() {
|
Chris@0
|
111 $namespaces = [];
|
Chris@0
|
112 // In order to resolve duplicate namespaces by using the earliest defined
|
Chris@0
|
113 // namespace, do not use \Drupal::moduleHandler()->invokeAll().
|
Chris@0
|
114 foreach (\Drupal::moduleHandler()->getImplementations('rdf_namespaces') as $module) {
|
Chris@0
|
115 $function = $module . '_rdf_namespaces';
|
Chris@0
|
116 foreach ($function() as $prefix => $namespace) {
|
Chris@0
|
117 if (array_key_exists($prefix, $namespaces) && $namespace !== $namespaces[$prefix]) {
|
Chris@0
|
118 throw new Exception(t('Tried to map @prefix to @namespace, but @prefix is already mapped to @orig_namespace.', ['@prefix' => $prefix, '@namespace' => $namespace, '@orig_namespace' => $namespaces[$prefix]]));
|
Chris@0
|
119 }
|
Chris@0
|
120 else {
|
Chris@0
|
121 $namespaces[$prefix] = $namespace;
|
Chris@0
|
122 }
|
Chris@0
|
123 }
|
Chris@0
|
124 }
|
Chris@0
|
125 return $namespaces;
|
Chris@0
|
126 }
|
Chris@0
|
127
|
Chris@0
|
128 /**
|
Chris@0
|
129 * @} End of "defgroup rdf".
|
Chris@0
|
130 */
|
Chris@0
|
131
|
Chris@0
|
132 /**
|
Chris@0
|
133 * @addtogroup rdf
|
Chris@0
|
134 * @{
|
Chris@0
|
135 */
|
Chris@0
|
136
|
Chris@0
|
137 /**
|
Chris@0
|
138 * Builds an array of RDFa attributes for a given mapping.
|
Chris@0
|
139 *
|
Chris@0
|
140 * This array will typically be passed through Drupal\Core\Template\Attribute
|
Chris@0
|
141 * to create the attributes variables that are available to template files.
|
Chris@0
|
142 * These include $attributes, $title_attributes, $content_attributes and the
|
Chris@0
|
143 * field-specific $item_attributes variables.
|
Chris@0
|
144 *
|
Chris@0
|
145 * @param array $mapping
|
Chris@0
|
146 * An array containing a mandatory 'properties' key and optional 'datatype',
|
Chris@0
|
147 * 'datatype_callback' and 'type' keys. For example:
|
Chris@0
|
148 * @code
|
Chris@0
|
149 * array(
|
Chris@0
|
150 * 'properties' => array('schema:interactionCount'),
|
Chris@0
|
151 * 'datatype' => 'xsd:integer',
|
Chris@0
|
152 * 'datatype_callback' => array(
|
Chris@0
|
153 * 'callable' => 'Drupal\rdf\SchemaOrgDataConverter::interactionCount',
|
Chris@0
|
154 * 'arguments' => array(
|
Chris@0
|
155 * 'interaction_type' => 'UserComments'
|
Chris@0
|
156 * ),
|
Chris@0
|
157 * ),
|
Chris@0
|
158 * );
|
Chris@0
|
159 * @endcode
|
Chris@0
|
160 * @param mixed $data
|
Chris@0
|
161 * (optional) A value that needs to be converted by the provided callback
|
Chris@0
|
162 * function.
|
Chris@0
|
163 *
|
Chris@0
|
164 * @return array
|
Chris@0
|
165 * RDFa attributes suitable for Drupal\Core\Template\Attribute.
|
Chris@0
|
166 */
|
Chris@0
|
167 function rdf_rdfa_attributes($mapping, $data = NULL) {
|
Chris@0
|
168 $attributes = [];
|
Chris@0
|
169
|
Chris@0
|
170 // The type of mapping defaults to 'property'.
|
Chris@0
|
171 $type = isset($mapping['mapping_type']) ? $mapping['mapping_type'] : 'property';
|
Chris@0
|
172
|
Chris@0
|
173 switch ($type) {
|
Chris@0
|
174 // The mapping expresses the relationship between two resources.
|
Chris@0
|
175 case 'rel':
|
Chris@0
|
176 case 'rev':
|
Chris@0
|
177 $attributes[$type] = $mapping['properties'];
|
Chris@0
|
178 break;
|
Chris@0
|
179
|
Chris@0
|
180 // The mapping expresses the relationship between a resource and some
|
Chris@0
|
181 // literal text.
|
Chris@0
|
182 case 'property':
|
Chris@0
|
183 if (!empty($mapping['properties'])) {
|
Chris@0
|
184 $attributes['property'] = $mapping['properties'];
|
Chris@0
|
185 // Convert $data to a specific format as per the callback function.
|
Chris@0
|
186 if (isset($data) && !empty($mapping['datatype_callback'])) {
|
Chris@0
|
187 $callback = $mapping['datatype_callback']['callable'];
|
Chris@0
|
188 $arguments = isset($mapping['datatype_callback']['arguments']) ? $mapping['datatype_callback']['arguments'] : NULL;
|
Chris@0
|
189 $attributes['content'] = call_user_func($callback, $data, $arguments);
|
Chris@0
|
190 }
|
Chris@0
|
191 if (isset($mapping['datatype'])) {
|
Chris@0
|
192 $attributes['datatype'] = $mapping['datatype'];
|
Chris@0
|
193 }
|
Chris@0
|
194 break;
|
Chris@0
|
195 }
|
Chris@0
|
196 }
|
Chris@0
|
197
|
Chris@0
|
198 return $attributes;
|
Chris@0
|
199 }
|
Chris@0
|
200
|
Chris@0
|
201 /**
|
Chris@0
|
202 * @} End of "addtogroup rdf".
|
Chris@0
|
203 */
|
Chris@0
|
204
|
Chris@0
|
205 /**
|
Chris@0
|
206 * Implements hook_entity_prepare_view().
|
Chris@0
|
207 */
|
Chris@0
|
208 function rdf_entity_prepare_view($entity_type, array $entities, array $displays) {
|
Chris@0
|
209 // Iterate over the RDF mappings for each entity and prepare the RDFa
|
Chris@0
|
210 // attributes to be added inside field formatters.
|
Chris@0
|
211 foreach ($entities as $entity) {
|
Chris@0
|
212 $mapping = rdf_get_mapping($entity_type, $entity->bundle());
|
Chris@0
|
213 // Only prepare the RDFa attributes for the fields which are configured to
|
Chris@0
|
214 // be displayed.
|
Chris@0
|
215 foreach ($displays[$entity->bundle()]->getComponents() as $name => $options) {
|
Chris@0
|
216 $field_mapping = $mapping->getPreparedFieldMapping($name);
|
Chris@0
|
217 if ($field_mapping) {
|
Chris@0
|
218 foreach ($entity->get($name) as $item) {
|
Chris@0
|
219 $item->_attributes += rdf_rdfa_attributes($field_mapping, $item->toArray());
|
Chris@0
|
220 }
|
Chris@0
|
221 }
|
Chris@0
|
222 }
|
Chris@0
|
223 }
|
Chris@0
|
224 }
|
Chris@0
|
225
|
Chris@0
|
226 /**
|
Chris@0
|
227 * Implements hook_ENTITY_TYPE_storage_load() for comment entities.
|
Chris@0
|
228 */
|
Chris@0
|
229 function rdf_comment_storage_load($comments) {
|
Chris@0
|
230 foreach ($comments as $comment) {
|
Chris@0
|
231 // Pages with many comments can show poor performance. This information
|
Chris@0
|
232 // isn't needed until rdf_preprocess_comment() is called, but set it here
|
Chris@0
|
233 // to optimize performance for websites that implement an entity cache.
|
Chris@0
|
234 $created_mapping = rdf_get_mapping('comment', $comment->bundle())
|
Chris@0
|
235 ->getPreparedFieldMapping('created');
|
Chris@0
|
236 /** @var \Drupal\comment\CommentInterface $comment*/
|
Chris@0
|
237 $comment->rdf_data['date'] = rdf_rdfa_attributes($created_mapping, $comment->get('created')->first()->toArray());
|
Chris@0
|
238 $entity = $comment->getCommentedEntity();
|
Chris@0
|
239 // The current function is a storage level hook, so avoid to bubble
|
Chris@0
|
240 // bubbleable metadata, because it can be outside of a render context.
|
Chris@0
|
241 $comment->rdf_data['entity_uri'] = $entity->toUrl()->toString(TRUE)->getGeneratedUrl();
|
Chris@0
|
242 if ($comment->hasParentComment()) {
|
Chris@0
|
243 $comment->rdf_data['pid_uri'] = $comment->getParentComment()->url();
|
Chris@0
|
244 }
|
Chris@0
|
245 }
|
Chris@0
|
246 }
|
Chris@0
|
247
|
Chris@0
|
248 /**
|
Chris@0
|
249 * Implements hook_theme().
|
Chris@0
|
250 */
|
Chris@0
|
251 function rdf_theme() {
|
Chris@0
|
252 return [
|
Chris@0
|
253 'rdf_wrapper' => [
|
Chris@0
|
254 'variables' => ['attributes' => [], 'content' => NULL],
|
Chris@0
|
255 ],
|
Chris@0
|
256 'rdf_metadata' => [
|
Chris@0
|
257 'variables' => ['metadata' => []],
|
Chris@0
|
258 ],
|
Chris@0
|
259 ];
|
Chris@0
|
260 }
|
Chris@0
|
261
|
Chris@0
|
262 /**
|
Chris@0
|
263 * Implements hook_preprocess_HOOK() for HTML document templates.
|
Chris@0
|
264 */
|
Chris@0
|
265 function rdf_preprocess_html(&$variables) {
|
Chris@0
|
266 // Adds RDF namespace prefix bindings in the form of an RDFa 1.1 prefix
|
Chris@0
|
267 // attribute inside the html element.
|
Chris@0
|
268 if (!isset($variables['html_attributes']['prefix'])) {
|
Chris@0
|
269 $variables['html_attributes']['prefix'] = [];
|
Chris@0
|
270 }
|
Chris@0
|
271 foreach (rdf_get_namespaces() as $prefix => $uri) {
|
Chris@0
|
272 $variables['html_attributes']['prefix'][] = $prefix . ': ' . $uri . " ";
|
Chris@0
|
273 }
|
Chris@0
|
274 }
|
Chris@0
|
275
|
Chris@0
|
276 /**
|
Chris@0
|
277 * Implements hook_preprocess_HOOK() for UID field templates.
|
Chris@0
|
278 */
|
Chris@0
|
279 function rdf_preprocess_field__node__uid(&$variables) {
|
Chris@0
|
280 _rdf_set_field_rel_attribute($variables);
|
Chris@0
|
281 }
|
Chris@0
|
282
|
Chris@0
|
283 /**
|
Chris@0
|
284 * Transforms the field property attribute into a rel attribute.
|
Chris@0
|
285 */
|
Chris@0
|
286 function _rdf_set_field_rel_attribute(&$variables) {
|
Chris@0
|
287 // Swap the regular field property attribute and use the rel attribute
|
Chris@0
|
288 // instead so that it plays well with the RDFa markup when only a link is
|
Chris@0
|
289 // present in the field output, for example in the case of the uid field.
|
Chris@0
|
290 if (!empty($variables['attributes']['property'])) {
|
Chris@0
|
291 $variables['attributes']['rel'] = $variables['attributes']['property'];
|
Chris@0
|
292 unset($variables['attributes']['property']);
|
Chris@0
|
293 }
|
Chris@0
|
294 }
|
Chris@0
|
295
|
Chris@0
|
296
|
Chris@0
|
297 /**
|
Chris@0
|
298 * Implements hook_preprocess_HOOK() for node templates.
|
Chris@0
|
299 */
|
Chris@0
|
300 function rdf_preprocess_node(&$variables) {
|
Chris@0
|
301 // Adds RDFa markup to the node container. The about attribute specifies the
|
Chris@0
|
302 // URI of the resource described within the HTML element, while the @typeof
|
Chris@0
|
303 // attribute indicates its RDF type (e.g., foaf:Document, sioc:Person, and so
|
Chris@0
|
304 // on.)
|
Chris@0
|
305 $bundle = $variables['node']->bundle();
|
Chris@0
|
306 $mapping = rdf_get_mapping('node', $bundle);
|
Chris@0
|
307 $bundle_mapping = $mapping->getPreparedBundleMapping('node', $bundle);
|
Chris@0
|
308 $variables['attributes']['about'] = empty($variables['url']) ? NULL : $variables['url'];
|
Chris@0
|
309 $variables['attributes']['typeof'] = empty($bundle_mapping['types']) ? NULL : $bundle_mapping['types'];
|
Chris@0
|
310
|
Chris@0
|
311 // Adds RDFa markup for the node title as metadata because wrapping the title
|
Chris@0
|
312 // with markup is not reliable and the title output is different depending on
|
Chris@0
|
313 // the view mode (e.g. full vs. teaser).
|
Chris@0
|
314 $title_mapping = $mapping->getPreparedFieldMapping('title');
|
Chris@0
|
315 if ($title_mapping) {
|
Chris@0
|
316 $title_attributes['property'] = empty($title_mapping['properties']) ? NULL : $title_mapping['properties'];
|
Chris@0
|
317 $title_attributes['content'] = $variables['node']->label();
|
Chris@0
|
318 $variables['title_suffix']['rdf_meta_title'] = [
|
Chris@0
|
319 '#theme' => 'rdf_metadata',
|
Chris@0
|
320 '#metadata' => [$title_attributes],
|
Chris@0
|
321 ];
|
Chris@0
|
322 }
|
Chris@0
|
323
|
Chris@0
|
324 // Adds RDFa markup for the date.
|
Chris@0
|
325 $created_mapping = $mapping->getPreparedFieldMapping('created');
|
Chris@0
|
326 if (!empty($created_mapping) && $variables['display_submitted']) {
|
Chris@0
|
327 $date_attributes = rdf_rdfa_attributes($created_mapping, $variables['node']->get('created')->first()->toArray());
|
Chris@0
|
328 $rdf_metadata = [
|
Chris@0
|
329 '#theme' => 'rdf_metadata',
|
Chris@0
|
330 '#metadata' => [$date_attributes],
|
Chris@0
|
331 ];
|
Chris@0
|
332 $variables['metadata'] = \Drupal::service('renderer')->render($rdf_metadata);
|
Chris@0
|
333 }
|
Chris@0
|
334
|
Chris@0
|
335 // Adds RDFa markup annotating the number of comments a node has.
|
Chris@0
|
336 if (\Drupal::moduleHandler()->moduleExists('comment') && \Drupal::currentUser()->hasPermission('access comments')) {
|
Chris@0
|
337 $comment_count_mapping = $mapping->getPreparedFieldMapping('comment_count');
|
Chris@0
|
338 if (!empty($comment_count_mapping['properties'])) {
|
Chris@0
|
339 $fields = array_keys(\Drupal::service('comment.manager')->getFields('node'));
|
Chris@0
|
340 $definitions = array_keys($variables['node']->getFieldDefinitions());
|
Chris@0
|
341 $valid_fields = array_intersect($fields, $definitions);
|
Chris@0
|
342 $count = 0;
|
Chris@0
|
343 foreach ($valid_fields as $field_name) {
|
Chris@0
|
344 $count += $variables['node']->get($field_name)->comment_count;
|
Chris@0
|
345 // Adds RDFa markup for the comment count near the node title as
|
Chris@0
|
346 // metadata.
|
Chris@0
|
347 $comment_count_attributes = rdf_rdfa_attributes($comment_count_mapping, $variables['node']->get($field_name)->comment_count);
|
Chris@0
|
348 $variables['title_suffix']['rdf_meta_comment_count'] = [
|
Chris@0
|
349 '#theme' => 'rdf_metadata',
|
Chris@0
|
350 '#metadata' => [$comment_count_attributes],
|
Chris@0
|
351 ];
|
Chris@0
|
352 }
|
Chris@0
|
353 }
|
Chris@0
|
354 }
|
Chris@0
|
355 }
|
Chris@0
|
356
|
Chris@0
|
357 /**
|
Chris@0
|
358 * Implements hook_preprocess_HOOK() for user templates.
|
Chris@0
|
359 */
|
Chris@0
|
360 function rdf_preprocess_user(&$variables) {
|
Chris@0
|
361 /** @var $account \Drupal\user\UserInterface */
|
Chris@0
|
362 $account = $variables['elements']['#user'];
|
Chris@0
|
363 $uri = $account->urlInfo();
|
Chris@0
|
364 $mapping = rdf_get_mapping('user', 'user');
|
Chris@0
|
365 $bundle_mapping = $mapping->getPreparedBundleMapping();
|
Chris@0
|
366
|
Chris@0
|
367 // Adds RDFa markup to the user profile page. Fields displayed in this page
|
Chris@0
|
368 // will automatically describe the user.
|
Chris@0
|
369 if (!empty($bundle_mapping['types'])) {
|
Chris@0
|
370 $variables['attributes']['typeof'] = $bundle_mapping['types'];
|
Chris@0
|
371 $variables['attributes']['about'] = $account->url();
|
Chris@0
|
372 }
|
Chris@0
|
373 // If we are on the user account page, add the relationship between the
|
Chris@0
|
374 // sioc:UserAccount and the foaf:Person who holds the account.
|
Chris@0
|
375 if (\Drupal::routeMatch()->getRouteName() == $uri->getRouteName()) {
|
Chris@0
|
376 // Adds the markup for username as language neutral literal, see
|
Chris@0
|
377 // rdf_preprocess_username().
|
Chris@0
|
378 $name_mapping = $mapping->getPreparedFieldMapping('name');
|
Chris@0
|
379 if (!empty($name_mapping['properties'])) {
|
Chris@0
|
380 $username_meta = [
|
Chris@0
|
381 '#tag' => 'meta',
|
Chris@0
|
382 '#attributes' => [
|
Chris@0
|
383 'about' => $account->url(),
|
Chris@0
|
384 'property' => $name_mapping['properties'],
|
Chris@0
|
385 'content' => $account->getDisplayName(),
|
Chris@0
|
386 'lang' => '',
|
Chris@0
|
387 ],
|
Chris@0
|
388 ];
|
Chris@0
|
389 $variables['#attached']['html_head'][] = [$username_meta, 'rdf_user_username'];
|
Chris@0
|
390 }
|
Chris@0
|
391 }
|
Chris@0
|
392 }
|
Chris@0
|
393
|
Chris@0
|
394 /**
|
Chris@0
|
395 * Implements hook_preprocess_HOOK() for username.html.twig.
|
Chris@0
|
396 */
|
Chris@0
|
397 function rdf_preprocess_username(&$variables) {
|
Chris@0
|
398 // Because lang is set on the HTML element that wraps the page, the
|
Chris@0
|
399 // username inherits this language attribute. However, since the username
|
Chris@0
|
400 // might not be transliterated to the same language that the content is in,
|
Chris@0
|
401 // we do not want it to inherit the language attribute, so we set the
|
Chris@0
|
402 // attribute to an empty string.
|
Chris@0
|
403 if (empty($variables['attributes']['lang'])) {
|
Chris@0
|
404 $variables['attributes']['lang'] = '';
|
Chris@0
|
405 }
|
Chris@0
|
406
|
Chris@0
|
407 // The profile URI is used to identify the user account. The about attribute
|
Chris@0
|
408 // is used to set the URI as the default subject of the properties embedded
|
Chris@0
|
409 // as RDFa in the child elements. Even if the user profile is not accessible
|
Chris@0
|
410 // to the current user, we use its URI in order to identify the user in RDF.
|
Chris@0
|
411 // We do not use this attribute for the anonymous user because we do not have
|
Chris@0
|
412 // a user profile URI for it (only a homepage which cannot be used as user
|
Chris@0
|
413 // profile in RDF.)
|
Chris@0
|
414 if ($variables['uid'] > 0) {
|
Chris@0
|
415 $variables['attributes']['about'] = \Drupal::url('entity.user.canonical', ['user' => $variables['uid']]);
|
Chris@0
|
416 }
|
Chris@0
|
417
|
Chris@0
|
418 // Add RDF type of user.
|
Chris@0
|
419 $mapping = rdf_get_mapping('user', 'user');
|
Chris@0
|
420 $bundle_mapping = $mapping->getPreparedBundleMapping();
|
Chris@0
|
421 if (!empty($bundle_mapping['types'])) {
|
Chris@0
|
422 $variables['attributes']['typeof'] = $bundle_mapping['types'];
|
Chris@0
|
423 }
|
Chris@0
|
424 // Annotate the username in RDFa. A property attribute is used with an empty
|
Chris@0
|
425 // datatype attribute to ensure the username is parsed as a plain literal
|
Chris@0
|
426 // in RDFa 1.0 and 1.1.
|
Chris@0
|
427 $name_mapping = $mapping->getPreparedFieldMapping('name');
|
Chris@0
|
428 if (!empty($name_mapping)) {
|
Chris@0
|
429 $variables['attributes']['property'] = $name_mapping['properties'];
|
Chris@0
|
430 $variables['attributes']['datatype'] = '';
|
Chris@0
|
431 }
|
Chris@0
|
432 // Add the homepage RDFa markup if present.
|
Chris@0
|
433 $homepage_mapping = $mapping->getPreparedFieldMapping('homepage');
|
Chris@0
|
434 if (!empty($variables['homepage']) && !empty($homepage_mapping)) {
|
Chris@0
|
435 $variables['attributes']['rel'] = $homepage_mapping['properties'];
|
Chris@0
|
436 }
|
Chris@0
|
437 // Long usernames are truncated by template_preprocess_username(). Store the
|
Chris@0
|
438 // full name in the content attribute so it can be extracted in RDFa.
|
Chris@0
|
439 if ($variables['truncated']) {
|
Chris@0
|
440 $variables['attributes']['content'] = $variables['name_raw'];
|
Chris@0
|
441 }
|
Chris@0
|
442 }
|
Chris@0
|
443
|
Chris@0
|
444 /**
|
Chris@0
|
445 * Implements hook_preprocess_HOOK() for comment templates.
|
Chris@0
|
446 */
|
Chris@0
|
447 function rdf_preprocess_comment(&$variables) {
|
Chris@0
|
448 $comment = $variables['comment'];
|
Chris@0
|
449 $mapping = rdf_get_mapping('comment', $comment->bundle());
|
Chris@0
|
450 $bundle_mapping = $mapping->getPreparedBundleMapping();
|
Chris@0
|
451
|
Chris@0
|
452 if (!empty($bundle_mapping['types']) && !isset($comment->in_preview)) {
|
Chris@0
|
453 // Adds RDFa markup to the comment container. The about attribute specifies
|
Chris@0
|
454 // the URI of the resource described within the HTML element, while the
|
Chris@0
|
455 // typeof attribute indicates its RDF type (e.g., sioc:Post, foaf:Document,
|
Chris@0
|
456 // and so on.)
|
Chris@0
|
457 $variables['attributes']['about'] = $comment->url();
|
Chris@0
|
458 $variables['attributes']['typeof'] = $bundle_mapping['types'];
|
Chris@0
|
459 }
|
Chris@0
|
460
|
Chris@0
|
461 // Adds RDFa markup for the relation between the comment and its author.
|
Chris@0
|
462 $author_mapping = $mapping->getPreparedFieldMapping('uid');
|
Chris@0
|
463 if (!empty($author_mapping)) {
|
Chris@0
|
464 $author_attributes = ['rel' => $author_mapping['properties']];
|
Chris@0
|
465 // Wraps the 'author' and 'submitted' variables which are both available in
|
Chris@0
|
466 // comment.html.twig.
|
Chris@0
|
467 $variables['author'] = [
|
Chris@0
|
468 '#theme' => 'rdf_wrapper',
|
Chris@0
|
469 '#content' => $variables['author'],
|
Chris@0
|
470 '#attributes' => $author_attributes,
|
Chris@0
|
471 ];
|
Chris@0
|
472 $variables['submitted'] = [
|
Chris@0
|
473 '#theme' => 'rdf_wrapper',
|
Chris@0
|
474 '#content' => $variables['submitted'],
|
Chris@0
|
475 '#attributes' => $author_attributes,
|
Chris@0
|
476 ];
|
Chris@0
|
477 }
|
Chris@0
|
478 // Adds RDFa markup for the date of the comment.
|
Chris@0
|
479 $created_mapping = $mapping->getPreparedFieldMapping('created');
|
Chris@0
|
480 if (!empty($created_mapping)) {
|
Chris@0
|
481 // The comment date is precomputed as part of the rdf_data so that it can be
|
Chris@0
|
482 // cached as part of the entity.
|
Chris@0
|
483 $date_attributes = $comment->rdf_data['date'];
|
Chris@0
|
484
|
Chris@0
|
485 $rdf_metadata = [
|
Chris@0
|
486 '#theme' => 'rdf_metadata',
|
Chris@0
|
487 '#metadata' => [$date_attributes],
|
Chris@0
|
488 ];
|
Chris@0
|
489 // Ensure the original variable is represented as a render array.
|
Chris@0
|
490 $created = !is_array($variables['created']) ? ['#markup' => $variables['created']] : $variables['created'];
|
Chris@0
|
491 $submitted = !is_array($variables['submitted']) ? ['#markup' => $variables['submitted']] : $variables['submitted'];
|
Chris@0
|
492 // Make render array and RDF metadata available in comment.html.twig.
|
Chris@0
|
493 $variables['created'] = [$created, $rdf_metadata];
|
Chris@0
|
494 $variables['submitted'] = [$submitted, $rdf_metadata];
|
Chris@0
|
495 }
|
Chris@0
|
496 $title_mapping = $mapping->getPreparedFieldMapping('subject');
|
Chris@0
|
497 if (!empty($title_mapping)) {
|
Chris@0
|
498 // Adds RDFa markup to the subject of the comment. Because the RDFa markup
|
Chris@0
|
499 // is added to an <h3> tag which might contain HTML code, we specify an
|
Chris@0
|
500 // empty datatype to ensure the value of the title read by the RDFa parsers
|
Chris@0
|
501 // is a literal.
|
Chris@0
|
502 $variables['title_attributes']['property'] = $title_mapping['properties'];
|
Chris@0
|
503 $variables['title_attributes']['datatype'] = '';
|
Chris@0
|
504 }
|
Chris@0
|
505
|
Chris@0
|
506 // Annotates the parent relationship between the current comment and the node
|
Chris@0
|
507 // it belongs to. If available, the parent comment is also annotated.
|
Chris@0
|
508 // @todo When comments are turned into fields, this should be changed.
|
Chris@0
|
509 // Currently there is no mapping relating a comment to its node.
|
Chris@0
|
510 $pid_mapping = $mapping->getPreparedFieldMapping('pid');
|
Chris@0
|
511 if (!empty($pid_mapping)) {
|
Chris@0
|
512 // Adds the relation to the parent entity.
|
Chris@0
|
513 $parent_entity_attributes['rel'] = $pid_mapping['properties'];
|
Chris@0
|
514 // The parent entity URI is precomputed as part of the rdf_data so that it
|
Chris@0
|
515 // can be cached as part of the entity.
|
Chris@0
|
516 $parent_entity_attributes['resource'] = $comment->rdf_data['entity_uri'];
|
Chris@0
|
517 $variables['rdf_metadata_attributes'][] = $parent_entity_attributes;
|
Chris@0
|
518
|
Chris@0
|
519 // Adds the relation to parent comment, if it exists.
|
Chris@0
|
520 if ($comment->hasParentComment()) {
|
Chris@0
|
521 $parent_comment_attributes['rel'] = $pid_mapping['properties'];
|
Chris@0
|
522 // The parent comment URI is precomputed as part of the rdf_data so that
|
Chris@0
|
523 // it can be cached as part of the entity.
|
Chris@0
|
524 $parent_comment_attributes['resource'] = $comment->rdf_data['pid_uri'];
|
Chris@0
|
525 $variables['rdf_metadata_attributes'][] = $parent_comment_attributes;
|
Chris@0
|
526 }
|
Chris@0
|
527 }
|
Chris@0
|
528 // Adds RDF metadata markup above comment body if any.
|
Chris@0
|
529 if (!empty($variables['rdf_metadata_attributes']) && isset($variables['content']['comment_body'])) {
|
Chris@0
|
530 $rdf_metadata = [
|
Chris@0
|
531 '#theme' => 'rdf_metadata',
|
Chris@0
|
532 '#metadata' => $variables['rdf_metadata_attributes'],
|
Chris@0
|
533 ];
|
Chris@0
|
534 if (!empty($variables['content']['comment_body']['#prefix'])) {
|
Chris@0
|
535 $rdf_metadata['#suffix'] = $variables['content']['comment_body']['#prefix'];
|
Chris@0
|
536 }
|
Chris@0
|
537 $variables['content']['comment_body']['#prefix'] = \Drupal::service('renderer')->render($rdf_metadata);
|
Chris@0
|
538 }
|
Chris@0
|
539 }
|
Chris@0
|
540
|
Chris@0
|
541 /**
|
Chris@0
|
542 * Implements hook_preprocess_HOOK() for taxonomy term templates.
|
Chris@0
|
543 */
|
Chris@0
|
544 function rdf_preprocess_taxonomy_term(&$variables) {
|
Chris@0
|
545 // Adds RDFa markup to the taxonomy term container.
|
Chris@0
|
546 // The @about attribute specifies the URI of the resource described within
|
Chris@0
|
547 // the HTML element, while the @typeof attribute indicates its RDF type
|
Chris@0
|
548 // (e.g., schema:Thing, skos:Concept, and so on).
|
Chris@0
|
549 $term = $variables['term'];
|
Chris@0
|
550 $mapping = rdf_get_mapping('taxonomy_term', $term->bundle());
|
Chris@0
|
551 $bundle_mapping = $mapping->getPreparedBundleMapping();
|
Chris@0
|
552 $variables['attributes']['about'] = $term->url();
|
Chris@0
|
553 $variables['attributes']['typeof'] = empty($bundle_mapping['types']) ? NULL : $bundle_mapping['types'];
|
Chris@0
|
554
|
Chris@0
|
555 // Add RDFa markup for the taxonomy term name as metadata, if present.
|
Chris@0
|
556 $name_field_mapping = $mapping->getPreparedFieldMapping('name');
|
Chris@0
|
557 if (!empty($name_field_mapping) && !empty($name_field_mapping['properties'])) {
|
Chris@0
|
558 $name_attributes = [
|
Chris@0
|
559 'property' => $name_field_mapping['properties'],
|
Chris@0
|
560 'content' => $term->getName(),
|
Chris@0
|
561 ];
|
Chris@0
|
562 $variables['title_suffix']['taxonomy_term_rdfa'] = [
|
Chris@0
|
563 '#theme' => 'rdf_metadata',
|
Chris@0
|
564 '#metadata' => [$name_attributes],
|
Chris@0
|
565 ];
|
Chris@0
|
566 }
|
Chris@0
|
567 }
|
Chris@0
|
568
|
Chris@0
|
569 /**
|
Chris@0
|
570 * Implements hook_preprocess_HOOK() for image.html.twig.
|
Chris@0
|
571 */
|
Chris@0
|
572 function rdf_preprocess_image(&$variables) {
|
Chris@0
|
573 // Adds the RDF type for image. We cannot use the usual entity-based mapping
|
Chris@0
|
574 // to get 'foaf:Image' because image does not have its own entity type or
|
Chris@0
|
575 // bundle.
|
Chris@0
|
576 $variables['attributes']['typeof'] = ['foaf:Image'];
|
Chris@0
|
577 }
|
Chris@0
|
578
|
Chris@0
|
579 /**
|
Chris@0
|
580 * Prepares variables for RDF metadata templates.
|
Chris@0
|
581 *
|
Chris@0
|
582 * Default template: rdf-metadata.html.twig.
|
Chris@0
|
583 *
|
Chris@0
|
584 * Sometimes it is useful to export data which is not semantically present in
|
Chris@0
|
585 * the HTML output. For example, a hierarchy of comments is visible for a human
|
Chris@0
|
586 * but not for machines because this hierarchy is not present in the DOM tree.
|
Chris@0
|
587 * We can express it in RDFa via empty <span> tags. These aren't visible and
|
Chris@0
|
588 * give machines extra information about the content and its structure.
|
Chris@0
|
589 *
|
Chris@0
|
590 * @param array $variables
|
Chris@0
|
591 * An associative array containing:
|
Chris@0
|
592 * - metadata: An array of attribute arrays. Each item in the array
|
Chris@0
|
593 * corresponds to its own set of attributes, and therefore, needs its own
|
Chris@0
|
594 * element.
|
Chris@0
|
595 */
|
Chris@0
|
596 function template_preprocess_rdf_metadata(&$variables) {
|
Chris@0
|
597 foreach ($variables['metadata'] as $key => $attributes) {
|
Chris@0
|
598 if (!is_null($attributes)) {
|
Chris@0
|
599 $variables['metadata'][$key] = new Attribute($attributes);
|
Chris@0
|
600 }
|
Chris@0
|
601 else {
|
Chris@0
|
602 $variables['metadata'][$key] = new Attribute();
|
Chris@0
|
603 }
|
Chris@0
|
604 }
|
Chris@0
|
605 }
|