LinkEdit.php 2.25 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Definition of Views\taxonomy\Plugin\views\field\LinkEdit.
6 7
 */

8 9 10 11
namespace Views\taxonomy\Plugin\views\field;

use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\Core\Annotation\Plugin;
12
use Drupal\Component\Plugin\Discovery\DiscoveryInterface;
13

14 15 16 17
/**
 * Field handler to present a term edit link.
 *
 * @ingroup views_field_handlers
18
 *
19
 * @Plugin(
20 21
 *   id = "term_link_edit",
 *   module = "taxonomy"
22 23 24
 * )
 */
class LinkEdit extends FieldPluginBase {
25

26 27 28 29 30 31
  /**
   * Constructs a LinkEdit object.
   */
  public function __construct(array $configuration, $plugin_id, DiscoveryInterface $discovery) {
    parent::__construct($configuration, $plugin_id, $discovery);

32 33 34 35 36 37 38 39
    $this->additional_fields['tid'] = 'tid';
    $this->additional_fields['vid'] = 'vid';
    $this->additional_fields['vocabulary_machine_name'] = array(
      'table' => 'taxonomy_vocabulary',
      'field' => 'machine_name',
    );
  }

40 41
  protected function defineOptions() {
    $options = parent::defineOptions();
42 43 44 45 46 47

    $options['text'] = array('default' => '', 'translatable' => TRUE);

    return $options;
  }

48
  public function buildOptionsForm(&$form, &$form_state) {
49 50 51 52 53
    $form['text'] = array(
      '#type' => 'textfield',
      '#title' => t('Text to display'),
      '#default_value' => $this->options['text'],
    );
54
    parent::buildOptionsForm($form, $form_state);
55 56
  }

57
  public function query() {
58
    $this->ensureMyTable();
59 60 61 62
    $this->add_additional_fields();
  }

  function render($values) {
63 64 65 66 67 68 69 70 71 72 73 74 75
    // Check there is an actual value, as on a relationship there may not be.
    if ($tid = $this->get_value($values, 'tid')) {
      // Mock a term object for taxonomy_term_access(). Use machine name and
      // vid to ensure compatibility with vid based and machine name based
      // access checks. See http://drupal.org/node/995156
      $term = entity_create('taxonomy_term', array(
        'vid' => $values->{$this->aliases['vid']},
        'vocabulary_machine_name' => $values->{$this->aliases['vocabulary_machine_name']},
      ));
      if (taxonomy_term_access('edit', $term)) {
        $text = !empty($this->options['text']) ? $this->options['text'] : t('edit');
        return l($text, 'taxonomy/term/'. $tid . '/edit', array('query' => drupal_get_destination()));
      }
76 77
    }
  }
78

79
}