Skip to content

Instantly share code, notes, and snippets.

@mirie
Created October 10, 2016 17:29
Show Gist options
  • Save mirie/ae430d53bc5cf78f12e8cf6dd7e10aaa to your computer and use it in GitHub Desktop.
Save mirie/ae430d53bc5cf78f12e8cf6dd7e10aaa to your computer and use it in GitHub Desktop.
postSave() of FileEntity
/**
* Implements hook_file_insert().
*/
public function postSave(EntityStorageInterface $storage, $update = TRUE) {
parent::postSave($storage, $update);
// Save file metadata.
if (!empty($this->metadata)) {
if ($update) {
db_delete('file_metadata')->condition('fid', $this->id())->execute();
}
$query = db_insert('file_metadata')->fields(array('fid', 'name', 'value'));
foreach ($this->getAllMetadata() as $name => $value) {
$query->values(array(
'fid' => $this->id(),
'name' => $name,
'value' => serialize($value),
));
}
$query->execute();
}
if ($update) {
if (\Drupal::moduleHandler()->moduleExists('image') && $this->getMimeTypeType() == 'image' && $this->getSize()) {
// If the image dimensions have changed, update any image field references
// to this file and flush image style derivatives.
if ($this->original->getMetadata('width') && ($this->getMetadata('width') != $this->original->getMetadata('width') || $this->getMetadata('height') != $this->original->getMetadata('height'))) {
$this->updateImageFieldDimensions();
}
// Flush image style derivatives whenever an image is updated.
image_path_flush($this->getFileUri());
}
}
}
@mirie
Copy link
Author

mirie commented Oct 10, 2016

    if ($update) {
      if (\Drupal::moduleHandler()->moduleExists('image') && $this->getMimeTypeType() == 'image' && $this->getSize()) {
        // If the image dimensions have changed, update any image field references
        // to this file and flush image style derivatives.
        if ($this->original->getMetadata('width') && ($this->getMetadata('width') != $this->original->getMetadata('width') || $this->getMetadata('height') != $this->original->getMetadata('height'))) {
          $this->updateImageFieldDimensions();
          // Flush image style derivatives whenever an image is updated.          
          image_path_flush($this->getFileUri());
        }

        /* Move the image_path_flush to if block above. 
         * When updating the image (with replace image), Drupal\file_entity\Form\FileEditForm::submitForm() has this block, which results in an image style flush:
         * if (file_unmanaged_copy($entity_replacement->getFileUri(), $this->entity->getFileUri(), FILE_EXISTS_REPLACE)) {
         *   $entity_replacement->delete();
         *   \Drupal::logger('file_entity')->info('File @old was replaced by @new', $log_args);
         * }
         *
         * It seems that even when the replacement file has a different filename, the uri is not changed. It remains to be the same.
         * Also changing the "filename" field has no effect on the file uri.
         */
      }
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment