From 4ae91a1d344167eaabb4fa1961b9212a8353f73c Mon Sep 17 00:00:00 2001 From: Nathaniel Catchpole Date: Thu, 21 Nov 2013 10:03:38 +0000 Subject: [PATCH] Issue #2099133 by Wim Leers: Comment form on same page as comments forces node render caching to be per user. --- core/modules/comment/comment.module | 17 +++++++++++++++++ .../CommentDefaultFormatter.php | 19 ++++++++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/core/modules/comment/comment.module b/core/modules/comment/comment.module index 9f8b2827a2b6..fa38bfb9b140 100644 --- a/core/modules/comment/comment.module +++ b/core/modules/comment/comment.module @@ -1731,3 +1731,20 @@ function comment_library_info() { ); return $libraries; } + +/** + * #post_render_cache callback; replaces the placeholder with the comment form. + * + * @param array $context + * An array with the following keys: + * - entity_type: an entity type + * - entity_id: an entity ID + * - field_name: a comment field name + * + * @return array $element + * The updated $element. + */ +function comment_replace_form_placeholder(array $context) { + $entity = entity_load($context['entity_type'], $context['entity_id']); + return comment_add($entity, $context['field_name']); +} diff --git a/core/modules/comment/lib/Drupal/comment/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php b/core/modules/comment/lib/Drupal/comment/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php index ff0e346b7961..be1b6db200ba 100644 --- a/core/modules/comment/lib/Drupal/comment/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php +++ b/core/modules/comment/lib/Drupal/comment/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php @@ -138,7 +138,24 @@ class CommentDefaultFormatter extends FormatterBase implements ContainerFactoryP if ($status == COMMENT_OPEN && $comment_settings['form_location'] == COMMENT_FORM_BELOW) { // Only show the add comment form if the user has permission. if ($this->currentUser->hasPermission('post comments')) { - $output['comment_form'] = comment_add($entity, $field_name); + // All users in the "anonymous" role can use the same form: it is fine + // for this form to be stored in the render cache. + if ($this->currentUser->isAnonymous()) { + $output['comment_form'] = comment_add($entity, $field_name); + } + // All other users need a user-specific form, which would break the + // render cache: hence use a #post_render_cache callback. + else { + $output['comment_form'] = array( + '#type' => 'render_cache_placeholder', + '#callback' => 'comment_replace_form_placeholder', + '#context' => array( + 'entity_type' => $entity->entityType(), + 'entity_id' => $entity->id(), + 'field_name' => $field_name + ), + ); + } } }