I have an image field that has a default image if no image is uploaded. In mytheme.theme file I am trying to get the default image and display it. Here is the code that I am using:
$field = $entity->get($fieldName); // Loading from field definition
if ($field) {
$default_image = $field->getSetting('default_image');
if ($default_image && $default_image['uuid']) {
$entityrepository = Drupal::service('entity.repository');
$defaultImageFile = $entityrepository->loadEntityByUuid('file', $default_image['uuid']);
if ($defaultImageFile) {
$image_uri = $defaultImageFile->getFileUri();
}
}
}
The issue is that the $default_image['uuid'] is empty. I am not sure how that can be if the default image is set. Since the image uuid is empty, the image cannot be loaded and now I am stuck.
What is wrong with the code that I am using? Or is there another way to accomplish this that works?