You are here

function rdf_preprocess_node in Drupal 8

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

Implements hook_preprocess_HOOK() for node templates.

File

core/modules/rdf/rdf.module, line 302
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'] = [
      '#theme' => 'rdf_metadata',
      '#metadata' => [
        $title_attributes,
      ],
    ];
  }

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

    // Depending on whether custom preprocessing is enabled, the 'created'
    // field may appear in either of two different places, so check both of
    // those places here.
    // @see template_preprocess_node.
    if (!empty($variables['display_submitted'])) {

      // If custom preprocessing is enabled, then detect if the 'created'
      // field is displayed by checking the 'display_submitted' variable.  In
      // this case, for back-compatibility, put the metadata into a special
      // variable.
      $variables['metadata'] = \Drupal::service('renderer')
        ->render($rdf_metadata);
    }
    elseif (isset($variables['elements']['created'])) {

      // Otherwise, detect if the 'created' field is displayed by checking if
      // it is present in the 'elements variable.  Put the metadata into
      // title_suffix, along with other metadata added by this module.
      $variables['title_suffix']['rdf_meta_created'] = $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'] = [
          '#theme' => 'rdf_metadata',
          '#metadata' => [
            $comment_count_attributes,
          ],
        ];
      }
    }
  }
}