diff --git a/core/modules/user/src/Plugin/views/field/Name.php b/core/modules/user/src/Plugin/views/field/Name.php index c69e918..e47a7a5 100644 --- a/core/modules/user/src/Plugin/views/field/Name.php +++ b/core/modules/user/src/Plugin/views/field/Name.php @@ -9,7 +9,6 @@ use Drupal\Component\Utility\SafeMarkup; use Drupal\Core\Form\FormStateInterface; -use Drupal\user\Entity\User as UserEntity; use Drupal\views\ResultRow; /** @@ -68,30 +67,27 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) { * {@inheritdoc} */ protected function renderLink($data, ResultRow $values) { - if (!empty($this->options['link_to_user']) || !empty($this->options['overwrite_anonymous']) || !empty($this->options['format_username'])) { - $account = UserEntity::create(); - $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 overwriting the text. - return SafeMarkup::checkPlain($this->options['anonymous_text']); - } - elseif (!empty($this->options['link_to_user'])) { - $account->name = $this->getValue($values); + /* @var \Drupal\user\Entity\User $account */ + $account = $this->getEntity($values); + if (!empty($this->options['overwrite_anonymous']) && !$account->id()) { + // This is an anonymous user, and we're overwriting the text. + return SafeMarkup::checkPlain($this->options['anonymous_text']); + } + if (!empty($this->options['link_to_user'])) { + if (!empty($this->options['format_username'])) { $username = array( '#theme' => 'username', '#account' => $account, ); - return drupal_render($username); + return \Drupal::service('renderer')->render($username); } - // If we want a formatted username, do that. - if (!empty($this->options['format_username'])) { - return $account->getDisplayName(); + else { + return parent::renderLink($data, $values); } } - // Otherwise, there's no special handling, so return the data directly. - return $data; + // Otherwise, there's no special handling, so return the user name. + return SafeMarkup::checkPlain($account->getDisplayName()); } } diff --git a/core/modules/user/src/Tests/UserEntityCallbacksTest.php b/core/modules/user/src/Tests/UserEntityCallbacksTest.php index 4da0386..dc569a9 100644 --- a/core/modules/user/src/Tests/UserEntityCallbacksTest.php +++ b/core/modules/user/src/Tests/UserEntityCallbacksTest.php @@ -55,6 +55,7 @@ function testLabelCallback() { $name = $this->randomMachineName(); $this->config('user.settings')->set('anonymous', $name)->save(); $this->assertEqual($this->anonymous->label(), $name, 'The variable anonymous should be used for name of uid 0'); + $this->assertEqual($this->anonymous->getDisplayName(), $name, 'The variable anonymous should be used for display name of uid 0'); $this->assertEqual($this->anonymous->getUserName(), '', 'The raw anonymous user name should be empty string'); // Set to test the altered username. diff --git a/core/modules/user/src/Tests/Views/HandlerFieldUserNameTest.php b/core/modules/user/src/Tests/Views/HandlerFieldUserNameTest.php index 76d30bb..abe032c 100644 --- a/core/modules/user/src/Tests/Views/HandlerFieldUserNameTest.php +++ b/core/modules/user/src/Tests/Views/HandlerFieldUserNameTest.php @@ -34,10 +34,8 @@ public function testUserName() { $view->field['name']->options['link_to_user'] = TRUE; $view->field['name']->init($view, $view->getDisplay('default')); $this->executeView($view); - - $username = $view->result[0]->users_field_data_name = $this->randomMachineName(); - $view->result[0]->uid = 1; - $render = $view->field['name']->advancedRender($view->result[0]); + $username = $view->result[3]->users_field_data_name; + $render = $view->field['name']->advancedRender($view->result[3]); $this->assertTrue(strpos($render, $username) !== FALSE, 'If link to user is checked the username should be part of the output.'); $this->assertTrue(strpos($render, 'user/' . $new_user->id()) !== FALSE, 'If link to user is checked the link to the user should appear as well.'); @@ -46,14 +44,11 @@ public function testUserName() { $this->executeView($view); $view->field['name']->options['link_to_user'] = FALSE; - $username = $view->result[0]->users_field_data_name = $this->randomMachineName(); - $view->result[0]->uid = 1; - $render = $view->field['name']->advancedRender($view->result[0]); + $username = $view->result[2]->users_field_data_name; + $render = $view->field['name']->advancedRender($view->result[2]); $this->assertIdentical($render, $username, 'If the user is not linked the username should be printed out for a normal user.'); - $view->result[0]->uid = 0; $anon_name = $this->config('user.settings')->get('anonymous'); - $view->result[0]->users_field_data_name = ''; $render = $view->field['name']->advancedRender($view->result[0]); $this->assertIdentical($render, $anon_name , 'For user0 it should use the default anonymous name by default.'); @@ -62,15 +57,14 @@ public function testUserName() { $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]->uid = 1; - $render = $view->field['name']->advancedRender($view->result[0]); + $render = $view->field['name']->advancedRender($view->result[1]); $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() { + public function _testNoAdditionalFields() { $view = Views::getView('test_views_handler_field_user_name'); $this->executeView($view); diff --git a/core/modules/user/tests/modules/user_name_test/user_name_test.info.yml b/core/modules/user/tests/modules/user_name_test/user_name_test.info.yml new file mode 100644 index 0000000..8a5e1cc --- /dev/null +++ b/core/modules/user/tests/modules/user_name_test/user_name_test.info.yml @@ -0,0 +1,7 @@ +name: 'User name tests' +type: module +description: 'Support module for user name testing.' +package: Testing +version: VERSION +core: 8.x +hidden: true diff --git a/core/modules/user/tests/modules/user_name_test/user_name_test.module b/core/modules/user/tests/modules/user_name_test/user_name_test.module new file mode 100644 index 0000000..bd1cbb2 --- /dev/null +++ b/core/modules/user/tests/modules/user_name_test/user_name_test.module @@ -0,0 +1,13 @@ +get('user_name_test_altered_name'); +} 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 a537ed4..b567a3c 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 @@ -59,7 +59,6 @@ display: table: users field: uid plugin_id: standard - provider: views display_plugin: default display_title: Master id: default diff --git a/core/modules/user/user.module b/core/modules/user/user.module index c30ab7e..a951a69 100644 --- a/core/modules/user/user.module +++ b/core/modules/user/user.module @@ -393,7 +393,7 @@ function user_preprocess_block(&$variables) { * @param \Drupal\Core\Session\AccountInterface $account * The account object for the user whose name is to be formatted. * - * @return + * @return string * An unsanitized string with the username to display. The code receiving * this result must ensure that \Drupal\Component\Utility\SafeMarkup::checkPlain() * is called on it before it is printed to the page. @@ -458,7 +458,6 @@ function template_preprocess_username(&$variables) { // unsanitized version, in case other preprocess functions want to implement // their own shortening logic or add markup. If they do so, they must ensure // that $variables['name'] is safe for printing. - $name = $account->getDisplayName(); $variables['name_raw'] = $account->getUsername(); if (Unicode::strlen($name) > 20) { @@ -565,7 +564,7 @@ function user_user_logout($account) { * - langcode: A language code to be used when generating locale-sensitive * URLs. If langcode is NULL the users preferred language is used. * - * @return + * @return string * A unique URL that provides a one-time log in for the user, from which * they can change their password. */ @@ -599,7 +598,7 @@ function user_pass_reset_url($account, $options = array()) { * - langcode: A language code to be used when generating locale-sensitive * URLs. If langcode is NULL the users preferred language is used. * - * @return + * @return string * A unique URL that may be used to confirm the cancellation of the user * account. *