W3cubDocs

/Drupal 8

function image_field_storage_config_update

image_field_storage_config_update(FieldStorageConfigInterface $field_storage)

Implements hook_ENTITY_TYPE_update() for 'field_storage_config'.

File

core/modules/image/image.module, line 377
Exposes global functionality for creating image styles.

Code

function image_field_storage_config_update(FieldStorageConfigInterface $field_storage) {
  if ($field_storage->getType() != 'image') {
    // Only act on image fields.
    return;
  }

  $prior_field_storage = $field_storage->original;

  // The value of a managed_file element can be an array if #extended == TRUE.
  $uuid_new = $field_storage->getSetting('default_image')['uuid'];
  $uuid_old = $prior_field_storage->getSetting('default_image')['uuid'];

  $file_new = $uuid_new ? \Drupal::entityManager()->loadEntityByUuid('file', $uuid_new) : FALSE;

  if ($uuid_new != $uuid_old) {

    // Is there a new file?
    if ($file_new) {
      $file_new->status = FILE_STATUS_PERMANENT;
      $file_new->save();
      \Drupal::service('file.usage')->add($file_new, 'image', 'default_image', $field_storage->uuid());
    }

    // Is there an old file?
    if ($uuid_old && ($file_old = \Drupal::entityManager()->loadEntityByUuid('file', $uuid_old))) {
      \Drupal::service('file.usage')->delete($file_old, 'image', 'default_image', $field_storage->uuid());
    }
  }

  // If the upload destination changed, then move the file.
  if ($file_new && (file_uri_scheme($file_new->getFileUri()) != $field_storage->getSetting('uri_scheme'))) {
    $directory = $field_storage->getSetting('uri_scheme') . '://default_images/';
    file_prepare_directory($directory, FILE_CREATE_DIRECTORY);
    file_move($file_new, $directory . $file_new->filename);
  }
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!modules!image!image.module/function/image_field_storage_config_update/8.1.x