0

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 = \Drupal\user\Entity\User::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

0

You can check if a user picture exists this way:

if (!$user->user_picture->isEmpty()) {
  $picture = file_create_url($file->getFileUri());
  $picture = $user->user_picture->view('large');
} else {
  $picture_field = \Drupal\field\Entity\FieldConfig::loadByName('user', 'user', 'user_picture');
  $default_image = $picture_field->getSetting('default_image');
  $file = \Drupal::entityManager()->loadEntityByUuid('file', $default_image['uuid']);
  $picture = $file->getFileUri();
}
0

Custom Function:

function custom_ImageURL() {

  $imageURL = NULL;

  $imageFieldName = // However you're getting your field name.

  // Check the field exists.
  if (!empty(FieldConfig::loadByName('user', 'user', $imageFieldName))) {

    // Check there is a primary image.
    if ($user->$imageFieldName->entity) {

      $imageURL = $user->$imageFieldName->entity->url();

    }
    // No primary image, check for default image.
    elseif (FieldConfig::loadByName('user', 'user', $imageFieldName)
      ->getSetting('default_image')['uuid']) {

      $imageGetSettings = FieldConfig::loadByName('user', 'user', $imageFieldName)
        ->getSetting('default_image');

      $imageURI = Drupal::service('entity.repository')
        ->loadEntityByUuid('file', $imageGetSettings['uuid'])
        ->getFileUri();

      $imageURL = file_create_url($imageURI);

    }

  }

  return $imageURL;

}

To load with an image style replace $imageURL = ... with:

$style = \Drupal::entityTypeManager()
  ->getStorage('image_style')->load('thumbnail');

$imageStyleURL = $style->buildUrl($user
  ->$imageFieldName->entity->uri->value);

$imageURL = $imageStyleURL;

Then in whatever preprocess function you need:

function hook_preprocess_menu(&$variables, $hook) {

  $imageURL = custom_ImageURL();

}

I have found checking if the field exists via FieldConfig::loadByName() produces fewer use case errors than ->isEmpty or ->isEmpty().

Then you can compare against entity existing for the primary value. The default value is stored in field settings array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.