function rdf_preprocess_node

Same name in other branches
  1. 7.x modules/rdf/rdf.module \rdf_preprocess_node()
  2. 8.9.x core/modules/rdf/rdf.module \rdf_preprocess_node()

Implements hook_preprocess_HOOK() for node templates.

File

core/modules/rdf/rdf.module, line 323

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);
            foreach ($valid_fields as $field_name) {
                // 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,
                    ],
                ];
            }
        }
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.