You are here

public function MetadataEventSubscriber::setMetadata in Filebrowser 3.x

Same name and namespace in other branches
  1. 8.2 src/EventSubscriber/MetadataEventSubscriber.php \Drupal\filebrowser\EventSubscriber\MetadataEventSubscriber::setMetadata()

File

src/EventSubscriber/MetadataEventSubscriber.php, line 31

Class

MetadataEventSubscriber

Namespace

Drupal\filebrowser\EventSubscriber

Code

public function setMetadata(MetadataEvent $event) {

  /** @var FilebrowserMetadataEntity $metadata */
  $this->nid = $event->nid;
  $fid = $event
    ->getFid();
  $file = $event->file;
  $subdir_fid = $event->subdir_fid;
  $columns = $event->columns;
  $meta = $this
    ->MetadataIds();
  foreach ($meta as $name => $title) {

    // only the selected columns
    if ($columns[$name]) {
      $data = $this
        ->createData($name, $fid, $file, $subdir_fid);
      $query = \Drupal::entityQuery('filebrowser_metadata_entity')
        ->condition('fid', $fid)
        ->condition('module', 'filebrowser')
        ->condition('name', $name);
      $entity_id = $query
        ->execute();
      if ($entity_id) {

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