function rdf_preprocess_node

Implements hook_preprocess_HOOK() for node.html.twig.

File

drupal/core/modules/rdf/rdf.module, line 497
Enables semantically enriched output for Drupal sites in the form of RDFa.

Code

function rdf_preprocess_node(&$variables) {

  // Adds RDFa markup to the node container. The about attribute specifies the
  // URI of the resource described within the HTML element, while the @typeof
  // attribute indicates its RDF type (e.g., foaf:Document, sioc:Person, and so
  // on.)
  $variables['attributes']['about'] = empty($variables['node_url']) ? NULL : $variables['node_url'];
  $variables['attributes']['typeof'] = empty($variables['node']->rdf_mapping['rdftype']) ? NULL : $variables['node']->rdf_mapping['rdftype'];

  // Adds RDFa markup to the title of the node. Because the RDFa markup is
  // added to the <h2> tag which might contain HTML code, we specify an empty
  // datatype to ensure the value of the title read by the RDFa parsers is a
  // literal.
  $variables['title_attributes']['property'] = empty($variables['node']->rdf_mapping['title']['predicates']) ? NULL : $variables['node']->rdf_mapping['title']['predicates'];
  $variables['title_attributes']['datatype'] = '';

  // In full node mode, the title is not displayed by node.html.twig so it is
  // added in the <head> tag of the HTML page.
  if ($variables['page']) {
    $element = array(
      '#tag' => 'meta',
      '#attributes' => array(
        'content' => $variables['label'],
        'about' => $variables['node_url'],
      ),
    );
    if (!empty($variables['node']->rdf_mapping['title']['predicates'])) {
      $element['#attributes']['property'] = $variables['node']->rdf_mapping['title']['predicates'];
    }
    drupal_add_html_head($element, 'rdf_node_title');
  }

  // Adds RDFa markup for the date.
  if (!empty($variables['node']->rdf_mapping['created'])) {
    $date_attributes = rdf_rdfa_attributes($variables['node']->rdf_mapping['created'], $variables['node']->created);
    $variables['rdf_template_variable_attributes']['date'] = $date_attributes;
    if ($variables['submitted']) {
      $variables['rdf_template_variable_attributes']['submitted'] = $date_attributes;
    }
  }

  // Adds RDFa markup for the relation between the node and its author.
  if (!empty($variables['node']->rdf_mapping['uid'])) {
    $variables['rdf_template_variable_attributes']['name']['rel'] = $variables['node']->rdf_mapping['uid']['predicates'];
    if ($variables['submitted']) {
      $variables['rdf_template_variable_attributes']['submitted']['rel'] = $variables['node']->rdf_mapping['uid']['predicates'];
    }
  }

  // Adds RDFa markup annotating the number of comments a node has.
  if (isset($variables['node']->comment_count) && !empty($variables['node']->rdf_mapping['comment_count']['predicates'])) {

    // Annotates the 'x comments' link in teaser view.
    if (isset($variables['content']['links']['comment']['#links']['comment-comments'])) {
      $comment_count_attributes['property'] = $variables['node']->rdf_mapping['comment_count']['predicates'];
      $comment_count_attributes['content'] = $variables['node']->comment_count;
      $comment_count_attributes['datatype'] = $variables['node']->rdf_mapping['comment_count']['datatype'];

      // According to RDFa parsing rule number 4, a new subject URI is created
      // from the href attribute if no rel/rev attribute is present. To get the
      // original node URL from the about attribute of the parent container we
      // set an empty rel attribute which triggers rule number 5. See
      // http://www.w3.org/TR/rdfa-syntax/#sec_5.5.
      $comment_count_attributes['rel'] = '';
      $variables['content']['links']['comment']['#links']['comment-comments']['attributes'] += $comment_count_attributes;
    }

    // In full node view, the number of comments is not displayed by
    // node.html.twig so it is expressed in RDFa in the <head> tag of the HTML
    // page.
    if ($variables['page'] && user_access('access comments')) {
      $element = array(
        '#tag' => 'meta',
        '#attributes' => array(
          'about' => $variables['node_url'],
          'property' => $variables['node']->rdf_mapping['comment_count']['predicates'],
          'content' => $variables['node']->comment_count,
          'datatype' => $variables['node']->rdf_mapping['comment_count']['datatype'],
        ),
      );
      drupal_add_html_head($element, 'rdf_node_comment_count');
    }
  }
}