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 387
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()]));
      }
    }
  }
}
doc_Drupal
2016-10-29 09:53:22
Comments
Leave a Comment

Please login to continue.