class User in Views (for Drupal 7) 8.3
Same name in this branch
- 8.3 lib/Views/user/Plugin/views/argument_default/User.php \Views\user\Plugin\views\argument_default\User
- 8.3 lib/Views/user/Plugin/views/argument_validator/User.php \Views\user\Plugin\views\argument_validator\User
- 8.3 lib/Views/user/Plugin/views/field/User.php \Views\user\Plugin\views\field\User
Validate whether an argument is a valid user.
This supports either numeric arguments (UID) or strings (username) and converts either one into the user's UID. This validator also sets the argument's title to the username.
Plugin annotation
@Plugin(
id = "user",
module = "user",
title = @Translation("User")
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\views\Plugin\views\PluginBase
- class \Drupal\views\Plugin\views\argument_validator\ArgumentValidatorPluginBase
- class \Views\user\Plugin\views\argument_validator\User
- class \Drupal\views\Plugin\views\argument_validator\ArgumentValidatorPluginBase
- class \Drupal\views\Plugin\views\PluginBase
Expanded class hierarchy of User
4 string references to 'User'
- file_views_data in modules/
file.views.inc - Implements hook_views_data().
- node_views_data in modules/
node.views.inc - Implements hook_views_data().
- statistics_views_data in modules/
statistics.views.inc - Implements hook_views_data().
- user_views_data in modules/
user.views.inc - Implements hook_views_data().
File
- lib/
Views/ user/ Plugin/ views/ argument_validator/ User.php, line 27 - Definition of Views\user\Plugin\views\argument_validator\User.
Namespace
Views\user\Plugin\views\argument_validatorView source
class User extends ArgumentValidatorPluginBase {
protected function defineOptions() {
$options = parent::defineOptions();
$options['type'] = array(
'default' => 'uid',
);
$options['restrict_roles'] = array(
'default' => FALSE,
'bool' => TRUE,
);
$options['roles'] = array(
'default' => array(),
);
return $options;
}
public function buildOptionsForm(&$form, &$form_state) {
$form['type'] = array(
'#type' => 'radios',
'#title' => t('Type of user filter value to allow'),
'#options' => array(
'uid' => t('Only allow numeric UIDs'),
'name' => t('Only allow string usernames'),
'either' => t('Allow both numeric UIDs and string usernames'),
),
'#default_value' => $this->options['type'],
);
$form['restrict_roles'] = array(
'#type' => 'checkbox',
'#title' => t('Restrict user based on role'),
'#default_value' => $this->options['restrict_roles'],
);
$form['roles'] = array(
'#type' => 'checkboxes',
'#title' => t('Restrict to the selected roles'),
'#options' => array_map('check_plain', user_roles(TRUE)),
'#default_value' => $this->options['roles'],
'#description' => t('If no roles are selected, users from any role will be allowed.'),
'#states' => array(
'visible' => array(
':input[name="options[validate][options][user][restrict_roles]"]' => array(
'checked' => TRUE,
),
),
),
);
}
public function submitOptionsForm(&$form, &$form_state, &$options = array()) {
// filter trash out of the options so we don't store giant unnecessary arrays
$options['roles'] = array_filter($options['roles']);
}
function validate_argument($argument) {
$type = $this->options['type'];
// is_numeric() can return false positives, so we ensure it's an integer.
// However, is_integer() will always fail, since $argument is a string.
if (is_numeric($argument) && $argument == (int) $argument) {
if ($type == 'uid' || $type == 'either') {
if ($argument == $GLOBALS['user']->uid) {
// If you assign an object to a variable in PHP, the variable
// automatically acts as a reference, not a copy, so we use
// clone to ensure that we don't actually mess with the
// real global $user object.
$account = clone $GLOBALS['user'];
}
$condition = 'uid';
}
}
else {
if ($type == 'name' || $type == 'either') {
$name = !empty($GLOBALS['user']->name) ? $GLOBALS['user']->name : config('user.settings')
->get('anonymous');
if ($argument == $name) {
$account = clone $GLOBALS['user'];
}
$condition = 'name';
}
}
// If we don't have a WHERE clause, the argument is invalid.
if (empty($condition)) {
return FALSE;
}
if (!isset($account)) {
$account = db_select('users', 'u')
->fields('u', array(
'uid',
'name',
))
->condition($condition, $argument)
->execute()
->fetchObject();
}
if (empty($account)) {
// User not found.
return FALSE;
}
// See if we're filtering users based on roles.
if (!empty($this->options['restrict_roles']) && !empty($this->options['roles'])) {
$roles = $this->options['roles'];
$account->roles = array();
$account->roles[] = $account->uid ? DRUPAL_AUTHENTICATED_RID : DRUPAL_ANONYMOUS_RID;
$query = db_select('users_roles', 'u');
$query
->addField('u', 'rid');
$query
->condition('u.uid', $account->uid);
$result = $query
->execute();
foreach ($result as $role) {
$account->roles[] = $role->rid;
}
if (!(bool) array_intersect($account->roles, $roles)) {
return FALSE;
}
}
$this->argument->argument = $account->uid;
$this->argument->validated_title = check_plain(user_format_name($account));
return TRUE;
}
function process_summary_arguments(&$args) {
// If the validation says the input is an username, we should reverse the
// argument so it works for example for generation summary urls.
$uids_arg_keys = array_flip($args);
if ($this->options['type'] == 'name') {
$users = user_load_multiple($args);
foreach ($users as $uid => $account) {
$args[$uids_arg_keys[$uid]] = $account->name;
}
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ArgumentValidatorPluginBase:: |
public | function | Determine if the administrator has the privileges to use this plugin | 1 |
ArgumentValidatorPluginBase:: |
function | If we don't have access to the form but are showing it anyway, ensure that the form is safe and cannot be changed from user input. | ||
ArgumentValidatorPluginBase:: |
public | function | Initialize this plugin with the view and the argument it is linked to. | 1 |
ArgumentValidatorPluginBase:: |
public | function |
Provide the default form form for validating options Overrides PluginBase:: |
|
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
public | property | Plugins's definition | |
PluginBase:: |
public | property | The display object this plugin is for. | |
PluginBase:: |
public | property | Options for this plugin will be held here. | |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
protected | property | Denotes whether the plugin has an additional options form. | 8 |
PluginBase:: |
public | property | The top object of a view. | 1 |
PluginBase:: |
public | function | Provide a list of additional theme functions for the theme information page | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function | Clears a plugin. | 2 |
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginBase:: |
public | function | Return the human readable name of the display. | |
PluginBase:: |
public | function | Add anything to the query that we might need to. | 13 |
PluginBase:: |
protected | function | ||
PluginBase:: |
public | function | Returns the summary of the settings in the display. | 6 |
PluginBase:: |
public | function | Provide a full list of possible theme templates used by this style. | 1 |
PluginBase:: |
public | function | Unpack options over our existing defaults, drilling down into arrays so that defaults don't get totally blown away. | |
PluginBase:: |
public | function | Returns the usesOptions property. | 8 |
PluginBase:: |
public | function | Validate that the plugin is correct and can be saved. | 4 |
PluginBase:: |
public | function |
Constructs a Plugin object. Overrides PluginBase:: |
2 |
User:: |
public | function |
Provide the default form for setting options. Overrides ArgumentValidatorPluginBase:: |
|
User:: |
protected | function |
Retrieve the options when this is a new access
control plugin Overrides ArgumentValidatorPluginBase:: |
|
User:: |
function |
Process the summary arguments for displaying. Overrides ArgumentValidatorPluginBase:: |
||
User:: |
public | function |
Provide the default form form for submitting options Overrides ArgumentValidatorPluginBase:: |
|
User:: |
function |
Overrides ArgumentValidatorPluginBase:: |