forked from colorfield/repec
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Base.php
132 lines (113 loc) · 3.56 KB
/
Base.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
namespace Drupal\repec\Series;
use Drupal\Core\Config\ImmutableConfig;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
/**
* Base structure for templating classes.
*/
abstract class Base implements BaseInterface {
use StringTranslationTrait;
/**
* Repec settings.
*
* @var \Drupal\Core\Config\ImmutableConfig
*/
protected $settings;
/**
* Entity used for generating the template.
*
* @var \Drupal\Core\Entity\ContentEntityInterface
*/
protected $entity;
/**
* Entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Messenger.
*
* @var \Drupal\Core\Messenger\MessengerInterface
*/
protected $messenger;
/**
* Bundle specific setting in repec settings.
*
* @var array|null
*/
protected $bundleSettings;
/**
* Base constructor.
*
* @param \Drupal\Core\Config\ImmutableConfig $settings
* Repec settings.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* Entity type manager.
* @param \Drupal\Core\Messenger\MessengerInterface $messenger
* Messenger.
* @param \Drupal\Core\Entity\ContentEntityInterface $entity
* The entity.
* @param array|null $bundle_settings
* Bundle specific setting in repec settings.
*/
public function __construct(ImmutableConfig $settings, EntityTypeManagerInterface $entity_type_manager, MessengerInterface $messenger, ContentEntityInterface $entity, array $bundle_settings) {
$this->settings = $settings;
$this->entityTypeManager = $entity_type_manager;
$this->messenger = $messenger;
$this->entity = $entity;
$this->bundleSettings = $bundle_settings;
}
/**
* {@inheritdoc}
*/
public function getDefault() : array {
// Get html title field and strip html tags.
$title = strip_tags($this->entity->html_title->value);
return [
[
'attribute' => 'Title',
'value' => $title,
],
[
'attribute' => 'Number',
// Entity id cannot be used here as there could be
// probably several entity types in a further release.
'value' => $this->entity->uuid(),
],
[
'attribute' => 'Handle',
'value' => "RePEc:{$this->settings->get('archive_code')}:{$this->bundleSettings['serie_type']}:{$this->entity->id()}",
],
];
}
/**
* {@inheritdoc}
*/
public function create(array $template) {
/** @var string $serie_directory_config */
$serie_directory_config = $this->bundleSettings['serie_directory'];
$archive_directory = "public://{$this->settings->get('base_path')}/{$this->settings->get('archive_code')}/";
$directory = "{$archive_directory}{$serie_directory_config}/";
if (!file_prepare_directory($directory, FILE_CREATE_DIRECTORY)) {
throw new CreateException($this->t('Directory @path could not be created.', [
'@path' => $directory,
]));
}
$file_name = "{$serie_directory_config}_{$this->entity->getEntityTypeId()}_{$this->entity->id()}.rdf";
$content = '';
foreach ($template as $item) {
if (!empty($item['value'])) {
$content .= $item['attribute'] . ': ' . $item['value'] . "\n";
}
}
if (!file_put_contents("$directory/$file_name", $content)) {
throw new CreateException($this->t('File @file_name could not be created.', [
'@file_name' => $file_name,
]));
}
}
}