Skip to content
Snippets Groups Projects
Commit 612c7e2c authored by catch's avatar catch
Browse files

Issue #1845526 by dawehner: User data service handler for views.

parent 4ad272c1
Branches
Tags
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
<?php
/**
* @file
* Contains \Drupal\user\Plugin\views\field\UserData.
*/
namespace Drupal\user\Plugin\views\field;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\ViewExecutable;
use Drupal\user\UserDataInterface;
use Drupal\Core\Annotation\Plugin;
/**
* Provides access to the user data service.
*
* @ingroup views_field_handlers
*
* @see \Drupal\user\UserDataInterface
*
* @Plugin(
* id = "user_data",
* module = "user"
* )
*/
class UserData extends FieldPluginBase {
/**
* Provides the user data service object.
*
* @var \Drupal\user\UserDataInterface
*/
protected $userData;
/**
* Overrides \Drupal\views\Plugin\views\field\FieldPluginBase::init().
*/
public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
parent::init($view, $display, $options);
$this->userData = drupal_container()->get('user.data');
}
/**
* Overrides \Drupal\views\Plugin\views\field\FieldPluginBase::defineOptions().
*/
protected function defineOptions() {
$options = parent::defineOptions();
$options['module'] = array('default' => '');
$options['name'] = array('default' => '');
return $options;
}
/**
* Overrides \Drupal\views\Plugin\views\field\FieldPluginBase::defineOptions().
*/
public function buildOptionsForm(&$form, &$form_state) {
parent::buildOptionsForm($form, $form_state);
$form['module'] = array(
'#title' => t('Module name'),
'#type' => 'select',
'#description' => t('The module which sets this user data.'),
'#default_value' => $this->options['module'],
'#options' => system_get_module_info('name'),
);
$form['name'] = array(
'#title' => t('Name'),
'#type' => 'textfield',
'#description' => t('The name of the data key.'),
'#default_value' => $this->options['name'],
);
}
/**
* Overrides \Drupal\views\Plugin\views\field\FieldPluginBase::render().
*/
function render($values) {
$uid = $this->get_value($values);
$data = $this->userData->get($this->options['module'], $uid, $this->options['name']);
// Don't sanitize if no value was found.
if (isset($data)) {
return $this->sanitizeValue($data);
}
}
}
<?php
/**
* @file
* Contains \Drupal\user\Tests\Views\UserDataTest.
*/
namespace Drupal\user\Tests\Views;
/**
* Tests the user data service field handler.
*
* @see \Drupal\user\Plugin\views\field\UserData
*/
class UserDataTest extends UserTestBase {
/**
* Provides the user data service object.
*
* @var \Drupal\user\UserDataInterface
*/
protected $userData;
/**
* Views used by this test.
*
* @var array
*/
public static $testViews = array('test_user_data');
public static function getInfo() {
return array(
'name' => 'User data: Field',
'description' => 'Tests the user data service field handler.',
'group' => 'Views module integration',
);
}
/**
* Tests field handler.
*/
public function testDataField() {
// But some random values into the user data service.
$this->userData = $this->container->get('user.data');
$random_value = $this->randomName();
$this->userData->set('views_test_config', $this->users[0]->id(), 'test_value_name', $random_value);
$view = views_get_view('test_user_data');
$this->executeView($view);
$output = $view->field['data']->render($view->result[0]);
$this->assertEqual($output, $random_value, 'A valid user data got rendered.');
$view->field['data']->options['name'] = $this->randomName();
$output = $view->field['data']->render($view->result[0]);
$this->assertFalse($output, 'An invalid configuration does not return anything');
}
}
base_field: uid
base_table: users
core: 8.x
description: ''
status: '1'
display:
default:
display_plugin: default
id: default
display_title: Master
position: ''
display_options:
access:
type: perm
perm: 'access user profiles'
cache:
type: none
query:
type: views_query
exposed_form:
type: basic
pager:
type: full
style:
type: default
row:
type: fields
fields:
name:
id: name
table: users
field: name
label: ''
alter:
alter_text: '0'
make_link: '0'
absolute: '0'
trim: '0'
word_boundary: '0'
ellipsis: '0'
strip_tags: '0'
html: '0'
hide_empty: '0'
empty_zero: '0'
link_to_user: '1'
overwrite_anonymous: '0'
plugin_id: string
data:
id: data
table: users
field: data
relationship: none
group_type: group
admin_label: ''
label: Data
exclude: '0'
alter:
alter_text: '0'
text: ''
make_link: '0'
path: ''
absolute: '0'
external: '0'
replace_spaces: '0'
path_case: none
trim_whitespace: '0'
alt: ''
rel: ''
link_class: ''
prefix: ''
suffix: ''
target: ''
nl2br: '0'
max_length: ''
word_boundary: '1'
ellipsis: '1'
more_link: '0'
more_link_text: ''
more_link_path: ''
strip_tags: '0'
trim: '0'
preserve_tags: ''
html: '0'
element_type: ''
element_class: ''
element_label_type: ''
element_label_class: ''
element_label_colon: '1'
element_wrapper_type: ''
element_wrapper_class: ''
element_default_classes: '1'
empty: ''
hide_empty: '0'
empty_zero: '0'
hide_alter_empty: '1'
module: views_test_config
name: test_value_name
plugin_id: user_data
filters:
uid:
value:
0: '2'
table: users
field: uid
id: uid
expose:
operator: '0'
group: '1'
plugin_id: numeric
sorts:
created:
id: created
table: users
field: created
order: DESC
plugin_id: date
human_name: test_user_data
module: views
id: test_user_data
tag: ''
......@@ -324,6 +324,15 @@ function user_views_data() {
),
);
$data['users']['data'] = array(
'title' => t('Data'),
'help' => t('Provides access to the user data service.'),
'real field' => 'uid',
'field' => array(
'id' => 'user_data',
),
);
// users_roles table
$data['users_roles']['table']['group'] = t('User');
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment