EntityLink.php

Same filename in this branch
  1. 9 core/modules/views/src/Plugin/views/field/EntityLink.php
Same filename in other branches
  1. 8.9.x core/modules/views/src/Plugin/views/field/EntityLink.php
  2. 8.9.x core/modules/comment/src/Plugin/views/field/EntityLink.php
  3. 10 core/modules/views/src/Plugin/views/field/EntityLink.php
  4. 10 core/modules/comment/src/Plugin/views/field/EntityLink.php
  5. 11.x core/modules/views/src/Plugin/views/field/EntityLink.php
  6. 11.x core/modules/comment/src/Plugin/views/field/EntityLink.php

Namespace

Drupal\comment\Plugin\views\field

File

core/modules/comment/src/Plugin/views/field/EntityLink.php

View source
<?php

namespace Drupal\comment\Plugin\views\field;

use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\ResultRow;

/**
 * Handler for showing comment module's entity links.
 *
 * @ingroup views_field_handlers
 *
 * @ViewsField("comment_entity_link")
 */
class EntityLink extends FieldPluginBase {
    
    /**
     * Stores the result of parent entities build for all rows to reuse it later.
     *
     * @var array
     */
    protected $build;
    
    /**
     * {@inheritdoc}
     */
    protected function defineOptions() {
        $options = parent::defineOptions();
        $options['teaser'] = [
            'default' => FALSE,
        ];
        return $options;
    }
    
    /**
     * {@inheritdoc}
     */
    public function buildOptionsForm(&$form, FormStateInterface $form_state) {
        $form['teaser'] = [
            '#type' => 'checkbox',
            '#title' => $this->t('Show teaser-style link'),
            '#default_value' => $this->options['teaser'],
            '#description' => $this->t('Show the comment link in the form used on standard entity teasers, rather than the full entity form.'),
        ];
        parent::buildOptionsForm($form, $form_state);
    }
    
    /**
     * {@inheritdoc}
     */
    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) {
            $entityTypeId = reset($entities)->getEntityTypeId();
            $viewMode = $this->options['teaser'] ? 'teaser' : 'full';
            $this->build = \Drupal::entityTypeManager()->getViewBuilder($entityTypeId)
                ->viewMultiple($entities, $viewMode);
        }
    }
    
    /**
     * {@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']) : '';
    }

}

Classes

Title Deprecated Summary
EntityLink Handler for showing comment module's entity links.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.