function rdf_preprocess_comment

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

Implements hook_preprocess_HOOK() for comment templates.

File

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

Code

function rdf_preprocess_comment(&$variables) {
    $comment = $variables['comment'];
    $mapping = rdf_get_mapping('comment', $comment->bundle());
    $bundle_mapping = $mapping->getPreparedBundleMapping();
    if (!empty($bundle_mapping['types']) && !isset($comment->in_preview)) {
        // Adds RDFa markup to the comment 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., sioc:Post, foaf:Document,
        // and so on.)
        $variables['attributes']['about'] = $comment->toUrl()
            ->toString();
        $variables['attributes']['typeof'] = $bundle_mapping['types'];
    }
    // Adds RDFa markup for the relation between the comment and its author.
    $author_mapping = $mapping->getPreparedFieldMapping('uid');
    if (!empty($author_mapping)) {
        $author_attributes = [
            'rel' => $author_mapping['properties'],
        ];
        // Wraps the 'author' and 'submitted' variables which are both available in
        // comment.html.twig.
        $variables['author'] = [
            '#theme' => 'rdf_wrapper',
            '#content' => $variables['author'],
            '#attributes' => $author_attributes,
        ];
        $variables['submitted'] = [
            '#theme' => 'rdf_wrapper',
            '#content' => $variables['submitted'],
            '#attributes' => $author_attributes,
        ];
    }
    // Adds RDFa markup for the date of the comment.
    $created_mapping = $mapping->getPreparedFieldMapping('created');
    if (!empty($created_mapping) && isset($comment->rdf_data)) {
        // The comment date is precomputed as part of the rdf_data so that it can be
        // cached as part of the entity.
        $date_attributes = $comment->rdf_data['date'];
        $rdf_metadata = [
            '#theme' => 'rdf_metadata',
            '#metadata' => [
                $date_attributes,
            ],
        ];
        // Ensure the original variable is represented as a render array.
        $created = !is_array($variables['created']) ? [
            '#markup' => $variables['created'],
        ] : $variables['created'];
        $submitted = !is_array($variables['submitted']) ? [
            '#markup' => $variables['submitted'],
        ] : $variables['submitted'];
        // Make render array and RDF metadata available in comment.html.twig.
        $variables['created'] = [
            $created,
            $rdf_metadata,
        ];
        $variables['submitted'] = [
            $submitted,
            $rdf_metadata,
        ];
    }
    $title_mapping = $mapping->getPreparedFieldMapping('subject');
    if (!empty($title_mapping)) {
        // Adds RDFa markup to the subject of the comment. Because the RDFa markup
        // is added to an <h3> 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'] = $title_mapping['properties'];
        $variables['title_attributes']['datatype'] = '';
    }
    // Annotates the parent relationship between the current comment and the node
    // it belongs to. If available, the parent comment is also annotated.
    // @todo When comments are turned into fields, this should be changed.
    // Currently there is no mapping relating a comment to its node.
    $pid_mapping = $mapping->getPreparedFieldMapping('pid');
    if (!empty($pid_mapping)) {
        // Adds the relation to the parent entity.
        $parent_entity_attributes['rel'] = $pid_mapping['properties'];
        // The parent entity URI is precomputed as part of the rdf_data so that it
        // can be cached as part of the entity.
        $parent_entity_attributes['resource'] = $comment->rdf_data['entity_uri'];
        $variables['rdf_metadata_attributes'][] = $parent_entity_attributes;
        // Adds the relation to parent comment, if it exists.
        if ($comment->hasParentComment()) {
            $parent_comment_attributes['rel'] = $pid_mapping['properties'];
            // The parent comment URI is precomputed as part of the rdf_data so that
            // it can be cached as part of the entity.
            $parent_comment_attributes['resource'] = $comment->rdf_data['pid_uri'];
            $variables['rdf_metadata_attributes'][] = $parent_comment_attributes;
        }
    }
    // Adds RDF metadata markup above comment body if any.
    if (!empty($variables['rdf_metadata_attributes']) && isset($variables['content']['comment_body'])) {
        $rdf_metadata = [
            '#theme' => 'rdf_metadata',
            '#metadata' => $variables['rdf_metadata_attributes'],
        ];
        if (!empty($variables['content']['comment_body']['#prefix'])) {
            $rdf_metadata['#suffix'] = $variables['content']['comment_body']['#prefix'];
        }
        $variables['content']['comment_body']['#prefix'] = \Drupal::service('renderer')->render($rdf_metadata);
    }
}

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