diff --git a/core/modules/rdf/rdf.module b/core/modules/rdf/rdf.module index 1beb1a1..35ac8b8 100644 --- a/core/modules/rdf/rdf.module +++ b/core/modules/rdf/rdf.module @@ -247,6 +247,9 @@ function rdf_comment_storage_load($comments) { */ function rdf_theme() { return array( + 'rdf_wrapper' => array( + 'variables' => array('attributes' => array(), 'content' => NULL), + ), 'rdf_metadata' => array( 'variables' => array('metadata' => array()), ), @@ -440,11 +443,19 @@ function rdf_preprocess_comment(&$variables) { // Adds RDFa markup for the relation between the comment and its author. $author_mapping = $mapping->getPreparedFieldMapping('uid'); if (!empty($author_mapping)) { - $author_attributes = array('rel' => $author_mapping['properties']); - // Wraps the author variable and the submitted variable which are both - // available in comment.html.twig. - $variables['author'] = SafeMarkup::set('' . $variables['author'] . ''); - $variables['submitted'] = SafeMarkup::set('' . $variables['submitted'] . ''); + $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'); @@ -457,11 +468,12 @@ function rdf_preprocess_comment(&$variables) { '#theme' => 'rdf_metadata', '#metadata' => array($date_attributes), ); - $created_metadata_markup = drupal_render($rdf_metadata); - // Appends the markup to the created variable and the submitted variable - // which are both available in comment.html.twig. - $variables['created'] = SafeMarkup::set(SafeMarkup::escape($variables['created']) . $created_metadata_markup); - $variables['submitted'] = SafeMarkup::set($variables['submitted'] . $created_metadata_markup); + // 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)) { diff --git a/core/modules/rdf/src/Tests/CommentAttributesTest.php b/core/modules/rdf/src/Tests/CommentAttributesTest.php index f7ad33a..3577ac7 100644 --- a/core/modules/rdf/src/Tests/CommentAttributesTest.php +++ b/core/modules/rdf/src/Tests/CommentAttributesTest.php @@ -145,6 +145,25 @@ public function testNumberOfCommentsRdfaMarkup() { } /** + * Tests comment author link markup has not been broken by RDF. + */ + public function testCommentRdfAuthorMarkup() { + // Post a comment as a registered user. + $this->saveComment($this->node->id(), $this->webUser->id()); + + // Give the user access to view user information so the link shows up. + user_role_grant_permissions( + RoleInterface::AUTHENTICATED_ID, + ['access user profiles'] + ); + $this->drupalLogin($this->webUser); + + $this->drupalGet('node/' . $this->node->id()); + $this->assertLink($this->webUser->getUsername()); + $this->assertLinkByHref('user/' . $this->webUser->id()); + } + + /** * Tests if RDFa markup for meta information is present in comments. * * Tests presence of RDFa markup for the title, date and author and homepage diff --git a/core/modules/rdf/templates/rdf-wrapper.html.twig b/core/modules/rdf/templates/rdf-wrapper.html.twig new file mode 100644 index 0000000..83ad5a2 --- /dev/null +++ b/core/modules/rdf/templates/rdf-wrapper.html.twig @@ -0,0 +1,13 @@ +{# +/** + * @file + * Default theme implementation for wrapping content with RDF attributes. + * + * Available variables: + * - content: The content being wrapped with RDF attributes. + * - attributes: HTML attributes, including RDF attributes for wrapper element. + * + * @ingroup themeable + */ +#} +{{ content }} \ No newline at end of file