summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Pott2015-02-02 12:27:17 +0000
committerAlex Pott2015-02-02 12:27:17 +0000
commit49001708757faaee72597a6a5b23cbecd9f30c7e (patch)
tree76e1b2520265989868daa760b7192b8f7c6aeac1
parent275de46483300156b8c3a6701222e4e4a4995d11 (diff)
Issue #2133471 by lokapujya, Lendude, jhedstrom, tadityar, webflo, whitelikeman: Notice: Undefined index: uid in simple a user view
-rw-r--r--core/modules/user/src/Plugin/views/field/Name.php17
-rw-r--r--core/modules/user/src/Tests/Views/HandlerFieldUserNameTest.php21
-rw-r--r--core/modules/user/tests/modules/user_test_views/test_views/views.view.test_views_handler_field_user_name.yml3
3 files changed, 31 insertions, 10 deletions
diff --git a/core/modules/user/src/Plugin/views/field/Name.php b/core/modules/user/src/Plugin/views/field/Name.php
index 0d5a768..cc91eea 100644
--- a/core/modules/user/src/Plugin/views/field/Name.php
+++ b/core/modules/user/src/Plugin/views/field/Name.php
@@ -80,10 +80,10 @@ class Name extends User {
* {@inheritdoc}
*/
protected function renderLink($data, ResultRow $values) {
- $account = entity_create('user');
- $account->uid = $this->getValue($values, 'uid');
- $account->name = $this->getValue($values);
- if (!empty($this->options['link_to_user']) || !empty($this->options['overwrite_anonymous'])) {
+ if (!empty($this->options['link_to_user']) || !empty($this->options['overwrite_anonymous']) || !empty($this->options['format_username'])) {
+ $account = entity_create('user');
+ $account->uid = $this->getValue($values, 'uid');
+ $account->name = $this->getValue($values);
if (!empty($this->options['overwrite_anonymous']) && !$account->id()) {
// This is an anonymous user, and we're overriting the text.
return String::checkPlain($this->options['anonymous_text']);
@@ -96,11 +96,12 @@ class Name extends User {
);
return drupal_render($username);
}
+ // If we want a formatted username, do that.
+ if (!empty($this->options['format_username'])) {
+ return user_format_name($account);
+ }
}
- // If we want a formatted username, do that.
- if (!empty($this->options['format_username'])) {
- return user_format_name($account);
- }
+
// Otherwise, there's no special handling, so return the data directly.
return $data;
}
diff --git a/core/modules/user/src/Tests/Views/HandlerFieldUserNameTest.php b/core/modules/user/src/Tests/Views/HandlerFieldUserNameTest.php
index c0067c9..4a1ca4e 100644
--- a/core/modules/user/src/Tests/Views/HandlerFieldUserNameTest.php
+++ b/core/modules/user/src/Tests/Views/HandlerFieldUserNameTest.php
@@ -27,10 +27,13 @@ class HandlerFieldUserNameTest extends UserTestBase {
public function testUserName() {
$this->drupalLogin($this->drupalCreateUser(array('access user profiles')));
+ // Set defaults.
$view = Views::getView('test_views_handler_field_user_name');
+ $view->initHandlers();
+ $view->field['name']->options['link_to_user'] = TRUE;
+ $view->field['name']->init($view, $view->getDisplay('default'));
$this->executeView($view);
- $view->field['name']->options['link_to_user'] = TRUE;
$username = $view->result[0]->users_field_data_name = $this->randomMachineName();
$view->result[0]->users_field_data_uid = 1;
$render = $view->field['name']->advancedRender($view->result[0]);
@@ -53,6 +56,22 @@ class HandlerFieldUserNameTest extends UserTestBase {
$anon_name = $view->field['name']->options['anonymous_text'] = $this->randomMachineName();
$render = $view->field['name']->advancedRender($view->result[0]);
$this->assertIdentical($render, $anon_name , 'For user0 it should use the configured anonymous text if overwrite_anonymous is checked.');
+ $view->result[0]->users_field_data_uid = 1;
+ $render = $view->field['name']->advancedRender($view->result[0]);
+ $this->assertNotIdentical($render, $anon_name , 'For registered user it should not use the configured anonymous text if overwrite_anonymous is checked.');
+ }
+
+ /**
+ * Tests that the field handler works when no additional fields are added.
+ */
+ public function testNoAdditionalFields() {
+ $view = Views::getView('test_views_handler_field_user_name');
+ $this->executeView($view);
+
+ $username = $view->result[0]->users_field_data_name = $this->randomMachineName();
+ $view->result[0]->users_field_data_uid = 1;
+ $render = $view->field['name']->advancedRender($view->result[0]);
+ $this->assertTrue(strpos($render, $username) !== FALSE, 'If link to user is checked the username should be part of the output.');
}
}
diff --git a/core/modules/user/tests/modules/user_test_views/test_views/views.view.test_views_handler_field_user_name.yml b/core/modules/user/tests/modules/user_test_views/test_views/views.view.test_views_handler_field_user_name.yml
index 9a9bac5..8f2a9ee 100644
--- a/core/modules/user/tests/modules/user_test_views/test_views/views.view.test_views_handler_field_user_name.yml
+++ b/core/modules/user/tests/modules/user_test_views/test_views/views.view.test_views_handler_field_user_name.yml
@@ -36,8 +36,9 @@ display:
hide_empty: false
id: name
label: ''
- link_to_user: true
+ link_to_user: false
overwrite_anonymous: false
+ format_username: false
table: users_field_data
plugin_id: user_name
entity_type: user