Issue #3072305 by jungle, andypost, thursday_bw, longwave, larowlan: Notice: Undefined index: #item in user_user_view_alter()
parent
8350f688ea
commit
aece6314d8
core/modules
image/tests/modules/image_module_test/src/Plugin/Field/FieldFormatter
user
tests/src/Functional
|
@ -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'],
|
||||
];
|
||||
}
|
||||
|
||||
}
|
|
@ -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 @@ class UserPictureTest extends BrowserTestBase {
|
|||
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\AccountInterface;
|
|||
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()]));
|
||||
|
|
Loading…
Reference in New Issue