Quantcast
Channel: Issues for Drupal core
Viewing all articles
Browse latest Browse all 293104

Notice: Undefined index: #item in user_user_view_alter()

$
0
0

Problem/Motivation

Notice: Undefined index: #item in user_user_view_alter() (line 409 of /Users/jungle/repo/drupal/web/core/modules/user/user.module) #0 

The line 409 of the file core/modules/user/user.module:

    $item = $build['user_picture'][$key]['#item'];

The background is that I was Implementing the avatar_field_formatter module, found that the #item in user_user_view_alter() dose not exist. As a custom image formatter, what the module does is if the image is empty, display an avatar letter generated instead.

The user_user_view_alter implementation in core assuming that the returned non-empty render array of user picture's field formatter always has #item key(s). Considering another use case, for an empty user picture, one wants to display a plain text: "No avatar" by using a custom field formatter. It may throw the same error too.

The whole function relevant:


/**
 * Implements hook_ENTITY_TYPE_view_alter() for user entities.
 *
 * This function adds a default alt tag to the user_picture field to maintain
 * accessibility.
 */
function user_user_view_alter(array &$build, UserInterface $account, EntityViewDisplayInterface $display) {
  if (user_picture_enabled() && !empty($build['user_picture'])) {
    foreach (Element::children($build['user_picture']) as $key) {
      $item = $build['user_picture'][$key]['#item'];
      if (!$item->get('alt')->getValue()) {
        $item->get('alt')->setValue(\Drupal::translation()->translate('Profile picture for user @username', ['@username' => $account->getAccountName()]));
      }
    }
  }
}

Steps to reproduce

  1. Apply patch: 3072305-11-test-only.patch in #11
  2. Add $settings['extension_discovery_scan_tests'] = TRUE; to settings.php
  3. Install Image test module (module machine name: image_module_test)
  4. Login as user 1
  5. Visit /admin/config/people/accounts/display
  6. Select Dummy image formatter for Picture field
  7. Visit /user/1, 500 error, reproduced.

Proposed resolution

Check the existence of #item first, and make sure it's an instance of ImageItem for further processing

+++ b/core/modules/user/user.module
@@ -416,9 +417,14 @@ function user_user_view(array &$build, UserInterface $account, EntityViewDisplay
     foreach (Element::children($build['user_picture']) as $key) {
+      if (!isset( $build['user_picture'][$key]['#item'])) {
+        continue;
+      }
       $item = $build['user_picture'][$key]['#item'];
-      if (!$item->get('alt')->getValue()) {
-        $item->get('alt')->setValue(\Drupal::translation()->translate('Profile picture for user @username', ['@username' => $account->getAccountName()]));
+      if ($item instanceof ImageItem) {
+        if (!$item->get('alt')->getValue()) {
+          $item->get('alt')->setValue(\Drupal::translation()->translate('Profile picture for user @username', ['@username' => $account->getAccountName()]));
+        }
       }

Remaining tasks

Needs review

How to test the patch manually

  1. Apply patch: 3072305-11.patch in #11
  2. Add $settings['extension_discovery_scan_tests'] = TRUE; to settings.php
  3. Install Image test module (module machine name: image_module_test)
  4. Login as user 1
  5. Visit /admin/config/people/accounts/display
  6. Select Dummy image formatter for Picture field
  7. Visit /user/1, you should see text Dummy which provides by the Dummy image formatter in Image test module.

User interface changes

No

API changes

No

Data model changes

No


Viewing all articles
Browse latest Browse all 293104

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>