From aece6314d84f6c287becaded039037538cc68311 Mon Sep 17 00:00:00 2001 From: Lee Rowlands <lee.rowlands@previousnext.com.au> Date: Fri, 19 Jun 2020 06:40:00 +1000 Subject: [PATCH] Issue #3072305 by jungle, andypost, thursday_bw, longwave, larowlan: Notice: Undefined index: #item in user_user_view_alter() --- .../FieldFormatter/DummyImageFormatter.php | 33 +++++++++++++++++++ .../tests/src/Functional/UserPictureTest.php | 18 ++++++++++ core/modules/user/user.module | 10 +++++- 3 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 core/modules/image/tests/modules/image_module_test/src/Plugin/Field/FieldFormatter/DummyImageFormatter.php diff --git a/core/modules/image/tests/modules/image_module_test/src/Plugin/Field/FieldFormatter/DummyImageFormatter.php b/core/modules/image/tests/modules/image_module_test/src/Plugin/Field/FieldFormatter/DummyImageFormatter.php new file mode 100644 index 000000000000..43a6860bd6bd --- /dev/null +++ b/core/modules/image/tests/modules/image_module_test/src/Plugin/Field/FieldFormatter/DummyImageFormatter.php @@ -0,0 +1,33 @@ +<?php + +namespace Drupal\image_module_test\Plugin\Field\FieldFormatter; + +use Drupal\Core\Field\FieldItemListInterface; +use Drupal\Core\Field\FormatterBase; + +/** + * Plugin implementation of the Dummy image formatter. + * + * @FieldFormatter( + * id = "dummy_image_formatter", + * label = @Translation("Dummy image"), + * field_types = { + * "image" + * }, + * quickedit = { + * "editor" = "image" + * } + * ) + */ +class DummyImageFormatter extends FormatterBase { + + /** + * {@inheritdoc} + */ + public function viewElements(FieldItemListInterface $items, $langcode) { + return [ + ['#markup' => 'Dummy'], + ]; + } + +} diff --git a/core/modules/user/tests/src/Functional/UserPictureTest.php b/core/modules/user/tests/src/Functional/UserPictureTest.php index b7fd36408a32..e00160809077 100644 --- a/core/modules/user/tests/src/Functional/UserPictureTest.php +++ b/core/modules/user/tests/src/Functional/UserPictureTest.php @@ -3,6 +3,7 @@ namespace Drupal\Tests\user\Functional; use Drupal\Core\Database\Database; +use Drupal\Core\Entity\Entity\EntityViewDisplay; use Drupal\Core\StreamWrapper\StreamWrapperManager; use Drupal\file\Entity\File; use Drupal\image\Entity\ImageStyle; @@ -159,4 +160,21 @@ public function saveUserPicture($image) { return File::load($account->user_picture->target_id); } + /** + * Tests user picture field with a non-standard field formatter. + * + * @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', + 'type' => 'dummy_image_formatter', + ])->save(); + $this->drupalLogin($this->webUser); + $this->assertSession()->statusCodeEquals(200); + $this->assertSession()->pageTextContains('Dummy'); + } + } diff --git a/core/modules/user/user.module b/core/modules/user/user.module index 5b8432c6b382..1130e322f459 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,8 +318,15 @@ function user_user_view(array &$build, UserInterface $account, EntityViewDisplay * accessibility. */ function user_user_view_alter(array &$build, UserInterface $account, EntityViewDisplayInterface $display) { - if (user_picture_enabled() && !empty($build['user_picture'])) { + if (!empty($build['user_picture']) && user_picture_enabled()) { foreach (Element::children($build['user_picture']) as $key) { + if (!isset($build['user_picture'][$key]['#item']) || !($build['user_picture'][$key]['#item'] instanceof ImageItem)) { + // User picture field is provided by standard profile install. If the + // display is configured to use a different formatter, the #item render + // key may not exist, or may not be an image field. + continue; + } + /** @var \Drupal\image\Plugin\Field\FieldType\ImageItem $item */ $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()])); -- GitLab