You are here

class MetadataEventSubscriber in Filebrowser 8.2

Same name in this branch
  1. 8.2 src/EventSubscriber/MetadataEventSubscriber.php \Drupal\filebrowser\EventSubscriber\MetadataEventSubscriber
  2. 8.2 modules/src/EventSubscriber/MetadataEventSubscriber.php \Drupal\filebrowser_extra\EventSubscriber\MetadataEventSubscriber
Same name and namespace in other branches
  1. 3.x modules/src/EventSubscriber/MetadataEventSubscriber.php \Drupal\filebrowser_extra\EventSubscriber\MetadataEventSubscriber

Hierarchy

Expanded class hierarchy of MetadataEventSubscriber

1 string reference to 'MetadataEventSubscriber'
filebrowser_extra.services.yml in modules/filebrowser_extra.services.yml
modules/filebrowser_extra.services.yml
1 service uses MetadataEventSubscriber
filebrowser_modified.metadata_event in modules/filebrowser_extra.services.yml
Drupal\filebrowser_extra\EventSubscriber\MetadataEventSubscriber

File

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

Namespace

Drupal\filebrowser_extra\EventSubscriber
View source
class MetadataEventSubscriber implements EventSubscriberInterface {
  use StringTranslationTrait;

  /**
   * @var integer
   */
  protected $nid;
  public static function getSubscribedEvents() {
    $events['filebrowser.metadata_event'][] = [
      'createModified',
      0,
    ];
    return $events;
  }

  // fixme: "go up" folder table cell for modified not shown
  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();
        }
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MetadataEventSubscriber::$nid protected property
MetadataEventSubscriber::createModified public function
MetadataEventSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.