Version 1
[yaffs-website] / web / core / modules / views / src / Plugin / views / field / Url.php
1 <?php
2
3 namespace Drupal\views\Plugin\views\field;
4
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\Core\Url as CoreUrl;
7 use Drupal\views\ResultRow;
8
9 /**
10  * Field handler to provide simple renderer that turns a URL into a clickable link.
11  *
12  * @ingroup views_field_handlers
13  *
14  * @ViewsField("url")
15  */
16 class Url extends FieldPluginBase {
17
18   /**
19    * {@inheritdoc}
20    */
21   protected function defineOptions() {
22     $options = parent::defineOptions();
23
24     $options['display_as_link'] = ['default' => TRUE];
25
26     return $options;
27   }
28
29   /**
30    * Provide link to the page being visited.
31    */
32   public function buildOptionsForm(&$form, FormStateInterface $form_state) {
33     $form['display_as_link'] = [
34       '#title' => $this->t('Display as link'),
35       '#type' => 'checkbox',
36       '#default_value' => !empty($this->options['display_as_link']),
37     ];
38     parent::buildOptionsForm($form, $form_state);
39   }
40
41   /**
42    * {@inheritdoc}
43    */
44   public function render(ResultRow $values) {
45     $value = $this->getValue($values);
46     if (!empty($this->options['display_as_link'])) {
47       // @todo Views should expect and store a leading /. See:
48       //   https://www.drupal.org/node/2423913
49       return \Drupal::l($this->sanitizeValue($value), CoreUrl::fromUserInput('/' . $value));
50     }
51     else {
52       return $this->sanitizeValue($value, 'url');
53     }
54   }
55
56 }