Url.php in Drupal 8
File
core/modules/views/src/Plugin/views/field/Url.php
View source
<?php
namespace Drupal\views\Plugin\views\field;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Drupal\Core\Url as CoreUrl;
use Drupal\views\ResultRow;
class Url extends FieldPluginBase {
protected function defineOptions() {
$options = parent::defineOptions();
$options['display_as_link'] = [
'default' => TRUE,
];
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
$form['display_as_link'] = [
'#title' => $this
->t('Display as link'),
'#type' => 'checkbox',
'#default_value' => !empty($this->options['display_as_link']),
];
parent::buildOptionsForm($form, $form_state);
}
public function render(ResultRow $values) {
$value = $this
->getValue($values);
if (!empty($this->options['display_as_link'])) {
return Link::fromTextAndUrl($this
->sanitizeValue($value), CoreUrl::fromUserInput('/' . $value))
->toString();
}
else {
return $this
->sanitizeValue($value, 'url');
}
}
}
Classes
Name |
Description |
Url |
Field handler to provide simple renderer that turns a URL into a clickable link. |