core/includes/common.inc | 28 ++++++++++++++++- core/modules/comment/comment.module | 15 ++++++++- core/modules/comment/js/comment-link-edit-own.js | 33 ++++++++++++++++++++ .../lib/Drupal/comment/CommentRenderController.php | 30 +++++++++++++++++- 4 files changed, 103 insertions(+), 3 deletions(-) diff --git a/core/includes/common.inc b/core/includes/common.inc index b339eb6..ac497e4 100644 --- a/core/includes/common.inc +++ b/core/includes/common.inc @@ -2462,6 +2462,28 @@ function drupal_merge_js_settings($settings_items) { } /** + * Merges two #attached arrays. + * + * @param array $a + * An #attached array. + * @param array $b + * Another #attached array. + * + * @return array + * The merged #attached array. + */ +function drupal_merge_attachments($a, $b) { + $a += array('library' => array(), 'css' => array(), 'js' => array()); + $b += array('library' => array(), 'css' => array(), 'js' => array()); + + $a['library'] = array_merge($a['library'], $b['library']); + $a['css'] = array_merge($a['css'], $b['css']); + $a['js'] = array_merge($a['js'], $b['js']); + + return $a; +} + +/** * #pre_render callback to add the elements needed for JavaScript tags to be rendered. * * This function evaluates the aggregation enabled/disabled condition on a group @@ -3603,7 +3625,7 @@ function drupal_pre_render_link($element) { * properties of the parent are used. */ function drupal_pre_render_links($element) { - $element += array('#links' => array()); + $element += array('#links' => array(), '#attached' => array()); foreach (element_children($element) as $key) { $child = &$element[$key]; // If the child has links which have not been printed yet and the user has @@ -3614,6 +3636,10 @@ function drupal_pre_render_links($element) { // cannot be mistakenly rendered twice). $child['#printed'] = TRUE; } + // Merge attachments. + if (isset($child['#attached'])) { + $element['#attached'] = drupal_merge_attachments($element['#attached'], $child['#attached']); + } } return $element; } diff --git a/core/modules/comment/comment.module b/core/modules/comment/comment.module index 14eb271..f9b63d8 100644 --- a/core/modules/comment/comment.module +++ b/core/modules/comment/comment.module @@ -1504,7 +1504,8 @@ function template_preprocess_comment(&$variables) { // Add clearfix class. $variables['attributes']['class'][] = 'clearfix'; - // Add comment author user ID. Necessary for the comment-by-viewer library. + // Add comment author user ID. Necessary for the comment-by-viewer and + // comment-link-edit-own libraries. $variables['attributes']['data-comment-user-id'] = $comment->uid->value; $variables['content_attributes']['class'][] = 'content'; @@ -1728,5 +1729,17 @@ function comment_library_info() { array('history', 'drupal.history'), ), ); + $libraries['drupal.comment-link-edit-own'] = array( + 'title' => 'Comment link "edit" own', + 'version' => \Drupal::VERSION, + 'js' => array( + $path . '/js/comment-link-edit-own.js' => array(), + ), + 'dependencies' => array( + array('system', 'jquery'), + array('system', 'drupal'), + array('system', 'drupalSettings'), + ), + ); return $libraries; } diff --git a/core/modules/comment/js/comment-link-edit-own.js b/core/modules/comment/js/comment-link-edit-own.js new file mode 100644 index 0000000..26d7b7a --- /dev/null +++ b/core/modules/comment/js/comment-link-edit-own.js @@ -0,0 +1,33 @@ +/** + * @file + * Attaches behaviors for the Comment module's "edit" comment links. + * + * May only be loaded for users that have the "edit own comments" permission. + */ + +(function ($, Drupal, drupalSettings) { + +"use strict"; + +Drupal.behaviors.commentLinkEditOwn = { + attach: function (context) { + var userID = parseInt(drupalSettings.user.uid, 10); + $(context) + // Find the "edit" comment links. + .find('a[data-comment-link-edit-own]') + .once('comment-link-edit-own', function () { + var $editCommentLink = $(this); + var commentUserID = $editCommentLink + .closest('[data-comment-user-id') + .attr('data-comment-user-id'); + // If the comment author's user ID equals the current user ID, then we + // must show the "edit" link, because the user has the + // "edit own comments" permission. + if (parseInt(commentUserID, 10) === userID) { + $editCommentLink.removeClass('hidden'); + } + }); + } +}; + +})(jQuery, Drupal, drupalSettings); diff --git a/core/modules/comment/lib/Drupal/comment/CommentRenderController.php b/core/modules/comment/lib/Drupal/comment/CommentRenderController.php index e33a825..93fd358 100644 --- a/core/modules/comment/lib/Drupal/comment/CommentRenderController.php +++ b/core/modules/comment/lib/Drupal/comment/CommentRenderController.php @@ -150,6 +150,7 @@ public function buildContent(array $entities, array $displays, $view_mode, $lang * An array that can be processed by drupal_pre_render_links(). */ protected function buildLinks(CommentInterface $entity, EntityInterface $commented_entity) { + $attached = array(); $links = array(); $status = $commented_entity->get($entity->field_name->value)->status; @@ -162,12 +163,38 @@ protected function buildLinks(CommentInterface $entity, EntityInterface $comment ); } - if ($entity->access('update')) { + if ($entity->status->value == COMMENT_PUBLISHED && \Drupal::currentUser()->hasPermission('edit own comments') || \Drupal::currentUser()->hasPermission('administer comments')) { $links['comment-edit'] = array( 'title' => t('edit'), 'href' => "comment/{$entity->id()}/edit", 'html' => TRUE, ); + // When the user does not have the "administer comments" permission, + // and only has the "edit own comments" permission, we must hide the + // "edit" link by default to not break the render cache. We then use + // JavaScript to only show the "edit" link on comments where the + // comment author is the same user as the currently active user. + if (!\Drupal::currentUser()->hasPermission('administer comments')) { + $links['comment-edit']['attributes'] = array( + 'class' => 'hidden', + 'data-comment-link-edit-own' => TRUE, + ); + $attached = array( + 'library' => array( + array('comment', 'drupal.comment-link-edit-own'), + ), + 'js' => array( + 0 => array( + 'type' => 'setting', + 'data' => array( + 'comment' => array( + 'canEditOwnComments' => \Drupal::currentUser()->hasPermission('edit own comments'), + ), + ), + ), + ), + ); + } } if ($entity->access('create')) { $links['comment-reply'] = array( @@ -210,6 +237,7 @@ protected function buildLinks(CommentInterface $entity, EntityInterface $comment // check. '#links' => $links, '#attributes' => array('class' => array('links', 'inline')), + '#attached' => $attached, ); }