View source
<?php
namespace Drupal\user\Plugin\views\field;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\ResultRow;
use Drupal\user\UserDataInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class UserData extends FieldPluginBase {
protected $userData;
protected $moduleHandler;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('user.data'), $container
->get('module_handler'));
}
public function __construct(array $configuration, $plugin_id, $plugin_definition, UserDataInterface $user_data, ModuleHandlerInterface $module_handler) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->userData = $user_data;
$this->moduleHandler = $module_handler;
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['data_module'] = [
'default' => '',
];
$options['data_name'] = [
'default' => '',
];
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$modules = $this->moduleHandler
->getModuleList();
$names = [];
foreach (array_keys($modules) as $name) {
$names[$name] = $this->moduleHandler
->getName($name);
}
$form['data_module'] = [
'#title' => $this
->t('Module name'),
'#type' => 'select',
'#description' => $this
->t('The module which sets this user data.'),
'#default_value' => $this->options['data_module'],
'#options' => $names,
];
$form['data_name'] = [
'#title' => $this
->t('Name'),
'#type' => 'textfield',
'#description' => $this
->t('The name of the data key.'),
'#default_value' => $this->options['data_name'],
];
}
public function render(ResultRow $values) {
$uid = $this
->getValue($values);
$data = $this->userData
->get($this->options['data_module'], $uid, $this->options['data_name']);
if (isset($data)) {
return $this
->sanitizeValue($data);
}
}
}