You are here

public function MetadataEventSubscriber::createModified in Filebrowser 8.2

Same name and namespace in other branches
  1. 3.x modules/src/EventSubscriber/MetadataEventSubscriber.php \Drupal\filebrowser_extra\EventSubscriber\MetadataEventSubscriber::createModified()

File

modules/src/EventSubscriber/MetadataEventSubscriber.php, line 25

Class

MetadataEventSubscriber

Namespace

Drupal\filebrowser_extra\EventSubscriber

Code

public function createModified(MetadataEvent $event) {

  /** @var FilebrowserMetadataEntity $metadata */

  /** @var DisplayFile $file */
  $this->nid = $event->nid;
  $fid = $event
    ->getFid();
  $file = $event->file;
  $columns = $event->columns;

  // Only calculate modified time if this column is selected
  if (!empty($columns['modified'])) {
    if (isset($file->fileData->uri)) {
      $file_real_path = \Drupal::service('file_system')
        ->realpath($file->fileData->uri);
      $m_time = filemtime($file_real_path);
      $m_time = empty($m_time) ? 0 : $m_time;
      $content = serialize(\Drupal::service('date.formatter')
        ->format($m_time, 'short'));
      $theme = "";
      $storage = \Drupal::entityTypeManager()
        ->getStorage('filebrowser_metadata_entity');
      $query = \Drupal::entityQuery('filebrowser_metadata_entity')
        ->condition('fid', $fid)
        ->condition('module', 'filebrowser_extra')
        ->condition('name', 'modified');
      $entity_id = $query
        ->execute();
      if ($entity_id) {

        // entity exists, so we just update the contents
        $metadata = $storage
          ->load(reset($entity_id));
        $metadata
          ->setContent($content);
        $metadata
          ->save();
      }
      else {
        $value = [
          'fid' => $fid,
          'nid' => $this->nid,
          'name' => 'modified',
          'title' => t('Modified'),
          'module' => 'filebrowser_extra',
          'theme' => $theme,
          'content' => $content,
        ];
        $entity = FilebrowserMetadataEntity::create($value);
        $entity
          ->save();
      }
    }
  }
}