image_entity_presave(EntityInterface $entity)
Implements hook_entity_presave().
Transforms default image of image field from array into single value at save.
File
- core/modules/image/image.module, line 339
- Exposes global functionality for creating image styles.
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | 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::entityManager()->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 ); } |
Please login to continue.