function hook_user_view_alter

You are here

7 user.api.php hook_user_view_alter(&$build)
8 user.api.php hook_user_view_alter(&$build, \Drupal\user\UserInterface $account, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display)

The user was built; the module may modify the structured content.

This hook is called after the content has been assembled in a structured array and may be used for doing processing which requires that the complete user content structure has been built.

If the module wishes to act on the rendered HTML of the user rather than the structured content array, it may use this hook to add a #post_render callback. Alternatively, it could also implement hook_preprocess_user_profile(). See drupal_render() and theme() documentation respectively for details.

Parameters

$build: A renderable array representing the user.

See also

user_view()

hook_entity_view_alter()

Related topics

File

modules/user/user.api.php, line 377
Hooks provided by the User module.

Code

function hook_user_view_alter(&$build) {
  // Check for the existence of a field added by another module.
  if (isset($build['an_additional_field'])) {
    // Change its weight.
    $build['an_additional_field']['#weight'] = -10;
  }

  // Add a #post_render callback to act on the rendered HTML of the user.
  $build['#post_render'][] = 'my_module_user_post_render';
}

Comments

Note that for some values of $build['#view_mode'] such as 'title' (called "Title only" in the Views UI) there is no #post_render processing. So if, for example, you need to customize the <description> value in an RSS feed, you will need to use 'Full content', 'RSS', or 'Teaser' for the Content formatting.