You are here

public function BaseUrl::render in Views base url 8

Same name and namespace in other branches
  1. 2.0.x src/Plugin/views/field/BaseUrl.php \Drupal\views_base_url\Plugin\views\field\BaseUrl::render()

Renders the field.

Parameters

\Drupal\views\ResultRow $values: The values retrieved from a single row of a view's query result.

Return value

string|\Drupal\Component\Render\MarkupInterface The rendered output. If the output is safe it will be wrapped in an object that implements MarkupInterface. If it is empty or unsafe it will be a string.

Overrides FieldPluginBase::render

File

src/Plugin/views/field/BaseUrl.php, line 170

Class

BaseUrl
A handler to output site's base url.

Namespace

Drupal\views_base_url\Plugin\views\field

Code

public function render(ResultRow $values) {
  global $base_url;
  global $language;
  if ($this->options['show_link']) {
    $tokens = $this
      ->getRenderTokens('');
    $link_query = [];

    // Link path.
    if (!empty($this->options['show_link_options']['link_path'])) {
      $aliased_path = $this
        ->viewsTokenReplace($this->options['show_link_options']['link_path'], $tokens);
      $aliased_path = $this->pathAliasManager
        ->getAliasByPath($aliased_path);
      $link_path = "{$base_url}{$aliased_path}";
    }
    else {
      $link_path = $base_url;
    }

    // Link text.
    if (!empty($this->options['show_link_options']['link_text'])) {
      $link_text = [
        '#plain_text' => $this->options['show_link_options']['link_text'],
      ];
    }
    else {
      $link_text = [
        '#plain_text' => $link_path,
      ];
    }

    // Link query.
    if (!empty($this->options['show_link_options']['link_query'])) {
      $queries = explode(' ', $this->options['show_link_options']['link_query']);
      foreach ($queries as $query) {
        $param = explode('=', $query);
        $link_query[$param[0]] = $param[1];
      }
    }

    // Create link with options.
    $url = Url::fromUri($link_path, [
      'attributes' => [
        'class' => explode(' ', $this->options['show_link_options']['link_class']),
        'title' => $this->options['show_link_options']['link_title'],
        'rel' => $this->options['show_link_options']['link_rel'],
        'target' => $this->options['show_link_options']['link_target'],
      ],
      'fragment' => $this->options['show_link_options']['link_fragment'],
      'query' => $link_query,
      'language' => $language,
    ]);

    // Replace token with values and return it as output.
    return [
      '#markup' => $this
        ->viewsTokenReplace(Link::fromTextAndUrl($link_text, $url)
        ->toString(), $tokens),
    ];
  }
  else {
    return [
      '#plain_text' => $base_url,
    ];
  }
}