-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArchiveListContents.php
95 lines (82 loc) · 2.61 KB
/
ArchiveListContents.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
namespace Drupal\archive_list_contents\Plugin\Field\FieldFormatter;
use Drupal\Core\Archiver\Zip;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Component\Utility\Html;
use Drupal\file\Plugin\Field\FieldFormatter\FileFormatterBase;
use Drupal\Core\File\FileSystemInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Plugin implementation of the 'archive_list_contents' formatter.
*
* @FieldFormatter(
* id = "archive_list_contents",
* label = @Translation("List archive contents"),
* field_types = {
* "file"
* }
* )
*/
class ArchiveListContents extends FileFormatterBase {
/**
* @var \Drupal\Core\File\FileSystem
*/
protected $fileSystem;
/**
* {@inheritdoc}
*/
public function __construct($plugin_id, $plugin_definition, $field_definition, array $settings, $label, $view_mode, array $third_party_settings, FileSystemInterface $file_system) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
$this->fileSystem = $file_system;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$plugin_id,
$plugin_definition,
$configuration['field_definition'],
$configuration['settings'],
$configuration['label'],
$configuration['view_mode'],
$configuration['third_party_settings'],
$container->get('file_system'),
);
}
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$element = [];
$element['#attached']['library'][] = 'archive_list_contents/archive_list_contents';
foreach ($this->getEntitiesToView($items, $langcode) as $delta => $file) {
$uri = $file->getFileUri();
$realpath = $this->fileSystem->realpath($uri);
$archive = new Zip($realpath);
$fileList = $archive->listContents();
// download link
$element[$delta][] = [
'#theme' => 'file_link',
'#file' => $file,
'#cache' => [
'tags' => $file->getCacheTags(),
],
];
// file size
$element[$delta][] = [
'#markup' => $this->t('<i> - ' . $file->getSize() . ' bytes</i><br>'),
];
$element[$delta]['archiveList'] = [
'#type' => 'container',
'#attributes' => [
'class' => 'archivelist-readmore',
],
];
$element[$delta]['archiveList'][] = [
'#markup' => $this->t(implode('<br>', $fileList)),
];
}
return $element;
}
}