0

The User module defines a UserData plugin for the Views module, which allows to access the user.data service from a view. Its render() method contains the following code.

$uid = $this->getValue($values);
$data = $this->userData->get($this->options['data_module'], $uid, $this->options['data_name']);

// Don't sanitize if no value was found.
if (isset($data)) {
  return $this->sanitizeValue($data);
}

}

How does the Views module know the plugin require a user ID? In other words, how does the Views module know that is a field plugin associated with the User entity and not another entity?

The annotation for the plugin is simply the following one.

@ViewsField("user_data")

Neither its create() nor its constructor method set something that would make clear to which entity the plugin is associated.

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;
}
1

The available fields for the user entity are defined in Drupal\user\UserViewsData::getViewsData():

$data['users']['data'] = [
  'title' => $this->t('Data'),
  'help' => $this->t('Provides access to the user data service.'),
  'real field' => 'uid',
  'field' => [
    'id' => 'user_data',
  ],
];

This attaches the field user_data to the user entity.

  • And it defines it as an alias of the uid field; that is why it is getting the user ID that is then used to retrieve the data using the user.data service. – kiamlaluno Jun 14 '17 at 7:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.