You are here

public function FileEntity::postSave in File Entity (fieldable files) 8.2

Implements hook_file_insert().

Overrides ContentEntityBase::postSave

File

src/Entity/FileEntity.php, line 199

Class

FileEntity
Replace for the core file entity class.

Namespace

Drupal\file_entity\Entity

Code

public function postSave(EntityStorageInterface $storage, $update = TRUE) {
  parent::postSave($storage, $update);

  // Save file metadata.
  if ($this->metadataChanged) {
    if ($update) {
      \Drupal::database()
        ->delete('file_metadata')
        ->condition('fid', $this
        ->id())
        ->execute();
    }
    $query = \Drupal::database()
      ->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();
    $this->metadataChanged = FALSE;
  }
  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());
    }
  }
}