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