I am trying to add a default image to render in the user account menu when the user does not have an image.
CODE:
function nestle_preprocess_menu(&$variables) {
if($variables['menu_name'] == "account") {
$user = DrupaluserEntityUser::load(Drupal::currentUser()->id());
$name = $variables['user']->getUsername();
$picture = file_create_url($file->getFileUri());
$picture = $user->user_picture->view('large');
$variables['items']['user.page']['title'] = $name;
$variables['items']['user.page']['picture'] = $picture;
}
}
What I did so far, is that I changed the “account” menu link to name of the logged in user and I added the picture of the user in the variables array so that I can render it using twig, The problem is I want to check when the $picture variable is empty ( ie: user has no photo ) it should render a default image from a URL.
PS: I am using Drupal 8, and this code is in the .theme file