3 namespace Drupal\comment\Plugin\views\field;
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\views\Plugin\views\field\FieldPluginBase;
7 use Drupal\views\ResultRow;
10 * Handler for showing comment module's entity links.
12 * @ingroup views_field_handlers
14 * @ViewsField("comment_entity_link")
16 class EntityLink extends FieldPluginBase {
19 * Stores the result of node_view_multiple for all rows to reuse it later.
28 protected function defineOptions() {
29 $options = parent::defineOptions();
30 $options['teaser'] = ['default' => FALSE];
37 public function buildOptionsForm(&$form, FormStateInterface $form_state) {
39 '#type' => 'checkbox',
40 '#title' => $this->t('Show teaser-style link'),
41 '#default_value' => $this->options['teaser'],
42 '#description' => $this->t('Show the comment link in the form used on standard entity teasers, rather than the full entity form.'),
45 parent::buildOptionsForm($form, $form_state);
51 public function query() {}
56 public function preRender(&$values) {
57 // Render all nodes, so you can grep the comment links.
59 foreach ($values as $row) {
60 $entity = $row->_entity;
61 $entities[$entity->id()] = $entity;
64 $this->build = entity_view_multiple($entities, $this->options['teaser'] ? 'teaser' : 'full');
71 public function render(ResultRow $values) {
72 $entity = $this->getEntity($values);
74 // Only render the links, if they are defined.
75 return !empty($this->build[$entity->id()]['links']['comment__comment']) ? drupal_render($this->build[$entity->id()]['links']['comment__comment']) : '';