class MetadataEventSubscriber in Filebrowser 8.2
Same name in this branch
- 8.2 src/EventSubscriber/MetadataEventSubscriber.php \Drupal\filebrowser\EventSubscriber\MetadataEventSubscriber
- 8.2 modules/src/EventSubscriber/MetadataEventSubscriber.php \Drupal\filebrowser_extra\EventSubscriber\MetadataEventSubscriber
Same name and namespace in other branches
- 3.x src/EventSubscriber/MetadataEventSubscriber.php \Drupal\filebrowser\EventSubscriber\MetadataEventSubscriber
Hierarchy
- class \Drupal\filebrowser\EventSubscriber\MetadataEventSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface uses StringTranslationTrait
Expanded class hierarchy of MetadataEventSubscriber
1 string reference to 'MetadataEventSubscriber'
1 service uses MetadataEventSubscriber
File
- src/
EventSubscriber/ MetadataEventSubscriber.php, line 13
Namespace
Drupal\filebrowser\EventSubscriberView source
class MetadataEventSubscriber implements EventSubscriberInterface {
use StringTranslationTrait;
protected $storage;
protected $nid;
public function __construct() {
$this->storage = \Drupal::entityTypeManager()
->getStorage('filebrowser_metadata_entity');
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events['filebrowser.metadata_event'][] = [
'setMetadata',
0,
];
return $events;
}
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();
}
}
}
}
protected function createData($id, $fid, $file, $subdir_fid) {
if ($file->fileData->type == 'file') {
/** @var DisplayFile $file */
switch ($id) {
case 'description':
return [
'content' => $this
->generateDescription($file, $subdir_fid, $fid),
'theme' => 'filebrowser_description',
];
case 'size':
return [
'content' => format_size($file->fileData->size),
'theme' => "",
];
case 'created':
return [
'theme' => "",
'content' => \Drupal::service('date.formatter')
->format($file->fileData->timestamp, 'short'),
];
case 'mimetype':
return [
'theme' => "",
'content' => $file->fileData->mimetype,
];
}
}
else {
if ($id == 'description') {
return [
'content' => $this
->generateDescription($file, $subdir_fid, $fid),
'theme' => 'filebrowser_description',
];
}
else {
return [
'theme' => "",
'content' => "",
];
}
}
}
public function MetadataIds() {
return [
'description' => $this
->t('Description'),
'size' => $this
->t('File size'),
'created' => $this
->t('Created'),
'mimetype' => $this
->t('Mimetype'),
];
}
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'),
];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
MetadataEventSubscriber:: |
protected | property | ||
MetadataEventSubscriber:: |
protected | property | ||
MetadataEventSubscriber:: |
protected | function | ||
MetadataEventSubscriber:: |
public | function | ||
MetadataEventSubscriber:: |
public static | function | Returns an array of event names this subscriber wants to listen to. | |
MetadataEventSubscriber:: |
public | function | ||
MetadataEventSubscriber:: |
public | function | ||
MetadataEventSubscriber:: |
public | function | ||
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |