1a4bb82acdd5e91e778400f0cbd2acb0e9cc3b22
[yaffs-website] / web / core / modules / user / src / Plugin / views / field / UserData.php
1 <?php
2
3 namespace Drupal\user\Plugin\views\field;
4
5 use Drupal\Core\Extension\ModuleHandlerInterface;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\views\Plugin\views\field\FieldPluginBase;
8 use Drupal\views\ResultRow;
9 use Drupal\user\UserDataInterface;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11
12 /**
13  * Provides access to the user data service.
14  *
15  * @ingroup views_field_handlers
16  *
17  * @see \Drupal\user\UserDataInterface
18  *
19  * @ViewsField("user_data")
20  */
21 class UserData extends FieldPluginBase {
22
23   /**
24    * Provides the user data service object.
25    *
26    * @var \Drupal\user\UserDataInterface
27    */
28   protected $userData;
29
30   /**
31    * The module handler.
32    *
33    * @var \Drupal\Core\Extension\ModuleHandlerInterface
34    */
35   protected $moduleHandler;
36
37   /**
38    * {@inheritdoc}
39    */
40   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
41     return new static($configuration, $plugin_id, $plugin_definition, $container->get('user.data'), $container->get('module_handler'));
42   }
43
44   /**
45    * Constructs a UserData object.
46    */
47   public function __construct(array $configuration, $plugin_id, $plugin_definition, UserDataInterface $user_data, ModuleHandlerInterface $module_handler) {
48     parent::__construct($configuration, $plugin_id, $plugin_definition);
49
50     $this->userData = $user_data;
51     $this->moduleHandler = $module_handler;
52   }
53
54   /**
55    * {@inheritdoc}
56    */
57   protected function defineOptions() {
58     $options = parent::defineOptions();
59
60     $options['data_module'] = ['default' => ''];
61     $options['data_name'] = ['default' => ''];
62
63     return $options;
64   }
65
66   /**
67    * {@inheritdoc}
68    */
69   public function buildOptionsForm(&$form, FormStateInterface $form_state) {
70     parent::buildOptionsForm($form, $form_state);
71
72     $modules = $this->moduleHandler->getModuleList();
73     $names = [];
74     foreach (array_keys($modules) as $name) {
75       $names[$name] = $this->moduleHandler->getName($name);
76     }
77
78     $form['data_module'] = [
79       '#title' => $this->t('Module name'),
80       '#type' => 'select',
81       '#description' => $this->t('The module which sets this user data.'),
82       '#default_value' => $this->options['data_module'],
83       '#options' => $names,
84     ];
85
86     $form['data_name'] = [
87       '#title' => $this->t('Name'),
88       '#type' => 'textfield',
89       '#description' => $this->t('The name of the data key.'),
90       '#default_value' => $this->options['data_name'],
91     ];
92   }
93
94   /**
95    * {@inheritdoc}
96    */
97   public function render(ResultRow $values) {
98     $uid = $this->getValue($values);
99     $data = $this->userData->get($this->options['data_module'], $uid, $this->options['data_name']);
100
101     // Don't sanitize if no value was found.
102     if (isset($data)) {
103       return $this->sanitizeValue($data);
104     }
105   }
106
107 }