From a94d165493e019c712846446f7f85b8cc85e1c46 Mon Sep 17 00:00:00 2001 From: William Hearn Date: Tue, 7 Nov 2017 09:54:45 -0500 Subject: [PATCH] Issue #2715483 by tar_inet, Daniel Korte: [regression] getEntity() doesn't always return entity, which results in add comment field not working in Views --- .../comment/src/Plugin/views/field/EntityLink.php | 75 +++++++++++++++++----- 1 file changed, 58 insertions(+), 17 deletions(-) diff --git a/core/modules/comment/src/Plugin/views/field/EntityLink.php b/core/modules/comment/src/Plugin/views/field/EntityLink.php index a2c6911..3b155f1 100644 --- a/core/modules/comment/src/Plugin/views/field/EntityLink.php +++ b/core/modules/comment/src/Plugin/views/field/EntityLink.php @@ -5,6 +5,9 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\views\Plugin\views\field\FieldPluginBase; use Drupal\views\ResultRow; +use Drupal\comment\CommentLinkBuilder; +use Symfony\Component\DependencyInjection\ContainerInterface; +use Drupal\Core\Render\RendererInterface; /** * Handler for showing comment module's entity links. @@ -23,6 +26,56 @@ class EntityLink extends FieldPluginBase { protected $build; /** + * Comment links builder for a commented entity. + * @var \Drupal\comment\CommentLinkBuilder + */ + protected $commentLinkBuilder; + + /** + * The render object. + * @var \Drupal\Core\Render\RendererInterface + */ + protected $renderer; + + /** + * Constructs a new EntityLink object. + * + * @param array $configuration + * A configuration array containing information about the plugin instance. + * @param string $plugin_id + * The plugin ID for the plugin instance. + * @param mixed $plugin_definition + * The plugin implementation definition. + * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager + * The entity manager. + * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager + * The language manager. + * @param \Drupal\comment\CommentLinkBuilder $commentLinkBuilder + * Comment links builder for a commented entity. + * @param \Drupal\Core\Render\RendererInterface $renderer + * The render object. + */ + public function __construct(array $configuration, $plugin_id, $plugin_definition, CommentLinkBuilder $commentLinkBuilder, RendererInterface $renderer) { + parent::__construct($configuration, $plugin_id, $plugin_definition); + + $this->commentLinkBuilder = $commentLinkBuilder; + $this->renderer = $renderer; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('comment.link_builder'), + $container->get('renderer') + ); + } + + /** * {@inheritdoc} */ protected function defineOptions() { @@ -53,26 +106,14 @@ public function query() {} /** * {@inheritdoc} */ - public function preRender(&$values) { - // Render all nodes, so you can grep the comment links. - $entities = []; - foreach ($values as $row) { - $entity = $row->_entity; - $entities[$entity->id()] = $entity; - } - if ($entities) { - $this->build = entity_view_multiple($entities, $this->options['teaser'] ? 'teaser' : 'full'); - } - } - - /** - * {@inheritdoc} - */ public function render(ResultRow $values) { $entity = $this->getEntity($values); - // Only render the links, if they are defined. - return !empty($this->build[$entity->id()]['links']['comment__comment']) ? \Drupal::service('renderer')->render($this->build[$entity->id()]['links']['comment__comment']) : ''; + // Create comment links for the entity. + $context = ['view_mode' => $this->options['teaser'] ? 'teaser' : 'full']; + $comment_links = $this->commentLinkBuilder ->buildCommentedEntityLinks($entity, $context); + + return !empty($comment_links['comment__comment']) ? $this->renderer->render($comment_links['comment__comment']) : ''; } } -- 2.5.4 (Apple Git-61)