summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNathaniel Catchpole2013-11-21 10:03:38 (GMT)
committerNathaniel Catchpole2013-11-21 10:03:38 (GMT)
commit4ae91a1d344167eaabb4fa1961b9212a8353f73c (patch)
treed974c1a7be612477f5e2e9cefc2aba629ad57098
parent67f30bc0a1f0007f4725a87eb1f0b9525f5a9a8f (diff)
Issue #2099133 by Wim Leers: Comment form on same page as comments forces node render caching to be per user.
-rw-r--r--core/modules/comment/comment.module17
-rw-r--r--core/modules/comment/lib/Drupal/comment/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php19
2 files changed, 35 insertions, 1 deletions
diff --git a/core/modules/comment/comment.module b/core/modules/comment/comment.module
index 9f8b282..fa38bfb 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 ff0e346..be1b6db 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
+ ),
+ );
+ }
}
}