Updated: Comment #N

Problem/Motivation

Simple user views without field formatting on the username field result in the following error message:

Notice: Undefined index: uid in Drupal\views\Plugin\views\field\FieldPluginBase->getValue() (line 393 of core/modules/views/lib/Drupal/views/Plugin/views/field/FieldPluginBase.php).

To reproduce

  1. Add a user view
  2. Remove the default filter (the anonymous one)
  3. Edit the username field, and uncheck both 'Link this field to the user', and 'Use formatted username'
  4. Save the field, see the notice in the view preview

Proposed resolution

Remaining tasks

User interface changes

API changes

Beta phase evaluation

Reference: https://www.drupal.org/core/beta-changes
Issue category Bug because PHP notice
Unfrozen changes Unfrozen because it only changes the code to not throw PHP notices

Comments

dawehner’s picture

There is at least one more bug hidden:

base_field: nid
base_table: users

Thank you for providing that report!

balazswmann’s picture

Assigned: Unassigned » balazswmann

I can confirm this bug. It still exists in the 8.x branch.

balazswmann’s picture

Assigned: balazswmann » Unassigned
Status: Active » Needs review
StatusFileSize
new1.97 KB

I have attached a patch to fix the undefined index PHP notice.

jhedstrom’s picture

Status: Needs review » Needs work
Issue tags: +Needs reroll
tadityar’s picture

Status: Needs work » Needs review
StatusFileSize
new1.53 KB

Re-rolled the patch.

tadityar’s picture

I also think that

-    // If we want a formatted username, do that.
-    if (!empty($this->options['format_username'])) {
-      return user_format_name($account);
+      if (!empty($this->options['format_username'])) {
+        return user_format_name($account);

is not necessary, so I didn't include that in #5 patch.
(It only change indentation).

Status: Needs review » Needs work

The last submitted patch, 5: fix_undefined_index_uid-2133471-5.patch, failed testing.

lokapujya’s picture

That change did more than just indentation, it moved the if statement inside of the other if statement.

tadityar’s picture

Assigned: Unassigned » tadityar
Status: Needs work » Needs review
StatusFileSize
new1.95 KB

Yes that did.. didn't notice that, thanks! Re-rolling now.

Status: Needs review » Needs work

The last submitted patch, 9: fix_undefined_index_uid-2133471-9.patch, failed testing.

tadityar’s picture

Status: Needs work » Needs review
StatusFileSize
new1.95 KB

Trying again.

Status: Needs review » Needs work

The last submitted patch, 11: fix_undefined_index_uid-2133471-11.patch, failed testing.

lokapujya’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
StatusFileSize
new1.95 KB
new806 bytes

Should keep String:checkPlain().

tadityar’s picture

Assigned: tadityar » Unassigned
jhedstrom’s picture

Issue summary: View changes
Status: Needs review » Reviewed & tested by the community
StatusFileSize
new244.72 KB

I've updated the issue summary with steps to reproduce, and a beta-phase evaluation. I've confirmed the patch in #13 removes the notice. RTBC I think.

jhedstrom’s picture

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests

We should have a test for this.

jhedstrom’s picture

I spent a bit of time trying to get Drupal\user\Tests\Views\HandlerFieldUserNameTest to throw this notice, but something in how that code is being tested bypasses the PHP notice. I'll revisit if I get a chance.

jhedstrom’s picture

Here's the issue: Since the test view test_views_handler_field_usern_name has certain defaults, the various init() methods in the test are setting the additional_fields parameter, which when set, avoids the PHP notice. Manually toggling these settings during the course of a test doesn't unset those from the field handler.

  /**
   * Overrides \Drupal\views\Plugin\views\field\FieldPluginBase::init().
   */
  public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
    parent::init($view, $display, $options);

    if (!empty($this->options['link_to_user'])) {
      $this->additional_fields['uid'] = 'uid';
    }
  }

Therefore, the test for this can either change the defaults in the existing test view, or add a new test view. Changing the defaults to false (format_username, overwrite_anonymous, and link_to_user) breaks some current tests, but definitely throws the php notice.

jhedstrom’s picture

Status: Needs work » Needs review
StatusFileSize
new2.6 KB
new4.55 KB

Here's a test. As mentioned above, I needed to rework the default settings of the existing user name field handler view, thus the changes to the current test.

The last submitted patch, 20: views-undefined-index-uid-2133471-WILL-FAIL.patch, failed testing.

lokapujya’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs tests

Test looks good.

olli’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +VDC

Applied the patch, edited the people view, checked "Overwrite the value to display for anonymous users" and set "Text to display for anonymous users" to "visitor". Went to admin/people and see "visitor" instead of "admin".

lendude’s picture

Status: Needs work » Needs review
StatusFileSize
new1.07 KB
new4.36 KB
+++ b/core/modules/user/src/Plugin/views/field/Name.php
@@ -81,25 +81,23 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) {
-      if (!empty($this->options['overwrite_anonymous']) && !$account->id()) {
...
+    if (!empty($this->options['overwrite_anonymous']) && !$account->id()) {

This line....

+++ b/core/modules/user/src/Plugin/views/field/Name.php
@@ -81,25 +81,23 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) {
+      $account->uid = $this->getValue($values, 'uid');

Needs this to be set.
That leads to the problem in #23.

Updated patch in #20 to account for this.

Status: Needs review » Needs work

The last submitted patch, 24: views-undefined-index-uid-2133471-24.patch, failed testing.

lendude’s picture

Well that undid the original fix, so that doesn't help much :) Back to the drawing board.....

lendude’s picture

Status: Needs work » Needs review
StatusFileSize
new1.87 KB
new4.31 KB

Let's try this again....

jhedstrom’s picture

Given #23 I'm concerned that neither the new tests, nor existing tests, caught that. Perhaps a few more assertions are in order for better coverage around the anonymous username formatting?

lokapujya’s picture

StatusFileSize
new1.23 KB

Tried something, but it's not working. I don't get it because it looks like the test already logs in a user, but it's supposed to be testing an anonymous user.

Status: Needs review » Needs work

The last submitted patch, 29: 2133471-29_test_not_working.patch, failed testing.

lokapujya’s picture

StatusFileSize
new3.48 KB

Last patch bad. this is what i tried.

lendude’s picture

Status: Needs work » Needs review

Let's make the testbot do it's magic....

Status: Needs review » Needs work

The last submitted patch, 31: 2133471_test_not_working.patch, failed testing.

lokapujya’s picture

Status: Needs work » Needs review
StatusFileSize
new4.99 KB
new964 bytes
new4.75 KB

In the previous patch, I tried to change the logged in user before executing the view; But, I didn't need that. What WAS needed is to show that overwrite anonymous doesn't affect a regular user's name. Here is a test-only patch against #20 to show the test failure. Plus, an interdiff, and the new patch.

The last submitted patch, 34: 2133471-34-test-only.patch, failed testing.

lendude’s picture

Status: Needs review » Reviewed & tested by the community

Fixes the issue and the issues in #17, #23 and #28 are addressed.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Nice tests - thanks. Committed 4900170 and pushed to 8.0.x. Thanks!

Thanks for adding the beta evaluation to the issue summary.

  • alexpott committed 4900170 on 8.0.x
    Issue #2133471 by lokapujya, Lendude, jhedstrom, tadityar, webflo,...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.