Chris@0: ' . t('About') . ''; Chris@0: $output .= '

' . 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 RDFa specification, 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 online documentation for the RDF module.', [':rdfa' => 'http://www.w3.org/TR/xhtml-rdfa-primer/', ':rdf' => 'https://www.drupal.org/documentation/modules/rdf']) . '

'; Chris@0: return $output; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * @defgroup rdf RDF Mapping API Chris@0: * @{ Chris@0: * Functions to describe entities and bundles in RDF. Chris@0: * Chris@0: * The RDF module introduces RDF and RDFa to Drupal. RDF is a W3C standard to Chris@0: * describe structured data. RDF can be serialized as RDFa in XHTML attributes Chris@0: * to augment visual data with machine-readable hints. Chris@0: * @see http://www.w3.org/RDF/ Chris@0: * @see http://www.w3.org/TR/xhtml-rdfa-primer/ Chris@0: * Chris@0: * Modules can provide mappings of their bundles' data and metadata to RDF Chris@0: * classes and properties. This module takes care of injecting these mappings Chris@0: * into variables available to theme functions and templates. All Drupal core Chris@0: * themes are coded to be RDFa compatible. Chris@0: */ Chris@17: Chris@0: /** Chris@0: * Returns the RDF mapping object associated with a bundle. Chris@0: * Chris@0: * The function reads the rdf_mapping object from the current configuration, Chris@0: * or returns a ready-to-use empty one if no configuration entry exists yet for Chris@0: * this bundle. This streamlines the manipulation of mapping objects by always Chris@0: * returning a consistent object that reflects the current state of the Chris@0: * configuration. Chris@0: * Chris@0: * Example usage: Chris@0: * -Map the 'article' bundle to 'sioc:Post' and the 'title' field to 'dc:title'. Chris@0: * @code Chris@0: * rdf_get_mapping('node', 'article') Chris@0: * ->setBundleMapping(array( Chris@0: * 'types' => array('sioc:Post'), Chris@0: * )) Chris@0: * ->setFieldMapping('title', array( Chris@0: * 'properties' => array('dc:title') Chris@0: * )) Chris@0: * ->save(); Chris@0: * @endcode Chris@0: * Chris@0: * @param string $entity_type Chris@0: * The entity type. Chris@0: * @param string $bundle Chris@0: * The bundle. Chris@0: * Chris@0: * @return \Drupal\rdf\Entity\RdfMapping Chris@0: * The RdfMapping object. Chris@0: */ Chris@0: function rdf_get_mapping($entity_type, $bundle) { Chris@0: // Try loading the mapping from configuration. Chris@0: $mapping = RdfMapping::load($entity_type . '.' . $bundle); Chris@0: Chris@0: // If not found, create a fresh mapping object. Chris@0: if (!$mapping) { Chris@0: $mapping = RdfMapping::create([ Chris@0: 'targetEntityType' => $entity_type, Chris@0: 'bundle' => $bundle, Chris@0: ]); Chris@0: } Chris@0: Chris@0: return $mapping; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Implements hook_rdf_namespaces(). Chris@0: */ Chris@0: function rdf_rdf_namespaces() { Chris@0: return [ Chris@0: 'content' => 'http://purl.org/rss/1.0/modules/content/', Chris@0: 'dc' => 'http://purl.org/dc/terms/', Chris@0: 'foaf' => 'http://xmlns.com/foaf/0.1/', Chris@0: 'og' => 'http://ogp.me/ns#', Chris@0: 'rdfs' => 'http://www.w3.org/2000/01/rdf-schema#', Chris@0: 'schema' => 'http://schema.org/', Chris@0: 'sioc' => 'http://rdfs.org/sioc/ns#', Chris@0: 'sioct' => 'http://rdfs.org/sioc/types#', Chris@0: 'skos' => 'http://www.w3.org/2004/02/skos/core#', Chris@0: 'xsd' => 'http://www.w3.org/2001/XMLSchema#', Chris@0: ]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Retrieves RDF namespaces. Chris@0: * Chris@0: * Invokes hook_rdf_namespaces() and collects RDF namespaces from modules that Chris@0: * implement it. Chris@0: */ Chris@0: function rdf_get_namespaces() { Chris@0: $namespaces = []; Chris@0: // In order to resolve duplicate namespaces by using the earliest defined Chris@0: // namespace, do not use \Drupal::moduleHandler()->invokeAll(). Chris@0: foreach (\Drupal::moduleHandler()->getImplementations('rdf_namespaces') as $module) { Chris@0: $function = $module . '_rdf_namespaces'; Chris@0: foreach ($function() as $prefix => $namespace) { Chris@0: if (array_key_exists($prefix, $namespaces) && $namespace !== $namespaces[$prefix]) { Chris@0: 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: } Chris@0: else { Chris@0: $namespaces[$prefix] = $namespace; Chris@0: } Chris@0: } Chris@0: } Chris@0: return $namespaces; Chris@0: } Chris@0: Chris@0: /** Chris@0: * @} End of "defgroup rdf". Chris@0: */ Chris@0: Chris@0: /** Chris@0: * @addtogroup rdf Chris@0: * @{ Chris@0: */ Chris@0: Chris@0: /** Chris@0: * Builds an array of RDFa attributes for a given mapping. Chris@0: * Chris@0: * This array will typically be passed through Drupal\Core\Template\Attribute Chris@0: * to create the attributes variables that are available to template files. Chris@0: * These include $attributes, $title_attributes, $content_attributes and the Chris@0: * field-specific $item_attributes variables. Chris@0: * Chris@0: * @param array $mapping Chris@0: * An array containing a mandatory 'properties' key and optional 'datatype', Chris@0: * 'datatype_callback' and 'type' keys. For example: Chris@0: * @code Chris@0: * array( Chris@0: * 'properties' => array('schema:interactionCount'), Chris@0: * 'datatype' => 'xsd:integer', Chris@0: * 'datatype_callback' => array( Chris@0: * 'callable' => 'Drupal\rdf\SchemaOrgDataConverter::interactionCount', Chris@0: * 'arguments' => array( Chris@0: * 'interaction_type' => 'UserComments' Chris@0: * ), Chris@0: * ), Chris@0: * ); Chris@0: * @endcode Chris@0: * @param mixed $data Chris@0: * (optional) A value that needs to be converted by the provided callback Chris@0: * function. Chris@0: * Chris@0: * @return array Chris@0: * RDFa attributes suitable for Drupal\Core\Template\Attribute. Chris@0: */ Chris@0: function rdf_rdfa_attributes($mapping, $data = NULL) { Chris@0: $attributes = []; Chris@0: Chris@0: // The type of mapping defaults to 'property'. Chris@0: $type = isset($mapping['mapping_type']) ? $mapping['mapping_type'] : 'property'; Chris@0: Chris@0: switch ($type) { Chris@0: // The mapping expresses the relationship between two resources. Chris@0: case 'rel': Chris@0: case 'rev': Chris@0: $attributes[$type] = $mapping['properties']; Chris@0: break; Chris@0: Chris@0: // The mapping expresses the relationship between a resource and some Chris@0: // literal text. Chris@0: case 'property': Chris@0: if (!empty($mapping['properties'])) { Chris@0: $attributes['property'] = $mapping['properties']; Chris@0: // Convert $data to a specific format as per the callback function. Chris@0: if (isset($data) && !empty($mapping['datatype_callback'])) { Chris@0: $callback = $mapping['datatype_callback']['callable']; Chris@0: $arguments = isset($mapping['datatype_callback']['arguments']) ? $mapping['datatype_callback']['arguments'] : NULL; Chris@0: $attributes['content'] = call_user_func($callback, $data, $arguments); Chris@0: } Chris@0: if (isset($mapping['datatype'])) { Chris@0: $attributes['datatype'] = $mapping['datatype']; Chris@0: } Chris@0: break; Chris@0: } Chris@0: } Chris@0: Chris@0: return $attributes; Chris@0: } Chris@0: Chris@0: /** Chris@0: * @} End of "addtogroup rdf". Chris@0: */ Chris@0: Chris@0: /** Chris@0: * Implements hook_entity_prepare_view(). Chris@0: */ Chris@0: function rdf_entity_prepare_view($entity_type, array $entities, array $displays) { Chris@0: // Iterate over the RDF mappings for each entity and prepare the RDFa Chris@0: // attributes to be added inside field formatters. Chris@0: foreach ($entities as $entity) { Chris@0: $mapping = rdf_get_mapping($entity_type, $entity->bundle()); Chris@0: // Only prepare the RDFa attributes for the fields which are configured to Chris@0: // be displayed. Chris@0: foreach ($displays[$entity->bundle()]->getComponents() as $name => $options) { Chris@0: $field_mapping = $mapping->getPreparedFieldMapping($name); Chris@0: if ($field_mapping) { Chris@0: foreach ($entity->get($name) as $item) { Chris@0: $item->_attributes += rdf_rdfa_attributes($field_mapping, $item->toArray()); Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Implements hook_ENTITY_TYPE_storage_load() for comment entities. Chris@0: */ Chris@0: function rdf_comment_storage_load($comments) { Chris@0: foreach ($comments as $comment) { Chris@0: // Pages with many comments can show poor performance. This information Chris@0: // isn't needed until rdf_preprocess_comment() is called, but set it here Chris@0: // to optimize performance for websites that implement an entity cache. Chris@0: $created_mapping = rdf_get_mapping('comment', $comment->bundle()) Chris@0: ->getPreparedFieldMapping('created'); Chris@0: /** @var \Drupal\comment\CommentInterface $comment*/ Chris@0: $comment->rdf_data['date'] = rdf_rdfa_attributes($created_mapping, $comment->get('created')->first()->toArray()); Chris@0: $entity = $comment->getCommentedEntity(); Chris@0: // The current function is a storage level hook, so avoid to bubble Chris@0: // bubbleable metadata, because it can be outside of a render context. Chris@0: $comment->rdf_data['entity_uri'] = $entity->toUrl()->toString(TRUE)->getGeneratedUrl(); Chris@0: if ($comment->hasParentComment()) { Chris@18: $comment->rdf_data['pid_uri'] = $comment->getParentComment()->toUrl()->toString(); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Implements hook_theme(). Chris@0: */ Chris@0: function rdf_theme() { Chris@0: return [ Chris@0: 'rdf_wrapper' => [ Chris@0: 'variables' => ['attributes' => [], 'content' => NULL], Chris@0: ], Chris@0: 'rdf_metadata' => [ Chris@0: 'variables' => ['metadata' => []], Chris@0: ], Chris@0: ]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Implements hook_preprocess_HOOK() for HTML document templates. Chris@0: */ Chris@0: function rdf_preprocess_html(&$variables) { Chris@0: // Adds RDF namespace prefix bindings in the form of an RDFa 1.1 prefix Chris@0: // attribute inside the html element. Chris@0: if (!isset($variables['html_attributes']['prefix'])) { Chris@0: $variables['html_attributes']['prefix'] = []; Chris@0: } Chris@0: foreach (rdf_get_namespaces() as $prefix => $uri) { Chris@0: $variables['html_attributes']['prefix'][] = $prefix . ': ' . $uri . " "; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Implements hook_preprocess_HOOK() for UID field templates. Chris@0: */ Chris@0: function rdf_preprocess_field__node__uid(&$variables) { Chris@0: _rdf_set_field_rel_attribute($variables); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Transforms the field property attribute into a rel attribute. Chris@0: */ Chris@0: function _rdf_set_field_rel_attribute(&$variables) { Chris@0: // Swap the regular field property attribute and use the rel attribute Chris@0: // instead so that it plays well with the RDFa markup when only a link is Chris@0: // present in the field output, for example in the case of the uid field. Chris@0: if (!empty($variables['attributes']['property'])) { Chris@0: $variables['attributes']['rel'] = $variables['attributes']['property']; Chris@0: unset($variables['attributes']['property']); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Implements hook_preprocess_HOOK() for node templates. Chris@0: */ Chris@0: function rdf_preprocess_node(&$variables) { Chris@0: // Adds RDFa markup to the node container. The about attribute specifies the Chris@0: // URI of the resource described within the HTML element, while the @typeof Chris@0: // attribute indicates its RDF type (e.g., foaf:Document, sioc:Person, and so Chris@0: // on.) Chris@0: $bundle = $variables['node']->bundle(); Chris@0: $mapping = rdf_get_mapping('node', $bundle); Chris@0: $bundle_mapping = $mapping->getPreparedBundleMapping('node', $bundle); Chris@0: $variables['attributes']['about'] = empty($variables['url']) ? NULL : $variables['url']; Chris@0: $variables['attributes']['typeof'] = empty($bundle_mapping['types']) ? NULL : $bundle_mapping['types']; Chris@0: Chris@0: // Adds RDFa markup for the node title as metadata because wrapping the title Chris@0: // with markup is not reliable and the title output is different depending on Chris@0: // the view mode (e.g. full vs. teaser). Chris@0: $title_mapping = $mapping->getPreparedFieldMapping('title'); Chris@0: if ($title_mapping) { Chris@0: $title_attributes['property'] = empty($title_mapping['properties']) ? NULL : $title_mapping['properties']; Chris@0: $title_attributes['content'] = $variables['node']->label(); Chris@0: $variables['title_suffix']['rdf_meta_title'] = [ Chris@0: '#theme' => 'rdf_metadata', Chris@0: '#metadata' => [$title_attributes], Chris@0: ]; Chris@0: } Chris@0: Chris@0: // Adds RDFa markup for the date. Chris@0: $created_mapping = $mapping->getPreparedFieldMapping('created'); Chris@18: if (!empty($created_mapping)) { Chris@0: $date_attributes = rdf_rdfa_attributes($created_mapping, $variables['node']->get('created')->first()->toArray()); Chris@0: $rdf_metadata = [ Chris@0: '#theme' => 'rdf_metadata', Chris@0: '#metadata' => [$date_attributes], Chris@0: ]; Chris@18: Chris@18: // Depending on whether custom preprocessing is enabled, the 'created' Chris@18: // field may appear in either of two different places, so check both of Chris@18: // those places here. Chris@18: // @see template_preprocess_node. Chris@18: if (!empty($variables['display_submitted'])) { Chris@18: // If custom preprocessing is enabled, then detect if the 'created' Chris@18: // field is displayed by checking the 'display_submitted' variable. In Chris@18: // this case, for back-compatibility, put the metadata into a special Chris@18: // variable. Chris@18: $variables['metadata'] = \Drupal::service('renderer')->render($rdf_metadata); Chris@18: } Chris@18: elseif (isset($variables['elements']['created'])) { Chris@18: // Otherwise, detect if the 'created' field is displayed by checking if Chris@18: // it is present in the 'elements variable. Put the metadata into Chris@18: // title_suffix, along with other metadata added by this module. Chris@18: $variables['title_suffix']['rdf_meta_created'] = $rdf_metadata; Chris@18: } Chris@0: } Chris@0: Chris@0: // Adds RDFa markup annotating the number of comments a node has. Chris@0: if (\Drupal::moduleHandler()->moduleExists('comment') && \Drupal::currentUser()->hasPermission('access comments')) { Chris@0: $comment_count_mapping = $mapping->getPreparedFieldMapping('comment_count'); Chris@0: if (!empty($comment_count_mapping['properties'])) { Chris@0: $fields = array_keys(\Drupal::service('comment.manager')->getFields('node')); Chris@0: $definitions = array_keys($variables['node']->getFieldDefinitions()); Chris@0: $valid_fields = array_intersect($fields, $definitions); Chris@0: $count = 0; Chris@0: foreach ($valid_fields as $field_name) { Chris@0: $count += $variables['node']->get($field_name)->comment_count; Chris@0: // Adds RDFa markup for the comment count near the node title as Chris@0: // metadata. Chris@0: $comment_count_attributes = rdf_rdfa_attributes($comment_count_mapping, $variables['node']->get($field_name)->comment_count); Chris@0: $variables['title_suffix']['rdf_meta_comment_count'] = [ Chris@0: '#theme' => 'rdf_metadata', Chris@0: '#metadata' => [$comment_count_attributes], Chris@0: ]; Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Implements hook_preprocess_HOOK() for user templates. Chris@0: */ Chris@0: function rdf_preprocess_user(&$variables) { Chris@0: /** @var $account \Drupal\user\UserInterface */ Chris@0: $account = $variables['elements']['#user']; Chris@18: $uri = $account->toUrl(); Chris@0: $mapping = rdf_get_mapping('user', 'user'); Chris@0: $bundle_mapping = $mapping->getPreparedBundleMapping(); Chris@0: Chris@0: // Adds RDFa markup to the user profile page. Fields displayed in this page Chris@0: // will automatically describe the user. Chris@0: if (!empty($bundle_mapping['types'])) { Chris@0: $variables['attributes']['typeof'] = $bundle_mapping['types']; Chris@18: $variables['attributes']['about'] = $account->toUrl()->toString(); Chris@0: } Chris@0: // If we are on the user account page, add the relationship between the Chris@0: // sioc:UserAccount and the foaf:Person who holds the account. Chris@0: if (\Drupal::routeMatch()->getRouteName() == $uri->getRouteName()) { Chris@0: // Adds the markup for username as language neutral literal, see Chris@0: // rdf_preprocess_username(). Chris@0: $name_mapping = $mapping->getPreparedFieldMapping('name'); Chris@0: if (!empty($name_mapping['properties'])) { Chris@0: $username_meta = [ Chris@0: '#tag' => 'meta', Chris@0: '#attributes' => [ Chris@18: 'about' => $account->toUrl()->toString(), Chris@0: 'property' => $name_mapping['properties'], Chris@0: 'content' => $account->getDisplayName(), Chris@0: 'lang' => '', Chris@0: ], Chris@0: ]; Chris@0: $variables['#attached']['html_head'][] = [$username_meta, 'rdf_user_username']; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Implements hook_preprocess_HOOK() for username.html.twig. Chris@0: */ Chris@0: function rdf_preprocess_username(&$variables) { Chris@0: // Because lang is set on the HTML element that wraps the page, the Chris@0: // username inherits this language attribute. However, since the username Chris@0: // might not be transliterated to the same language that the content is in, Chris@0: // we do not want it to inherit the language attribute, so we set the Chris@0: // attribute to an empty string. Chris@0: if (empty($variables['attributes']['lang'])) { Chris@0: $variables['attributes']['lang'] = ''; Chris@0: } Chris@0: Chris@0: // The profile URI is used to identify the user account. The about attribute Chris@0: // is used to set the URI as the default subject of the properties embedded Chris@0: // as RDFa in the child elements. Even if the user profile is not accessible Chris@0: // to the current user, we use its URI in order to identify the user in RDF. Chris@0: // We do not use this attribute for the anonymous user because we do not have Chris@0: // a user profile URI for it (only a homepage which cannot be used as user Chris@0: // profile in RDF.) Chris@0: if ($variables['uid'] > 0) { Chris@18: $variables['attributes']['about'] = Url::fromRoute('entity.user.canonical', ['user' => $variables['uid']])->toString(); Chris@0: } Chris@0: Chris@0: // Add RDF type of user. Chris@0: $mapping = rdf_get_mapping('user', 'user'); Chris@0: $bundle_mapping = $mapping->getPreparedBundleMapping(); Chris@0: if (!empty($bundle_mapping['types'])) { Chris@0: $variables['attributes']['typeof'] = $bundle_mapping['types']; Chris@0: } Chris@0: // Annotate the username in RDFa. A property attribute is used with an empty Chris@0: // datatype attribute to ensure the username is parsed as a plain literal Chris@0: // in RDFa 1.0 and 1.1. Chris@0: $name_mapping = $mapping->getPreparedFieldMapping('name'); Chris@0: if (!empty($name_mapping)) { Chris@0: $variables['attributes']['property'] = $name_mapping['properties']; Chris@0: $variables['attributes']['datatype'] = ''; Chris@0: } Chris@0: // Add the homepage RDFa markup if present. Chris@0: $homepage_mapping = $mapping->getPreparedFieldMapping('homepage'); Chris@0: if (!empty($variables['homepage']) && !empty($homepage_mapping)) { Chris@0: $variables['attributes']['rel'] = $homepage_mapping['properties']; Chris@0: } Chris@0: // Long usernames are truncated by template_preprocess_username(). Store the Chris@0: // full name in the content attribute so it can be extracted in RDFa. Chris@0: if ($variables['truncated']) { Chris@0: $variables['attributes']['content'] = $variables['name_raw']; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Implements hook_preprocess_HOOK() for comment templates. Chris@0: */ Chris@0: function rdf_preprocess_comment(&$variables) { Chris@0: $comment = $variables['comment']; Chris@0: $mapping = rdf_get_mapping('comment', $comment->bundle()); Chris@0: $bundle_mapping = $mapping->getPreparedBundleMapping(); Chris@0: Chris@0: if (!empty($bundle_mapping['types']) && !isset($comment->in_preview)) { Chris@0: // Adds RDFa markup to the comment container. The about attribute specifies Chris@0: // the URI of the resource described within the HTML element, while the Chris@0: // typeof attribute indicates its RDF type (e.g., sioc:Post, foaf:Document, Chris@0: // and so on.) Chris@18: $variables['attributes']['about'] = $comment->toUrl()->toString(); Chris@0: $variables['attributes']['typeof'] = $bundle_mapping['types']; Chris@0: } Chris@0: Chris@0: // Adds RDFa markup for the relation between the comment and its author. Chris@0: $author_mapping = $mapping->getPreparedFieldMapping('uid'); Chris@0: if (!empty($author_mapping)) { Chris@0: $author_attributes = ['rel' => $author_mapping['properties']]; Chris@0: // Wraps the 'author' and 'submitted' variables which are both available in Chris@0: // comment.html.twig. Chris@0: $variables['author'] = [ Chris@0: '#theme' => 'rdf_wrapper', Chris@0: '#content' => $variables['author'], Chris@0: '#attributes' => $author_attributes, Chris@0: ]; Chris@0: $variables['submitted'] = [ Chris@0: '#theme' => 'rdf_wrapper', Chris@0: '#content' => $variables['submitted'], Chris@0: '#attributes' => $author_attributes, Chris@0: ]; Chris@0: } Chris@0: // Adds RDFa markup for the date of the comment. Chris@0: $created_mapping = $mapping->getPreparedFieldMapping('created'); Chris@0: if (!empty($created_mapping)) { Chris@0: // The comment date is precomputed as part of the rdf_data so that it can be Chris@0: // cached as part of the entity. Chris@0: $date_attributes = $comment->rdf_data['date']; Chris@0: Chris@0: $rdf_metadata = [ Chris@0: '#theme' => 'rdf_metadata', Chris@0: '#metadata' => [$date_attributes], Chris@0: ]; Chris@0: // Ensure the original variable is represented as a render array. Chris@0: $created = !is_array($variables['created']) ? ['#markup' => $variables['created']] : $variables['created']; Chris@0: $submitted = !is_array($variables['submitted']) ? ['#markup' => $variables['submitted']] : $variables['submitted']; Chris@0: // Make render array and RDF metadata available in comment.html.twig. Chris@0: $variables['created'] = [$created, $rdf_metadata]; Chris@0: $variables['submitted'] = [$submitted, $rdf_metadata]; Chris@0: } Chris@0: $title_mapping = $mapping->getPreparedFieldMapping('subject'); Chris@0: if (!empty($title_mapping)) { Chris@0: // Adds RDFa markup to the subject of the comment. Because the RDFa markup Chris@0: // is added to an

tag which might contain HTML code, we specify an Chris@0: // empty datatype to ensure the value of the title read by the RDFa parsers Chris@0: // is a literal. Chris@0: $variables['title_attributes']['property'] = $title_mapping['properties']; Chris@0: $variables['title_attributes']['datatype'] = ''; Chris@0: } Chris@0: Chris@0: // Annotates the parent relationship between the current comment and the node Chris@0: // it belongs to. If available, the parent comment is also annotated. Chris@0: // @todo When comments are turned into fields, this should be changed. Chris@0: // Currently there is no mapping relating a comment to its node. Chris@0: $pid_mapping = $mapping->getPreparedFieldMapping('pid'); Chris@0: if (!empty($pid_mapping)) { Chris@0: // Adds the relation to the parent entity. Chris@0: $parent_entity_attributes['rel'] = $pid_mapping['properties']; Chris@0: // The parent entity URI is precomputed as part of the rdf_data so that it Chris@0: // can be cached as part of the entity. Chris@0: $parent_entity_attributes['resource'] = $comment->rdf_data['entity_uri']; Chris@0: $variables['rdf_metadata_attributes'][] = $parent_entity_attributes; Chris@0: Chris@0: // Adds the relation to parent comment, if it exists. Chris@0: if ($comment->hasParentComment()) { Chris@0: $parent_comment_attributes['rel'] = $pid_mapping['properties']; Chris@0: // The parent comment URI is precomputed as part of the rdf_data so that Chris@0: // it can be cached as part of the entity. Chris@0: $parent_comment_attributes['resource'] = $comment->rdf_data['pid_uri']; Chris@0: $variables['rdf_metadata_attributes'][] = $parent_comment_attributes; Chris@0: } Chris@0: } Chris@0: // Adds RDF metadata markup above comment body if any. Chris@0: if (!empty($variables['rdf_metadata_attributes']) && isset($variables['content']['comment_body'])) { Chris@0: $rdf_metadata = [ Chris@0: '#theme' => 'rdf_metadata', Chris@0: '#metadata' => $variables['rdf_metadata_attributes'], Chris@0: ]; Chris@0: if (!empty($variables['content']['comment_body']['#prefix'])) { Chris@0: $rdf_metadata['#suffix'] = $variables['content']['comment_body']['#prefix']; Chris@0: } Chris@0: $variables['content']['comment_body']['#prefix'] = \Drupal::service('renderer')->render($rdf_metadata); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Implements hook_preprocess_HOOK() for taxonomy term templates. Chris@0: */ Chris@0: function rdf_preprocess_taxonomy_term(&$variables) { Chris@0: // Adds RDFa markup to the taxonomy term container. Chris@0: // The @about attribute specifies the URI of the resource described within Chris@0: // the HTML element, while the @typeof attribute indicates its RDF type Chris@0: // (e.g., schema:Thing, skos:Concept, and so on). Chris@0: $term = $variables['term']; Chris@0: $mapping = rdf_get_mapping('taxonomy_term', $term->bundle()); Chris@0: $bundle_mapping = $mapping->getPreparedBundleMapping(); Chris@18: $variables['attributes']['about'] = $term->toUrl()->toString(); Chris@0: $variables['attributes']['typeof'] = empty($bundle_mapping['types']) ? NULL : $bundle_mapping['types']; Chris@0: Chris@0: // Add RDFa markup for the taxonomy term name as metadata, if present. Chris@0: $name_field_mapping = $mapping->getPreparedFieldMapping('name'); Chris@0: if (!empty($name_field_mapping) && !empty($name_field_mapping['properties'])) { Chris@0: $name_attributes = [ Chris@0: 'property' => $name_field_mapping['properties'], Chris@0: 'content' => $term->getName(), Chris@0: ]; Chris@0: $variables['title_suffix']['taxonomy_term_rdfa'] = [ Chris@0: '#theme' => 'rdf_metadata', Chris@0: '#metadata' => [$name_attributes], Chris@0: ]; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Implements hook_preprocess_HOOK() for image.html.twig. Chris@0: */ Chris@0: function rdf_preprocess_image(&$variables) { Chris@0: // Adds the RDF type for image. We cannot use the usual entity-based mapping Chris@0: // to get 'foaf:Image' because image does not have its own entity type or Chris@0: // bundle. Chris@0: $variables['attributes']['typeof'] = ['foaf:Image']; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Prepares variables for RDF metadata templates. Chris@0: * Chris@0: * Default template: rdf-metadata.html.twig. Chris@0: * Chris@0: * Sometimes it is useful to export data which is not semantically present in Chris@0: * the HTML output. For example, a hierarchy of comments is visible for a human Chris@0: * but not for machines because this hierarchy is not present in the DOM tree. Chris@0: * We can express it in RDFa via empty tags. These aren't visible and Chris@0: * give machines extra information about the content and its structure. Chris@0: * Chris@0: * @param array $variables Chris@0: * An associative array containing: Chris@0: * - metadata: An array of attribute arrays. Each item in the array Chris@0: * corresponds to its own set of attributes, and therefore, needs its own Chris@0: * element. Chris@0: */ Chris@0: function template_preprocess_rdf_metadata(&$variables) { Chris@0: foreach ($variables['metadata'] as $key => $attributes) { Chris@0: if (!is_null($attributes)) { Chris@0: $variables['metadata'][$key] = new Attribute($attributes); Chris@0: } Chris@0: else { Chris@0: $variables['metadata'][$key] = new Attribute(); Chris@0: } Chris@0: } Chris@0: }