Created
October 10, 2016 17:29
-
-
Save mirie/ae430d53bc5cf78f12e8cf6dd7e10aaa to your computer and use it in GitHub Desktop.
postSave() of FileEntity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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()); | |
} | |
} | |
} |
Author
mirie
commented
Oct 10, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment