You are here

function rdf_preprocess_node in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/rdf/rdf.module \rdf_preprocess_node()

Implements hook_preprocess_HOOK() for node templates.

File

core/modules/rdf/rdf.module, line 296
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.)
  $bundle = $variables['node']
    ->bundle();
  $mapping = rdf_get_mapping('node', $bundle);
  $bundle_mapping = $mapping
    ->getPreparedBundleMapping('node', $bundle);
  $variables['attributes']['about'] = empty($variables['url']) ? NULL : $variables['url'];
  $variables['attributes']['typeof'] = empty($bundle_mapping['types']) ? NULL : $bundle_mapping['types'];

  // Adds RDFa markup for the node title as metadata because wrapping the title
  // with markup is not reliable and the title output is different depending on
  // the view mode (e.g. full vs. teaser).
  $title_mapping = $mapping
    ->getPreparedFieldMapping('title');
  if ($title_mapping) {
    $title_attributes['property'] = empty($title_mapping['properties']) ? NULL : $title_mapping['properties'];
    $title_attributes['content'] = $variables['node']
      ->label();
    $variables['title_suffix']['rdf_meta_title'] = array(
      '#theme' => 'rdf_metadata',
      '#metadata' => array(
        $title_attributes,
      ),
    );
  }

  // Adds RDFa markup for the date.
  $created_mapping = $mapping
    ->getPreparedFieldMapping('created');
  if (!empty($created_mapping) && $variables['display_submitted']) {
    $date_attributes = rdf_rdfa_attributes($created_mapping, $variables['node']
      ->get('created')
      ->first()
      ->toArray());
    $rdf_metadata = array(
      '#theme' => 'rdf_metadata',
      '#metadata' => array(
        $date_attributes,
      ),
    );
    $variables['metadata'] = drupal_render($rdf_metadata);
  }

  // Adds RDFa markup annotating the number of comments a node has.
  if (\Drupal::moduleHandler()
    ->moduleExists('comment') && \Drupal::currentUser()
    ->hasPermission('access comments')) {
    $comment_count_mapping = $mapping
      ->getPreparedFieldMapping('comment_count');
    if (!empty($comment_count_mapping['properties'])) {
      $fields = array_keys(\Drupal::service('comment.manager')
        ->getFields('node'));
      $definitions = array_keys($variables['node']
        ->getFieldDefinitions());
      $valid_fields = array_intersect($fields, $definitions);
      $count = 0;
      foreach ($valid_fields as $field_name) {
        $count += $variables['node']
          ->get($field_name)->comment_count;

        // Adds RDFa markup for the comment count near the node title as
        // metadata.
        $comment_count_attributes = rdf_rdfa_attributes($comment_count_mapping, $variables['node']
          ->get($field_name)->comment_count);
        $variables['title_suffix']['rdf_meta_comment_count'] = array(
          '#theme' => 'rdf_metadata',
          '#metadata' => array(
            $comment_count_attributes,
          ),
        );
      }
    }
  }
}