You are here

public function ContextualLinks::render in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 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 100
Contains \Drupal\contextual\Plugin\views\field\ContextualLinks.

Class

ContextualLinks
Provides a handler that adds contextual links.

Namespace

Drupal\contextual\Plugin\views\field

Code

public function render(ResultRow $values) {
  $links = array();
  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(array());
      $path = strip_tags(Html::decodeEntities(strtr($path, $tokens)));
      $links[$field] = array(
        'href' => $path,
        'title' => $title,
      );
      if (!empty($this->options['destination'])) {
        $links[$field]['query'] = $this
          ->getDestinationArray();
      }
    }
  }

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