core/modules/comment/comment.module | 17 +++++++++++++++++ .../FieldFormatter/CommentDefaultFormatter.php | 19 ++++++++++++++++++- .../comment/Plugin/Field/FieldType/CommentItem.php | 1 + 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/core/modules/comment/comment.module b/core/modules/comment/comment.module index 8b28e76..097465d 100644 --- a/core/modules/comment/comment.module +++ b/core/modules/comment/comment.module @@ -1733,3 +1733,20 @@ function comment_library_info() { ); return $libraries; } + +/** + * #post_render_cache callback; inserts 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_insert_form(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..d9b4a6d 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 @@ public function viewElements(FieldItemListInterface $items) { 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_insert_form', + '#context' => array( + 'entity_type' => $entity->entityType(), + 'entity_id' => $entity->id(), + 'field_name' => $field_name + ), + ); + } } } diff --git a/core/modules/comment/lib/Drupal/comment/Plugin/Field/FieldType/CommentItem.php b/core/modules/comment/lib/Drupal/comment/Plugin/Field/FieldType/CommentItem.php index c91f1bb..0f83f91 100644 --- a/core/modules/comment/lib/Drupal/comment/Plugin/Field/FieldType/CommentItem.php +++ b/core/modules/comment/lib/Drupal/comment/Plugin/Field/FieldType/CommentItem.php @@ -150,6 +150,7 @@ public function instanceSettingsForm(array $form, array &$form_state) { $element['comment']['form_location'] = array( '#type' => 'checkbox', '#title' => t('Show reply form on the same page as comments'), + '#description' => t('Beware! This makes the page inherently slower for logged in users.'), '#default_value' => $settings['form_location'], ); $element['comment']['preview'] = array(