function ContextualLinks::render

Render the contextual fields.

Overrides FieldPluginBase::render

See also

contextual_preprocess()

contextual_contextual_links_view_alter()

File

drupal/core/modules/contextual/lib/Drupal/contextual/Plugin/views/field/ContextualLinks.php, line 71
Definition of Drupal\contextual\Plugin\views\field\ContextualLinks.

Class

ContextualLinks
Provides a handler that adds contextual links.

Namespace

Drupal\contextual\Plugin\views\field

Code

function render($values) {
  $links = array();
  foreach ($this->options['fields'] as $field) {
    $rendered_field = $this->view->style_plugin
      ->getField($this->view->row_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'];
    }
    if (!empty($title) && !empty($path)) {

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

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