comparison core/modules/rdf/rdf.module @ 18:af1871eacc83

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:33:08 +0100
parents 129ea1e6d783
children
comparison
equal deleted inserted replaced
17:129ea1e6d783 18:af1871eacc83
3 /** 3 /**
4 * @file 4 * @file
5 * Enables semantically enriched output for Drupal sites in the form of RDFa. 5 * Enables semantically enriched output for Drupal sites in the form of RDFa.
6 */ 6 */
7 7
8 use Drupal\Core\Url;
8 use Drupal\Core\Routing\RouteMatchInterface; 9 use Drupal\Core\Routing\RouteMatchInterface;
9 use Drupal\Core\Template\Attribute; 10 use Drupal\Core\Template\Attribute;
10 use Drupal\rdf\Entity\RdfMapping; 11 use Drupal\rdf\Entity\RdfMapping;
11 12
12 /** 13 /**
239 $entity = $comment->getCommentedEntity(); 240 $entity = $comment->getCommentedEntity();
240 // The current function is a storage level hook, so avoid to bubble 241 // The current function is a storage level hook, so avoid to bubble
241 // bubbleable metadata, because it can be outside of a render context. 242 // bubbleable metadata, because it can be outside of a render context.
242 $comment->rdf_data['entity_uri'] = $entity->toUrl()->toString(TRUE)->getGeneratedUrl(); 243 $comment->rdf_data['entity_uri'] = $entity->toUrl()->toString(TRUE)->getGeneratedUrl();
243 if ($comment->hasParentComment()) { 244 if ($comment->hasParentComment()) {
244 $comment->rdf_data['pid_uri'] = $comment->getParentComment()->url(); 245 $comment->rdf_data['pid_uri'] = $comment->getParentComment()->toUrl()->toString();
245 } 246 }
246 } 247 }
247 } 248 }
248 249
249 /** 250 /**
321 ]; 322 ];
322 } 323 }
323 324
324 // Adds RDFa markup for the date. 325 // Adds RDFa markup for the date.
325 $created_mapping = $mapping->getPreparedFieldMapping('created'); 326 $created_mapping = $mapping->getPreparedFieldMapping('created');
326 if (!empty($created_mapping) && $variables['display_submitted']) { 327 if (!empty($created_mapping)) {
327 $date_attributes = rdf_rdfa_attributes($created_mapping, $variables['node']->get('created')->first()->toArray()); 328 $date_attributes = rdf_rdfa_attributes($created_mapping, $variables['node']->get('created')->first()->toArray());
328 $rdf_metadata = [ 329 $rdf_metadata = [
329 '#theme' => 'rdf_metadata', 330 '#theme' => 'rdf_metadata',
330 '#metadata' => [$date_attributes], 331 '#metadata' => [$date_attributes],
331 ]; 332 ];
332 $variables['metadata'] = \Drupal::service('renderer')->render($rdf_metadata); 333
334 // Depending on whether custom preprocessing is enabled, the 'created'
335 // field may appear in either of two different places, so check both of
336 // those places here.
337 // @see template_preprocess_node.
338 if (!empty($variables['display_submitted'])) {
339 // If custom preprocessing is enabled, then detect if the 'created'
340 // field is displayed by checking the 'display_submitted' variable. In
341 // this case, for back-compatibility, put the metadata into a special
342 // variable.
343 $variables['metadata'] = \Drupal::service('renderer')->render($rdf_metadata);
344 }
345 elseif (isset($variables['elements']['created'])) {
346 // Otherwise, detect if the 'created' field is displayed by checking if
347 // it is present in the 'elements variable. Put the metadata into
348 // title_suffix, along with other metadata added by this module.
349 $variables['title_suffix']['rdf_meta_created'] = $rdf_metadata;
350 }
333 } 351 }
334 352
335 // Adds RDFa markup annotating the number of comments a node has. 353 // Adds RDFa markup annotating the number of comments a node has.
336 if (\Drupal::moduleHandler()->moduleExists('comment') && \Drupal::currentUser()->hasPermission('access comments')) { 354 if (\Drupal::moduleHandler()->moduleExists('comment') && \Drupal::currentUser()->hasPermission('access comments')) {
337 $comment_count_mapping = $mapping->getPreparedFieldMapping('comment_count'); 355 $comment_count_mapping = $mapping->getPreparedFieldMapping('comment_count');
358 * Implements hook_preprocess_HOOK() for user templates. 376 * Implements hook_preprocess_HOOK() for user templates.
359 */ 377 */
360 function rdf_preprocess_user(&$variables) { 378 function rdf_preprocess_user(&$variables) {
361 /** @var $account \Drupal\user\UserInterface */ 379 /** @var $account \Drupal\user\UserInterface */
362 $account = $variables['elements']['#user']; 380 $account = $variables['elements']['#user'];
363 $uri = $account->urlInfo(); 381 $uri = $account->toUrl();
364 $mapping = rdf_get_mapping('user', 'user'); 382 $mapping = rdf_get_mapping('user', 'user');
365 $bundle_mapping = $mapping->getPreparedBundleMapping(); 383 $bundle_mapping = $mapping->getPreparedBundleMapping();
366 384
367 // Adds RDFa markup to the user profile page. Fields displayed in this page 385 // Adds RDFa markup to the user profile page. Fields displayed in this page
368 // will automatically describe the user. 386 // will automatically describe the user.
369 if (!empty($bundle_mapping['types'])) { 387 if (!empty($bundle_mapping['types'])) {
370 $variables['attributes']['typeof'] = $bundle_mapping['types']; 388 $variables['attributes']['typeof'] = $bundle_mapping['types'];
371 $variables['attributes']['about'] = $account->url(); 389 $variables['attributes']['about'] = $account->toUrl()->toString();
372 } 390 }
373 // If we are on the user account page, add the relationship between the 391 // If we are on the user account page, add the relationship between the
374 // sioc:UserAccount and the foaf:Person who holds the account. 392 // sioc:UserAccount and the foaf:Person who holds the account.
375 if (\Drupal::routeMatch()->getRouteName() == $uri->getRouteName()) { 393 if (\Drupal::routeMatch()->getRouteName() == $uri->getRouteName()) {
376 // Adds the markup for username as language neutral literal, see 394 // Adds the markup for username as language neutral literal, see
378 $name_mapping = $mapping->getPreparedFieldMapping('name'); 396 $name_mapping = $mapping->getPreparedFieldMapping('name');
379 if (!empty($name_mapping['properties'])) { 397 if (!empty($name_mapping['properties'])) {
380 $username_meta = [ 398 $username_meta = [
381 '#tag' => 'meta', 399 '#tag' => 'meta',
382 '#attributes' => [ 400 '#attributes' => [
383 'about' => $account->url(), 401 'about' => $account->toUrl()->toString(),
384 'property' => $name_mapping['properties'], 402 'property' => $name_mapping['properties'],
385 'content' => $account->getDisplayName(), 403 'content' => $account->getDisplayName(),
386 'lang' => '', 404 'lang' => '',
387 ], 405 ],
388 ]; 406 ];
410 // to the current user, we use its URI in order to identify the user in RDF. 428 // to the current user, we use its URI in order to identify the user in RDF.
411 // We do not use this attribute for the anonymous user because we do not have 429 // We do not use this attribute for the anonymous user because we do not have
412 // a user profile URI for it (only a homepage which cannot be used as user 430 // a user profile URI for it (only a homepage which cannot be used as user
413 // profile in RDF.) 431 // profile in RDF.)
414 if ($variables['uid'] > 0) { 432 if ($variables['uid'] > 0) {
415 $variables['attributes']['about'] = \Drupal::url('entity.user.canonical', ['user' => $variables['uid']]); 433 $variables['attributes']['about'] = Url::fromRoute('entity.user.canonical', ['user' => $variables['uid']])->toString();
416 } 434 }
417 435
418 // Add RDF type of user. 436 // Add RDF type of user.
419 $mapping = rdf_get_mapping('user', 'user'); 437 $mapping = rdf_get_mapping('user', 'user');
420 $bundle_mapping = $mapping->getPreparedBundleMapping(); 438 $bundle_mapping = $mapping->getPreparedBundleMapping();
452 if (!empty($bundle_mapping['types']) && !isset($comment->in_preview)) { 470 if (!empty($bundle_mapping['types']) && !isset($comment->in_preview)) {
453 // Adds RDFa markup to the comment container. The about attribute specifies 471 // Adds RDFa markup to the comment container. The about attribute specifies
454 // the URI of the resource described within the HTML element, while the 472 // the URI of the resource described within the HTML element, while the
455 // typeof attribute indicates its RDF type (e.g., sioc:Post, foaf:Document, 473 // typeof attribute indicates its RDF type (e.g., sioc:Post, foaf:Document,
456 // and so on.) 474 // and so on.)
457 $variables['attributes']['about'] = $comment->url(); 475 $variables['attributes']['about'] = $comment->toUrl()->toString();
458 $variables['attributes']['typeof'] = $bundle_mapping['types']; 476 $variables['attributes']['typeof'] = $bundle_mapping['types'];
459 } 477 }
460 478
461 // Adds RDFa markup for the relation between the comment and its author. 479 // Adds RDFa markup for the relation between the comment and its author.
462 $author_mapping = $mapping->getPreparedFieldMapping('uid'); 480 $author_mapping = $mapping->getPreparedFieldMapping('uid');
547 // the HTML element, while the @typeof attribute indicates its RDF type 565 // the HTML element, while the @typeof attribute indicates its RDF type
548 // (e.g., schema:Thing, skos:Concept, and so on). 566 // (e.g., schema:Thing, skos:Concept, and so on).
549 $term = $variables['term']; 567 $term = $variables['term'];
550 $mapping = rdf_get_mapping('taxonomy_term', $term->bundle()); 568 $mapping = rdf_get_mapping('taxonomy_term', $term->bundle());
551 $bundle_mapping = $mapping->getPreparedBundleMapping(); 569 $bundle_mapping = $mapping->getPreparedBundleMapping();
552 $variables['attributes']['about'] = $term->url(); 570 $variables['attributes']['about'] = $term->toUrl()->toString();
553 $variables['attributes']['typeof'] = empty($bundle_mapping['types']) ? NULL : $bundle_mapping['types']; 571 $variables['attributes']['typeof'] = empty($bundle_mapping['types']) ? NULL : $bundle_mapping['types'];
554 572
555 // Add RDFa markup for the taxonomy term name as metadata, if present. 573 // Add RDFa markup for the taxonomy term name as metadata, if present.
556 $name_field_mapping = $mapping->getPreparedFieldMapping('name'); 574 $name_field_mapping = $mapping->getPreparedFieldMapping('name');
557 if (!empty($name_field_mapping) && !empty($name_field_mapping['properties'])) { 575 if (!empty($name_field_mapping) && !empty($name_field_mapping['properties'])) {