Add test that user_user_view_alter works with different formatters

Created on 7 September 2017, over 7 years ago
Updated 17 April 2023, over 1 year ago

When users upload a user picture the user profile page under /user/uid is causing a WSOD. By enabling Devel and error handling output it shows the follwong error message:

The website encountered an unexpected error. Please try again later.
Error: Call to a member function get() on null in user_user_view_alter() (line 404 of core/modules/user/user.module).

Regarding the referenced code part of user.module, the following function is mentioned in the error message:

394 /**
395  * Implements hook_ENTITY_TYPE_view_alter() for user entities.
396  *
397  * This function adds a default alt tag to the user_picture field to maintain
398  * accessibility.
399 */
400 function user_user_view_alter(array &$build, UserInterface $account, EntityViewDisplayInterface $display) {
401  if (user_picture_enabled() && !empty($build['user_picture'])) {
402    foreach (Element::children($build['user_picture']) as $key) {
403     $item = $build['user_picture'][$key]['#item'];
404      if (!$item->get('alt')->getValue()) {
405       $item->get('alt')->setValue(\Drupal::translation()->translate('Profile picture for user @username', ['@username' => $account->getUsername()]));
406      }
407    }
408  }
409 }

So maybe we can't !check against 'alt' if it doesn't exist? Maybe we need !isset?

var_dump($item->get('alt')->getValue()); exit(); creates the same error ... Maybe the controller's registry object needs to have the get() method. We probably don't have the correct registry obj passed into the controller constructor here? Still investigating ... (stack trace coming soon...)

Removing the uploaded user picture from the profile and loading the user page again, removes the issue and returns to normal view, the user profile is shown normally. Uploading another user picture again, is causing the error again.

Playing around with alt / title fields filled / unfilled, activated / deactivated makes no difference on this issue.

Tested system:
Fresh installed Drupal 8.3 on Debian 9 with PHP 5 and MariaDB
Additional Installed modules: entity, token, rules, file_entity, devel, paragraphs, ctools, redirect
User picture field settings have been altered to support up to 100kb files and max 800x800px.


EDIT (stack trace added):

user_user_view_alter(Array, Object, Object) (Line: 501)
Drupal\Core\Extension\ModuleHandler->alter('user_view', Array, Object, Object) (Line: 285)
Drupal\Core\Entity\EntityViewBuilder->buildMultiple(Array) (Line: 220)
Drupal\Core\Entity\EntityViewBuilder->build(Array)
call_user_func(Array, Array) (Line: 376)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 195)
Drupal\Core\Render\Renderer->render(Array, ) (Line: 226)
Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}() (Line: 574)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 227)
Drupal\Core\Render\MainContent\HtmlRenderer->prepare(Array, Object, Object) (Line: 117)
Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object, Object) (Line: 90)
Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object, 'kernel.view', Object) (Line: 111)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch('kernel.view', Object) (Line: 149)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 64)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 99)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 78)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 50)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 656)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

Without an added user picture the error disappears but the back trace still looks at least like this (maybe it reveals the underlying issue?):

    Notice: Undefined index: #item in user_user_view_alter() (line 403 of core/modules/user/user.module).

    user_user_view_alter(Array, Object, Object) (Line: 501)
    Drupal\Core\Extension\ModuleHandler->alter('user_view', Array, Object, Object) (Line: 285)
    Drupal\Core\Entity\EntityViewBuilder->buildMultiple(Array) (Line: 220)
    Drupal\Core\Entity\EntityViewBuilder->build(Array)
    call_user_func(Array, Array) (Line: 376)
    Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 195)
    Drupal\Core\Render\Renderer->render(Array, ) (Line: 226)
    Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}() (Line: 574)
    Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 227)
    Drupal\Core\Render\MainContent\HtmlRenderer->prepare(Array, Object, Object) (Line: 117)
    Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object, Object) (Line: 90)
    Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object, 'kernel.view', Object) (Line: 111)
    Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch('kernel.view', Object) (Line: 149)
    Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 64)
    Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57)
    Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47)
    Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 99)
    Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 78)
    Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 47)
    Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 50)
    Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
    Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 656)
    Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

    Notice: Undefined index: #item in user_user_view_alter() (line 403 of core/modules/user/user.module).

    user_user_view_alter(Array, Object, Object) (Line: 501)
    Drupal\Core\Extension\ModuleHandler->alter('user_view', Array, Object, Object) (Line: 285)
    Drupal\Core\Entity\EntityViewBuilder->buildMultiple(Array) (Line: 220)
    Drupal\Core\Entity\EntityViewBuilder->build(Array)
    call_user_func(Array, Array) (Line: 376)
    Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 195)
    Drupal\Core\Render\Renderer->render(Array, ) (Line: 226)
    Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}() (Line: 574)
    Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 227)
    Drupal\Core\Render\MainContent\HtmlRenderer->prepare(Array, Object, Object) (Line: 117)
    Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object, Object) (Line: 90)
    Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object, 'kernel.view', Object) (Line: 111)
    Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch('kernel.view', Object) (Line: 149)
    Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 64)
    Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57)
    Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47)
    Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 99)
    Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 78)
    Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 47)
    Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 50)
    Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
    Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 656)
    Drupal\Core\DrupalKernel->handle(Object) (Line: 19)
📌 Task
Status

Needs work

Version

10.1

Component
User module 

Last updated 7 days ago

Created by

🇫🇷France dqd London | N.Y.C | Paris | Hamburg | Berlin

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • The Needs Review Queue Bot tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

    Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

    Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

  • 🇮🇳India nikhil_110

    Attached patch against Drupal 10.1.x

    Patch #56 is not applied for Drupal 10.1.x so Inter-diff file is not added.

    Checking patch core/modules/user/tests/src/Functional/UserPictureTest.php...
    Hunk #1 succeeded at 59 (offset 4 lines).
    error: while searching for:
    $elements = $this->cssSelect('.comment__meta .field--name-user-picture img[alt="' . $alt_text . '"][src="' . $image_url . '"]');
    $this->assertCount(1, $elements, 'User picture with alt text found on the comment.');

    // Disable user pictures on comments and nodes.
    $this->config('system.theme.global')
    ->set('features.node_user_picture', FALSE)

    error: patch failed: core/modules/user/tests/src/Functional/UserPictureTest.php:138
    error: core/modules/user/tests/src/Functional/UserPictureTest.php: patch does not apply

  • Open in Jenkins → Open on Drupal.org →
    Environment: PHP 8.1 & MySQL 5.7
    51:13
    47:53
    Running
Production build 0.71.5 2024