Verified Commit e6a1e87d authored by Lee Rowlands's avatar Lee Rowlands
Browse files

Issue #3072305 by jungle, andypost, thursday_bw, longwave, larowlan: Notice:...

Issue #3072305 by jungle, andypost, thursday_bw, longwave, larowlan: Notice: Undefined index: #item in user_user_view_alter()

(cherry picked from commit aece6314)
parent c26cb4aa
Loading
Loading
Loading
Loading
+33 −0
Original line number Diff line number Diff line
<?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'],
    ];
  }

}
+18 −0
Original line number Diff line number Diff line
@@ -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');
  }

}
+9 −1
Original line number Diff line number Diff line
@@ -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;
@@ -414,8 +415,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()]));