Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / comment / src / Plugin / views / field / EntityLink.php
1 <?php
2
3 namespace Drupal\comment\Plugin\views\field;
4
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\views\Plugin\views\field\FieldPluginBase;
7 use Drupal\views\ResultRow;
8
9 /**
10  * Handler for showing comment module's entity links.
11  *
12  * @ingroup views_field_handlers
13  *
14  * @ViewsField("comment_entity_link")
15  */
16 class EntityLink extends FieldPluginBase {
17
18   /**
19    * Stores the result of node_view_multiple for all rows to reuse it later.
20    *
21    * @var array
22    */
23   protected $build;
24
25   /**
26    * {@inheritdoc}
27    */
28   protected function defineOptions() {
29     $options = parent::defineOptions();
30     $options['teaser'] = ['default' => FALSE];
31     return $options;
32   }
33
34   /**
35    * {@inheritdoc}
36    */
37   public function buildOptionsForm(&$form, FormStateInterface $form_state) {
38     $form['teaser'] = [
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.'),
43     ];
44
45     parent::buildOptionsForm($form, $form_state);
46   }
47
48   /**
49    * {@inheritdoc}
50    */
51   public function query() {}
52
53   /**
54    * {@inheritdoc}
55    */
56   public function preRender(&$values) {
57     // Render all nodes, so you can grep the comment links.
58     $entities = [];
59     foreach ($values as $row) {
60       $entity = $row->_entity;
61       $entities[$entity->id()] = $entity;
62     }
63     if ($entities) {
64       $this->build = entity_view_multiple($entities, $this->options['teaser'] ? 'teaser' : 'full');
65     }
66   }
67
68   /**
69    * {@inheritdoc}
70    */
71   public function render(ResultRow $values) {
72     $entity = $this->getEntity($values);
73
74     // Only render the links, if they are defined.
75     return !empty($this->build[$entity->id()]['links']['comment__comment']) ? \Drupal::service('renderer')->render($this->build[$entity->id()]['links']['comment__comment']) : '';
76   }
77
78 }