Link.php 1.75 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Definition of Views\user\Plugin\views\field\Link.
6 7
 */

8 9 10 11
namespace Views\user\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 link to the user.
 *
 * @ingroup views_field_handlers
18
 *
19
 * @Plugin(
20 21
 *   id = "user_link",
 *   module = "user"
22 23
 * )
 */
24
class Link extends FieldPluginBase {
25

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

32 33 34
    $this->additional_fields['uid'] = 'uid';
  }

35 36
  protected function defineOptions() {
    $options = parent::defineOptions();
37 38 39 40
    $options['text'] = array('default' => '', 'translatable' => TRUE);
    return $options;
  }

41
  public function buildOptionsForm(&$form, &$form_state) {
42 43 44 45 46
    $form['text'] = array(
      '#type' => 'textfield',
      '#title' => t('Text to display'),
      '#default_value' => $this->options['text'],
    );
47
    parent::buildOptionsForm($form, $form_state);
48 49 50
  }

  // An example of field level access control.
51
  public function access() {
52 53 54
    return user_access('access user profiles');
  }

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

  function render($values) {
    $value = $this->get_value($values, 'uid');
62
    return $this->render_link($this->sanitizeValue($value), $values);
63 64 65 66 67 68 69 70 71 72 73 74
  }

  function render_link($data, $values) {
    $text = !empty($this->options['text']) ? $this->options['text'] : t('view');

    $this->options['alter']['make_link'] = TRUE;
    $this->options['alter']['path'] = "user/" . $data;

    return $text;
  }

}