You are here

public function MetadataEventSubscriber::generateDescription in Filebrowser 3.x

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

File

src/EventSubscriber/MetadataEventSubscriber.php, line 126

Class

MetadataEventSubscriber

Namespace

Drupal\filebrowser\EventSubscriber

Code

public function generateDescription($file, $subdir_fid, $fid) {

  /** @var FilebrowserMetadataEntity $metadata */

  // get the present description
  $query = \Drupal::entityQuery('filebrowser_metadata_entity')
    ->condition('fid', $fid)
    ->condition('module', 'filebrowser')
    ->condition('name', 'description');
  $entity_id = $query
    ->execute();
  if ($entity_id) {

    // entity exists
    $metadata = $this->storage
      ->load(reset($entity_id));
    $content = unserialize($metadata->content->value);

    //originally title was not set for directories. So even if the entity existed, there was no title
    $description = isset($content['title']) ? $content['title'] : $this
      ->t('Default description');
  }
  else {

    // no description available
    $description = $this
      ->t('Default description');
  }
  if (!empty($subdir_fid)) {

    //this is a subfolder
    $p = [
      'nid' => $this->nid,
      'query_fid' => $subdir_fid,
      'fids' => $fid,
    ];
  }
  else {
    $p = [
      'nid' => $this->nid,
      'fids' => $fid,
    ];
  }
  return [
    'create_link' => $file->name == '..' ? false : true,
    'title' => $file->name == '..' ? '' : $description,
    'url' => Url::fromRoute('filebrowser.inline_description_form', $p),
    'attributes' => [
      'class' => [
        'use-ajax',
      ],
      'data-dialog-type' => 'modal',
      'data-dialog-options' => Json::encode([
        'width' => 700,
      ]),
    ],
    'image_title' => $this
      ->t('Edit description'),
  ];
}