RevisionLink.php 2.07 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Definition of Drupal\node\Plugin\views\field\RevisionLink.
6 7
 */

8
namespace Drupal\node\Plugin\views\field;
9

10
use Drupal\node\Plugin\views\field\Link;
11
use Drupal\views\ViewExecutable;
12 13
use Drupal\Core\Annotation\Plugin;

14 15 16 17
/**
 * Field handler to present a link to a node revision.
 *
 * @ingroup views_field_handlers
18
 *
19
 * @Plugin(
20 21
 *   id = "node_revision_link",
 *   module = "node"
22 23 24
 * )
 */
class RevisionLink extends Link {
25

26
  /**
27
   * Overrides Drupal\views\Plugin\views\field\FieldPluginBase::init().
28
   */
29 30
  public function init(ViewExecutable $view, &$options) {
    parent::init($view, $options);
31

32 33 34
    $this->additional_fields['node_vid'] = array('table' => 'node_revision', 'field' => 'vid');
  }

35
  public function access() {
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
    return user_access('view revisions') || user_access('administer nodes');
  }

  function render_link($data, $values) {
    list($node, $vid) = $this->get_revision_entity($values, 'view');
    if (!isset($vid)) {
      return;
    }

    // Current revision uses the node view path.
    $path = 'node/' . $node->nid;
    if ($node->vid != $vid) {
      $path .= "/revisions/$vid/view";
    }

    $this->options['alter']['make_link'] = TRUE;
    $this->options['alter']['path'] = $path;
    $this->options['alter']['query'] = drupal_get_destination();

    return !empty($this->options['text']) ? $this->options['text'] : t('view');
  }

  /**
   * Returns the revision values of a node.
   *
   * @param object $values
   *   An object containing all retrieved values.
   * @param string $op
   *   The operation being performed.
   *
   * @return array
   *   A numerically indexed array containing the current node object and the
   *   revision ID for this row.
   */
  function get_revision_entity($values, $op) {
    $vid = $this->get_value($values, 'node_vid');
    $node = $this->get_value($values);
    // Unpublished nodes ignore access control.
    $node->status = 1;
    // Ensure user has access to perform the operation on this node.
    if (!node_access($op, $node)) {
      return array($node, NULL);
    }
    return array($node, $vid);
  }
81

82
}