You are here

public function ContextualLinks::render in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/contextual/src/Plugin/views/field/ContextualLinks.php \Drupal\contextual\Plugin\views\field\ContextualLinks::render()

Overrides \Drupal\views\Plugin\views\field\FieldPluginBase::render().

Renders the contextual fields.

Parameters

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

Overrides FieldPluginBase::render

See also

contextual_preprocess()

contextual_contextual_links_view_alter()

File

core/modules/contextual/src/Plugin/views/field/ContextualLinks.php, line 95

Class

ContextualLinks
Provides a handler that adds contextual links.

Namespace

Drupal\contextual\Plugin\views\field

Code

public function render(ResultRow $values) {
  $links = [];
  foreach ($this->options['fields'] as $field) {
    $rendered_field = $this->view->style_plugin
      ->getField($values->index, $field);
    if (empty($rendered_field)) {
      continue;
    }
    $title = $this->view->field[$field]->last_render_text;
    $path = '';
    if (!empty($this->view->field[$field]->options['alter']['path'])) {
      $path = $this->view->field[$field]->options['alter']['path'];
    }
    elseif (!empty($this->view->field[$field]->options['alter']['url']) && $this->view->field[$field]->options['alter']['url'] instanceof Url) {
      $path = $this->view->field[$field]->options['alter']['url']
        ->toString();
    }
    if (!empty($title) && !empty($path)) {

      // Make sure that tokens are replaced for this paths as well.
      $tokens = $this
        ->getRenderTokens([]);
      $path = strip_tags(Html::decodeEntities(strtr($path, $tokens)));
      $links[$field] = [
        'href' => $path,
        'title' => $title,
      ];
      if (!empty($this->options['destination'])) {
        $links[$field]['query'] = $this
          ->getDestinationArray();
      }
    }
  }

  // Renders a contextual links placeholder.
  if (!empty($links)) {
    $contextual_links = [
      'contextual' => [
        '',
        [],
        [
          'contextual-views-field-links' => UrlHelper::encodePath(Json::encode($links)),
        ],
      ],
    ];
    $element = [
      '#type' => 'contextual_links_placeholder',
      '#id' => _contextual_links_to_id($contextual_links),
    ];
    return \Drupal::service('renderer')
      ->render($element);
  }
  else {
    return '';
  }
}