Skip to content
Snippets Groups Projects
Verified Commit aece6314 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()
parent 8350f688
No related branches found
No related tags found
8 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!1012Issue #3226887: Hreflang on non-canonical content pages,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10,!596Issue #3046532: deleting an entity reference field, used in a contextual view, makes the whole site unrecoverable,!496Issue #2463967: Use .user.ini file for PHP settings,!144Issue #2666286: Clean up menu_ui to conform to Drupal coding standards,!16Draft: Resolve #2081585 "History storage",!13Resolve #2903456
<?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'],
];
}
}
......@@ -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');
}
}
......@@ -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()]));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment