3 namespace Drupal\views\Plugin\views\field;
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\Core\Url as CoreUrl;
7 use Drupal\views\ResultRow;
10 * Field handler to provide simple renderer that turns a URL into a clickable link.
12 * @ingroup views_field_handlers
16 class Url extends FieldPluginBase {
21 protected function defineOptions() {
22 $options = parent::defineOptions();
24 $options['display_as_link'] = ['default' => TRUE];
30 * Provide link to the page being visited.
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']),
38 parent::buildOptionsForm($form, $form_state);
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));
52 return $this->sanitizeValue($value, 'url');