function user_user_view_alter

user_user_view_alter(array &$build, UserInterface $account, EntityViewDisplayInterface $display)

Implements hook_ENTITY_TYPE_view_alter() for user entities.

This function adds a default alt tag to the user_picture field to maintain accessibility.

File

core/modules/user/user.module, line 385
Enables the user registration and login system.

Code

function user_user_view_alter(array &$build, UserInterface $account, EntityViewDisplayInterface $display) {
  if (user_picture_enabled() && !empty($build['user_picture'])) {
    foreach (Element::children($build['user_picture']) as $key) {
      $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->getUsername()]));
      }
    }
  }
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!modules!user!user.module/function/user_user_view_alter/8.1.x