365ab3b746a6a3b285855b4bd64d4e6ba3269867
[yaffs-website] / 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   /**
39    * {@inheritdoc}
40    */
41   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
42     return new static($configuration, $plugin_id, $plugin_definition, $container->get('user.data'), $container->get('module_handler'));
43   }
44
45   /**
46    * Constructs a UserData object.
47    */
48   public function __construct(array $configuration, $plugin_id, $plugin_definition, UserDataInterface $user_data, ModuleHandlerInterface $module_handler) {
49     parent::__construct($configuration, $plugin_id, $plugin_definition);
50
51     $this->userData = $user_data;
52     $this->moduleHandler = $module_handler;
53   }
54
55   /**
56    * {@inheritdoc}
57    */
58   protected function defineOptions() {
59     $options = parent::defineOptions();
60
61     $options['data_module'] = ['default' => ''];
62     $options['data_name'] = ['default' => ''];
63
64     return $options;
65   }
66
67   /**
68    * {@inheritdoc}
69    */
70   public function buildOptionsForm(&$form, FormStateInterface $form_state) {
71     parent::buildOptionsForm($form, $form_state);
72
73     $modules = $this->moduleHandler->getModuleList();
74     $names = [];
75     foreach (array_keys($modules) as $name) {
76       $names[$name] = $this->moduleHandler->getName($name);
77     }
78
79     $form['data_module'] = [
80       '#title' => $this->t('Module name'),
81       '#type' => 'select',
82       '#description' => $this->t('The module which sets this user data.'),
83       '#default_value' => $this->options['data_module'],
84       '#options' => $names,
85     ];
86
87     $form['data_name'] = [
88       '#title' => $this->t('Name'),
89       '#type' => 'textfield',
90       '#description' => $this->t('The name of the data key.'),
91       '#default_value' => $this->options['data_name'],
92     ];
93   }
94
95   /**
96    * {@inheritdoc}
97    */
98   public function render(ResultRow $values) {
99     $uid = $this->getValue($values);
100     $data = $this->userData->get($this->options['data_module'], $uid, $this->options['data_name']);
101
102     // Don't sanitize if no value was found.
103     if (isset($data)) {
104       return $this->sanitizeValue($data);
105     }
106   }
107
108 }