You are here

class Url in Drupal 10

Same name in this branch
  1. 10 core/lib/Drupal/Core/Url.php \Drupal\Core\Url
  2. 10 core/lib/Drupal/Core/Render/Element/Url.php \Drupal\Core\Render\Element\Url
  3. 10 core/modules/views/src/Plugin/views/field/Url.php \Drupal\views\Plugin\views\field\Url
Same name and namespace in other branches
  1. 8 core/modules/views/src/Plugin/views/field/Url.php \Drupal\views\Plugin\views\field\Url
  2. 9 core/modules/views/src/Plugin/views/field/Url.php \Drupal\views\Plugin\views\field\Url

Field handler to provide simple renderer that turns a URL into a clickable link.

Plugin annotation

@ViewsField("url");

Hierarchy

  • class \Drupal\views\Plugin\views\field\Url extends \Drupal\views\Plugin\views\field\FieldPluginBase

Expanded class hierarchy of Url

File

core/modules/views/src/Plugin/views/field/Url.php, line 17

Namespace

Drupal\views\Plugin\views\field
View source
class Url extends FieldPluginBase {

  /**
   * {@inheritdoc}
   */
  protected function defineOptions() {
    $options = parent::defineOptions();
    $options['display_as_link'] = [
      'default' => TRUE,
    ];
    return $options;
  }

  /**
   * Provide link to the page being visited.
   */
  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);
  }

  /**
   * {@inheritdoc}
   */
  public function render(ResultRow $values) {
    $value = $this
      ->getValue($values);
    if (!empty($this->options['display_as_link'])) {

      // @todo Views should expect and store a leading /. See:
      //   https://www.drupal.org/node/2423913
      return Link::fromTextAndUrl($this
        ->sanitizeValue($value), CoreUrl::fromUserInput('/' . $value))
        ->toString();
    }
    else {
      return $this
        ->sanitizeValue($value, 'url');
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Url::buildOptionsForm public function Provide link to the page being visited.
Url::defineOptions protected function
Url::render public function