Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / modules / views / src / Plugin / views / field / EntityLink.php
1 <?php
2
3 namespace Drupal\views\Plugin\views\field;
4
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\views\ResultRow;
7
8 /**
9  * Field handler to present a link to an entity.
10  *
11  * @ingroup views_field_handlers
12  *
13  * @ViewsField("entity_link")
14  */
15 class EntityLink extends LinkBase {
16
17   /**
18    * {@inheritdoc}
19    */
20   public function render(ResultRow $row) {
21     return $this->getEntity($row) ? parent::render($row) : [];
22   }
23
24   /**
25    * {@inheritdoc}
26    */
27   protected function renderLink(ResultRow $row) {
28     if ($this->options['output_url_as_text']) {
29       return $this->getUrlInfo($row)->toString();
30     }
31     return parent::renderLink($row);
32   }
33
34   /**
35    * {@inheritdoc}
36    */
37   protected function getUrlInfo(ResultRow $row) {
38     $template = $this->getEntityLinkTemplate();
39     return $this->getEntity($row)->toUrl($template)->setAbsolute($this->options['absolute']);
40   }
41
42   /**
43    * Returns the entity link template name identifying the link route.
44    *
45    * @returns string
46    *   The link template name.
47    */
48   protected function getEntityLinkTemplate() {
49     return 'canonical';
50   }
51
52   /**
53    * {@inheritdoc}
54    */
55   protected function getDefaultLabel() {
56     return $this->t('view');
57   }
58
59   /**
60    * {@inheritdoc}
61    */
62   protected function defineOptions() {
63     $options = parent::defineOptions();
64     $options['output_url_as_text'] = ['default' => FALSE];
65     $options['absolute'] = ['default' => FALSE];
66     return $options;
67   }
68
69   /**
70    * {@inheritdoc}
71    */
72   public function buildOptionsForm(&$form, FormStateInterface $form_state) {
73     $form['output_url_as_text'] = [
74       '#type' => 'checkbox',
75       '#title' => $this->t('Output the URL as text'),
76       '#default_value' => $this->options['output_url_as_text'],
77     ];
78     $form['absolute'] = [
79       '#type' => 'checkbox',
80       '#title' => $this->t('Use absolute link (begins with "http://")'),
81       '#default_value' => $this->options['absolute'],
82       '#description' => $this->t('Enable this option to output an absolute link. Required if you want to use the path as a link destination.'),
83     ];
84     parent::buildOptionsForm($form, $form_state);
85     // Only show the 'text' field if we don't want to output the raw URL.
86     $form['text']['#states']['visible'][':input[name="options[output_url_as_text]"]'] = ['checked' => FALSE];
87   }
88
89 }