function image_entity_presave
Same name in other branches
- 9 core/modules/image/image.module \image_entity_presave()
- 10 core/modules/image/image.module \image_entity_presave()
- 11.x core/modules/image/image.module \image_entity_presave()
Implements hook_entity_presave().
Transforms default image of image field from array into single value at save.
File
-
core/
modules/ image/ image.module, line 372
Code
function image_entity_presave(EntityInterface $entity) {
// Get the default image settings, return if not saving an image field storage
// or image field entity.
$default_image = [];
if (($entity instanceof FieldStorageConfigInterface || $entity instanceof FieldConfigInterface) && $entity->getType() == 'image') {
$default_image = $entity->getSetting('default_image');
}
else {
return;
}
if ($entity->isSyncing()) {
return;
}
$uuid = $default_image['uuid'];
if ($uuid) {
$original_uuid = isset($entity->original) ? $entity->original
->getSetting('default_image')['uuid'] : NULL;
if ($uuid != $original_uuid) {
$file = \Drupal::service('entity.repository')->loadEntityByUuid('file', $uuid);
if ($file) {
$image = \Drupal::service('image.factory')->get($file->getFileUri());
$default_image['width'] = $image->getWidth();
$default_image['height'] = $image->getHeight();
}
else {
$default_image['uuid'] = NULL;
}
}
}
// Both FieldStorageConfigInterface and FieldConfigInterface have a
// setSetting() method.
$entity->setSetting('default_image', $default_image);
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.