diff --git a/core/modules/user/tests/src/Functional/UserPictureTest.php b/core/modules/user/tests/src/Functional/UserPictureTest.php index 7730889b24..3598196617 100644 --- a/core/modules/user/tests/src/Functional/UserPictureTest.php +++ b/core/modules/user/tests/src/Functional/UserPictureTest.php @@ -43,14 +43,6 @@ class UserPictureTest extends BrowserTestBase { */ protected $webUser; - /** - * {@inheritdoc} - */ - protected static $modules = ['image_module_test']; - - /** - * {@inheritdoc} - */ protected function setUp(): void { parent::setUp(); @@ -71,7 +63,7 @@ protected function setUp(): void { /** * Tests creation, display, and deletion of user pictures. */ - public function testCreateDeletePicture() { + public function _testCreateDeletePicture() { $this->drupalLogin($this->webUser); // Save a new picture. @@ -109,7 +101,7 @@ public function testCreateDeletePicture() { /** * Tests embedded users on node pages. */ - public function testPictureOnNodeComment() { + public function _testPictureOnNodeComment() { $this->drupalLogin($this->webUser); // Save a new picture. @@ -169,9 +161,12 @@ public function saveUserPicture($image) { } /** - * Tests user_user_view_alter() with a dummy image formatter for user picture. + * Tests user picture field with a dummy image formatter for user picture. + * + * @see user_user_view_alter() */ public function testUserViewAlter() { + \Drupal::service('module_installer')->install(['image_module_test']); // Set dummy_image_formatter to the default view mode of user entity. EntityViewDisplay::load('user.user.default')->setComponent('user_picture', [ 'region' => 'content', diff --git a/core/modules/user/user.module b/core/modules/user/user.module index d6139494ef..b882c839c4 100644 --- a/core/modules/user/user.module +++ b/core/modules/user/user.module @@ -19,6 +19,7 @@ use Drupal\Core\Session\AnonymousUserSession; use Drupal\Core\Site\Settings; use Drupal\Core\Url; +use Drupal\image\Plugin\Field\FieldType\ImageItem; use Drupal\system\Entity\Action; use Drupal\user\Entity\Role; use Drupal\user\Entity\User; @@ -317,14 +318,16 @@ function user_user_view(array &$build, UserInterface $account, EntityViewDisplay * accessibility. */ function user_user_view_alter(array &$build, UserInterface $account, EntityViewDisplayInterface $display) { - if (isset($build['user_picture']) && !empty($build['user_picture']) && user_picture_enabled()) { + if (!empty($build['user_picture']) && user_picture_enabled()) { foreach (Element::children($build['user_picture']) as $key) { - if (!isset($build['user_picture'][$key]['#item'])) { + if (!isset($build['user_picture'][$key]['#item']) || !($build['user_picture'][$key]['#item'] instanceof ImageItem)) { + // User picture field is provided by standard profile install, skip + // if the render using non-image field item which may miss alt property. continue; } - /** @var \Drupal\Core\Field\FieldItemInterface $item */ + /** @var \Drupal\image\Plugin\Field\FieldType\ImageItem $item */ $item = $build['user_picture'][$key]['#item']; - if ($item->get('alt') && !$item->get('alt')->getValue()) { + if (!$item->get('alt')->getValue()) { $item->get('alt')->setValue(\Drupal::translation()->translate('Profile picture for user @username', ['@username' => $account->getAccountName()])); } }