User.php

Definition of Drupal\user\Plugin\views\field\User.

Namespace

Drupal\user\Plugin\views\field

File

drupal/core/modules/user/lib/Drupal/user/Plugin/views/field/User.php
View source
<?php

/**
 * @file
 * Definition of Drupal\user\Plugin\views\field\User.
 */
namespace Drupal\user\Plugin\views\field;

use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\Core\Annotation\Plugin;
use Drupal\views\ViewExecutable;

/**
 * Field handler to provide simple renderer that allows linking to a user.
 *
 * @ingroup views_field_handlers
 *
 * @Plugin(
 *   id = "user",
 *   module = "user"
 * )
 */
class User extends FieldPluginBase {

  /**
   * Override init function to provide generic option to link to user.
   */
  public function init(ViewExecutable $view, &$data) {
    parent::init($view, $data);
    if (!empty($this->options['link_to_user'])) {
      $this->additional_fields['uid'] = 'uid';
    }
  }
  protected function defineOptions() {
    $options = parent::defineOptions();
    $options['link_to_user'] = array(
      'default' => TRUE,
      'bool' => TRUE,
    );
    return $options;
  }

  /**
   * Provide link to node option
   */
  public function buildOptionsForm(&$form, &$form_state) {
    $form['link_to_user'] = array(
      '#title' => t('Link this field to its user'),
      '#description' => t("Enable to override this field's links."),
      '#type' => 'checkbox',
      '#default_value' => $this->options['link_to_user'],
    );
    parent::buildOptionsForm($form, $form_state);
  }
  function render_link($data, $values) {
    if (!empty($this->options['link_to_user']) && user_access('access user profiles') && ($uid = $this
      ->get_value($values, 'uid')) && $data !== NULL && $data !== '') {
      $this->options['alter']['make_link'] = TRUE;
      $this->options['alter']['path'] = "user/" . $uid;
    }
    return $data;
  }
  function render($values) {
    $value = $this
      ->get_value($values);
    return $this
      ->render_link($this
      ->sanitizeValue($value), $values);
  }

}

Classes

Namesort descending Description
User Field handler to provide simple renderer that allows linking to a user.