Comments

temoor’s picture

Status: Active » Needs review
StatusFileSize
new5.99 KB
dawehner’s picture

Status: Needs review » Reviewed & tested by the community

Cool!

+++ b/core/modules/comment/src/CommentViewBuilder.php
@@ -17,6 +17,7 @@
@@ -189,8 +190,10 @@ public static function renderLinks(array $element, array $context) {

@@ -189,8 +190,10 @@ public static function renderLinks(array $element, array $context) {
+      $entity_manager = \Drupal::entityManager();

Meh, I don't really like static methods but this is life

m1r1k’s picture

Issue tags: +#ams2014contest
alexpott’s picture

Status: Reviewed & tested by the community » Needs work
  1. +++ b/core/modules/comment/src/CommentViewBuilder.php
    @@ -17,6 +17,7 @@
    +use Drupal\comment\Entity\Comment;
    
    @@ -189,8 +190,10 @@ public static function renderLinks(array $element, array $context) {
    -      $entity = entity_load('comment', $context['comment_entity_id']);
    -      $commented_entity = entity_load($context['commented_entity_type'], $context['commented_entity_id']);
    +      $entity = Comment::load($context['comment_entity_id']);
    +      $entity_manager = \Drupal::entityManager();
    +      $entity_storage = $entity_manager->getStorage($context['commented_entity_type']);
    +      $commented_entity = $entity_storage->load($context['commented_entity_id']);
    

    We already have the entity manager injected ($this->entityManager). All of these calls should be using it instead.

  2. +++ b/core/modules/comment/src/Plugin/views/row/Rss.php
    @@ -9,6 +9,7 @@
    +use Drupal\comment\Entity\Comment;
    
    @@ -60,7 +61,7 @@ public function preRender($result) {
    -    $this->comments = entity_load_multiple('comment', $cids);
    +    $this->comments = Comment::loadMultiple($cids);
    

    This should needs to override the create() and __construct() from Drupal\views\Plugin\views\PluginBase to inject the comment storage from the entity manager.

temoor’s picture

Status: Needs work » Needs review
StatusFileSize
new7.74 KB
new3.55 KB

1. But renderLinks is a static method

Status: Needs review » Needs work

The last submitted patch, 5: drupal8-entity_system-comment-load-2321599-5.patch, failed testing.

The last submitted patch, 5: drupal8-entity_system-comment-load-2321599-5.patch, failed testing.

temoor’s picture

Status: Needs work » Needs review
StatusFileSize
new8.14 KB
new1005 bytes

Fixed typo

m1r1k’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -

2 was fixed.
1 is static and it doesn't use instance.
RBTC

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 9: drupal8-entity_system-comment-load-2321599-9.patch, failed testing.

temoor’s picture

Status: Needs work » Needs review
StatusFileSize
new1.81 KB
new8.59 KB

Re-rolled patch, used already injected container instead of static method.

Status: Needs review » Needs work

The last submitted patch, 12: drupal8-entity_system-comment-load-2321599-12.patch, failed testing.

rpayanm’s picture

Status: Needs work » Needs review
StatusFileSize
new8.27 KB
mikemiles86’s picture

Assigned: Unassigned » mikemiles86
Issue tags: +SprintWeekend2015
mikemiles86’s picture

Assigned: mikemiles86 » Unassigned
Status: Needs review » Reviewed & tested by the community

Tested re-rolled patch #16

- Applies cleanly
- All instances of comment_load(), entity_load('comment') and entity_load_multiple('comment') removed & replaced.
- All comment tests pass.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 16: 2321599-16.patch, failed testing.

rpayanm’s picture

Status: Needs work » Needs review
StatusFileSize
new7.75 KB

rerolled

linl’s picture

Status: Needs review » Needs work
Issue tags: +@deprecated, +Needs reroll

Patch no longer applies, tagging for reroll.

piyuesh23’s picture

Issue tags: +#DCM2015
vineeth@nair’s picture

Assigned: Unassigned » vineeth@nair
vineeth@nair’s picture

Status: Needs work » Needs review
StatusFileSize
new8.9 KB

rerolled , Please review and let me know

pwieck’s picture

Issue tags: -Needs reroll
berdir’s picture

Component: entity system » comment.module

LinL queued 24: 2321599-24.patch for re-testing.

Status: Needs review » Needs work

The last submitted patch, 24: 2321599-24.patch, failed testing.

jeroent’s picture

Assigned: vineeth@nair » Unassigned
Issue tags: +Needs reroll

@vineeth@nair, are you still working on this?

subhojit777’s picture

Assigned: Unassigned » subhojit777
subhojit777’s picture

Assigned: subhojit777 » Unassigned
Status: Needs work » Needs review
Issue tags: -#DCM2015, -Needs reroll
StatusFileSize
new13.81 KB
jeroent’s picture

Issue tags: +Needs reroll

Patch no longer applies.

jeroent’s picture

Status: Needs review » Needs work
subhojit777’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
StatusFileSize
new13.84 KB
jeroent’s picture

Status: Needs review » Needs work
diff --git a/core/modules/comment/src/CommentViewBuilder.php b/core/modules/comment/src/CommentViewBuilder.php
index bf0f526..df371d2 100644
--- a/core/modules/comment/src/CommentViewBuilder.php
+++ b/core/modules/comment/src/CommentViewBuilder.php
@@ -2,12 +2,13 @@
 
 /**
  * @file
- * Contents Drupal\comment\CommentViewBuilder.
+ * Definition of Drupal\comment\CommentViewBuilder.
  */
 
 namespace Drupal\comment;
 
 use Drupal\Core\Cache\Cache;
+use Drupal\comment\Entity\Comment;
 use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
 use Drupal\Core\Entity\EntityInterface;
 use Drupal\Core\Entity\EntityViewBuilder;
@@ -130,6 +131,132 @@ public function buildComponents(array &$build, array $entities, array $displays,
   }
 
   /**
+   * #post_render_cache callback; replaces the placeholder with comment links.
+   *
+   * Renders the links on a comment.
+   *
+   * @param array $element
+   *   The renderable array that contains the to be replaced placeholder.
+   * @param array $context
+   *   An array with the following keys:
+   *   - comment_entity_id: a comment entity ID
+   *   - view_mode: the view mode in which the comment entity is being viewed
+   *   - langcode: in which language the comment entity is being viewed
+   *   - commented_entity_type: the entity type to which the comment is attached
+   *   - commented_entity_id: the entity ID to which the comment is attached
+   *   - in_preview: whether the comment is currently being previewed
+   *
+   * @return array
+   *   A renderable array representing the comment links.
+   */
+  public static function renderLinks(array $element, array $context) {
+    $callback = get_called_class() . '::renderLinks';
+    $placeholder = drupal_render_cache_generate_placeholder($callback, $context);
+    $links = array(
+      '#theme' => 'links__comment',
+      '#pre_render' => array('drupal_pre_render_links'),
+      '#attributes' => array('class' => array('links', 'inline')),
+    );
+
+    if (!$context['in_preview']) {
+      /**
+       * @var $comment CommentInterface
+       */
+      $comment = Comment::load($context['comment_entity_id']);
+      $commented_entity = $comment->getCommentedEntity();
+
+      $links['comment'] = static::buildLinks($comment, $commented_entity);
+      // Allow other modules to alter the comment links.
+      $hook_context = array(
+        'view_mode' => $context['view_mode'],
+        'langcode' => $context['langcode'],
+        'commented_entity' => $commented_entity
+      );
+      \Drupal::moduleHandler()->alter('comment_links', $links, $comment, $hook_context);
+    }
+    $markup = drupal_render($links);
+    $element['#markup'] = str_replace($placeholder, $markup, $element['#markup']);
+
+    return $element;
+  }
+
+  /**
+   * Build the default links (reply, edit, delete …) for a comment.
+   *
+   * @param \Drupal\comment\CommentInterface $entity
+   *   The comment object.
+   * @param \Drupal\Core\Entity\EntityInterface $commented_entity
+   *   The entity to which the comment is attached.
+   *
+   * @return array
+   *   An array that can be processed by drupal_pre_render_links().
+   */
+  protected static function buildLinks(CommentInterface $entity, EntityInterface $commented_entity) {
+    $links = array();
+    $status = $commented_entity->get($entity->getFieldName())->status;
+
+    $container = \Drupal::getContainer();
+
+    if ($status == CommentItemInterface::OPEN) {
+      if ($entity->access('delete')) {
+        $links['comment-delete'] = array(
+          'title' => t('Delete'),
+          'url' => $entity->urlInfo('delete-form'),
+          'html' => TRUE,
+        );
+      }
+
+      if ($entity->access('update')) {
+        $links['comment-edit'] = array(
+          'title' => t('Edit'),
+          'url' => $entity->urlInfo('edit-form'),
+          'html' => TRUE,
+        );
+      }
+      if ($entity->access('create')) {
+        $links['comment-reply'] = array(
+          'title' => t('Reply'),
+          'url' => Url::fromRoute('comment.reply', [
+            'entity_type' => $entity->getCommentedEntityTypeId(),
+            'entity' => $entity->getCommentedEntityId(),
+            'field_name' => $entity->getFieldName(),
+            'pid' => $entity->id(),
+          ]),
+          'html' => TRUE,
+        );
+      }
+      if (!$entity->isPublished() && $entity->access('approve')) {
+        $links['comment-approve'] = array(
+          'title' => t('Approve'),
+          'url' => Url::fromRoute('comment.approve', ['comment' => $entity->id()]),
+          'html' => TRUE,
+        );
+      }
+      if (empty($links) && \Drupal::currentUser()->isAnonymous()) {
+        $links['comment-forbidden']['title'] = \Drupal::service('comment.manager')->forbiddenMessage($commented_entity, $entity->getFieldName());
+        $links['comment-forbidden']['html'] = TRUE;
+      }
+    }
+
+    // Add translations link for translation-enabled comment bundles.
+    if (\Drupal::moduleHandler()->moduleExists('content_translation') && content_translation_translate_access($entity)->isAllowed()) {
+      $links['comment-translations'] = array(
+        'title' => t('Translate'),
+        'url' => $entity->urlInfo('drupal:content-translation-overview'),
+        'html' => TRUE,
+      );
+    }
+
+    return array(
+      '#theme' => 'links__comment__comment',
+      // The "entity" property is specified to be present, so no need to
+      // check.
+      '#links' => $links,
+      '#attributes' => array('class' => array('links', 'inline')),
+    );
+  }
+
+  /**
    * {@inheritdoc}
    */
   protected function alterBuild(array &$build, EntityInterface $comment, EntityViewDisplayInterface $display, $view_mode, $langcode = NULL) {
@@ -158,4 +285,52 @@ protected function alterBuild(array &$build, EntityInterface $comment, EntityVie
     }
   }
 
+  /**
+   * #post_render_cache callback; attaches "X new comments" link metadata.
+   *
+   * @param array $element
+   *   A render array with the following keys:
+   *   - #markup
+   *   - #attached
+   * @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.
+   */
+  public static function attachNewCommentsLinkMetadata(array $element, array $context) {
+    // Build "X new comments" link metadata.
+    $new = \Drupal::service('comment.manager')
+      ->getCountNewComments(entity_load($context['entity_type'], $context['entity_id']));
+    // Early-return if there are zero new comments for the current user.
+    if ($new === 0) {
+      return $element;
+    }
+    $entity = \Drupal::entityManager()
+      ->getStorage($context['entity_type'])
+      ->load($context['entity_id']);
+    $field_name = $context['field_name'];
+    $page_number = \Drupal::entityManager()
+      ->getStorage('comment')
+      ->getNewCommentPageNumber($entity->{$field_name}->comment_count, $new, $entity);
+    $query = $page_number ? array('page' => $page_number) : NULL;
+
+    // Attach metadata.
+    $element['#attached']['drupalSettings']['comment']['newCommentsLinks'] = [
+      $context['entity_type'] => [
+        $context['field_name'] => [
+          $context['entity_id'] => [
+            'new_comment_count' => (int)$new,
+            'first_new_comment_link' => \Drupal::urlGenerator()->generateFromPath('node/' . $entity->id(), ['query' => $query, 'fragment' => 'new']),
+          ],
+        ],
+      ],
+    ];
+
+    return $element;
+  }
+
 }

I Think your patch contains code of another issue.

linl’s picture

Status: Needs work » Needs review
StatusFileSize
new6.21 KB

Rerolled without the extra code. Have doubled checked and I can't find any more instances of comment_load(), entity_load('comment') or entity_load_multiple('comment').

The patch is smaller as many instances of entity_load('comment') were taken care of in #2350273: Move CommentViewBuilder::renderLinks post_render_cache callback to CommentPostRenderCache

jeroent’s picture

+++ b/core/modules/comment/src/Plugin/views/row/Rss.php
@@ -35,6 +37,38 @@ class Rss extends RssPluginBase {
+    $this->definition = $plugin_definition + $configuration;

Why are we doing this?

Other than that, I think the patch is good to go.

andypost’s picture

Status: Needs review » Reviewed & tested by the community

git grep returns no remains

rahul.shinde’s picture

Issue tags: +#drupalgoa2015
alexpott’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/modules/comment/src/Plugin/views/row/Rss.php
@@ -7,7 +7,9 @@
+use Drupal\Core\Entity\EntityManagerInterface;
...
+use Symfony\Component\DependencyInjection\ContainerInterface;

@@ -35,6 +37,38 @@ class Rss extends RssPluginBase {
   /**
+   * @var EntityManagerInterface
+   */
+  protected $entityManager;
...
+  /**
+   * {@inheritdoc}
+   */
+  public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityManagerInterface $entity_manager) {
+    parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_manager);
+
+    $this->definition = $plugin_definition + $configuration;
+    $this->entityManager = $entity_manager;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
+    return new static(
+      $configuration,
+      $plugin_id,
+      $plugin_definition,
+      $container->get('entity.manager')
+    );
+  }

This is not necessary the parent class (Drupal\node\Plugin\views\row\RssPluginBase) has the entity manager already

rpayanm’s picture

Status: Needs work » Needs review
StatusFileSize
new1.59 KB
new5.21 KB
andypost’s picture

Status: Needs review » Needs work

small nit, please fix

+++ b/core/modules/migrate_drupal/src/Tests/d6/MigrateCommentTest.php
@@ -76,8 +76,10 @@ protected function setUp() {
+    /** @var EntityStorageInterface */

should be full namespace and $comment_storage appended

rpayanm’s picture

Status: Needs work » Needs review
StatusFileSize
new766 bytes
new5.24 KB
andypost’s picture

Status: Needs review » Reviewed & tested by the community
alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed c4550b7 and pushed to 8.0.x. Thanks!

The beta evaluation is in the parent meta issue.

  • alexpott committed c4550b7 on 8.0.x
    Issue #2321599 by Temoor, rpayanm, subhojit777, vineeth@nair, LinL,...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.