From d6002691b239842470690a318f6c7cacce9dd815 Mon Sep 17 00:00:00 2001 From: xjm Date: Sat, 15 Aug 2015 13:22:43 -0500 Subject: [PATCH] Issue #2501697 by ZenDoodles, leslieg, Les Lim, pfrenssen, joelpittet, hestenet, YesCT, scor, Cottser, xjm, lauriii, dsnopek, alexpott, akalata, Wim Leers, acouch, kgoel: Remove SafeMarkup::set in rdf_preprocess_comment() --- core/modules/rdf/rdf.module | 32 +++++++++++++------ .../rdf/src/Tests/CommentAttributesTest.php | 18 +++++++++++ .../rdf/templates/rdf-wrapper.html.twig | 13 ++++++++ 3 files changed, 53 insertions(+), 10 deletions(-) create mode 100644 core/modules/rdf/templates/rdf-wrapper.html.twig diff --git a/core/modules/rdf/rdf.module b/core/modules/rdf/rdf.module index 1beb1a1053f3..35ac8b8a8327 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 f7ad33a228b2..8d3beb756455 100644 --- a/core/modules/rdf/src/Tests/CommentAttributesTest.php +++ b/core/modules/rdf/src/Tests/CommentAttributesTest.php @@ -144,6 +144,24 @@ class CommentAttributesTest extends CommentTestBase { $this->assertTrue($graph->hasProperty($this->nodeUri, 'http://rdfs.org/sioc/ns#num_replies', $expected_value), 'Number of comments found in RDF output of full node view mode (sioc:num_replies).'); } + /** + * 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 profiles so the profile link shows up. + user_role_grant_permissions(RoleInterface::AUTHENTICATED_ID, ['access user profiles']); + $this->drupalLogin($this->webUser); + + // Ensure that the author link still works properly after the author output + // is modified by the RDF module. + $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. * 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 000000000000..cfdb31e21a24 --- /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 }}