This repository has been archived by the owner on Jan 5, 2018. It is now read-only.
/
Media.php
339 lines (295 loc) · 9.47 KB
/
Media.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<?php
/**
* @file
* Contains \Drupal\media_entity\Entity\Media.
*/
namespace Drupal\media_entity\Entity;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\media_entity\MediaInterface;
/**
* Defines the media entity class.
*
* @ContentEntityType(
* id = "media",
* label = @Translation("Media"),
* bundle_label = @Translation("Media bundle"),
* handlers = {
* "storage" = "Drupal\media_entity\MediaStorage",
* "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
* "access" = "Drupal\media_entity\MediaAccessController",
* "form" = {
* "default" = "Drupal\media_entity\MediaForm",
* "delete" = "Drupal\media_entity\Form\MediaDeleteForm",
* "edit" = "Drupal\media_entity\MediaForm"
* },
* "translation" = "Drupal\content_translation\ContentTranslationHandler"
* },
* base_table = "media",
* data_table = "media_field_data",
* revision_table = "media_revision",
* revision_data_table = "media_field_revision",
* fieldable = TRUE,
* translatable = TRUE,
* render_cache = TRUE,
* entity_keys = {
* "id" = "mid",
* "revision" = "vid",
* "bundle" = "bundle",
* "label" = "name",
* "uuid" = "uuid"
* },
* bundle_entity_type = "media_bundle",
* permission_granularity = "entity_type",
* admin_permission = "administer media",
* field_ui_base_route = "media.bundle_edit",
* links = {
* "canonical" = "media.view",
* "delete-form" = "media.delete_confirm",
* "edit-form" = "media.edit",
* "admin-form" = "media.bundle_edit"
* }
* )
*/
class Media extends ContentEntityBase implements MediaInterface {
/**
* Value that represents the media being published.
*/
const PUBLISHED = 1;
/**
* Value that represents the media being unpublished.
*/
const NOT_PUBLISHED = 0;
/**
* {@inheritdoc}
*/
public function getBundle() {
return $this->bundle();
}
/**
* {@inheritdoc}
*/
public function getName() {
return $this->get('name')->value;
}
/**
* {@inheritdoc}
*/
public function setName($name) {
$this->set('name', $name);
return $this;
}
/**
* {@inheritdoc}
*/
public function getCreatedTime() {
return $this->get('created')->value;
}
/**
* {@inheritdoc}
*/
public function setCreatedTime($timestamp) {
$this->set('created', $timestamp);
return $this;
}
/**
* {@inheritdoc}
*/
public function getChangedTime() {
return $this->get('changed')->value;
}
/**
* {@inheritdoc}
*/
public function isPublished() {
return (bool) $this->get('status')->value;
}
/**
* {@inheritdoc}
*/
public function setPublished($published) {
$this->set('status', $published ? Media::PUBLISHED : Media::NOT_PUBLISHED);
return $this;
}
/**
* {@inheritdoc}
*/
public function getPublisher() {
return $this->get('uid')->entity;
}
/**
* {@inheritdoc}
*/
public function getPublisherId() {
return $this->get('uid')->target_id;
}
/**
* {@inheritdoc}
*/
public function setPublisherId($uid) {
$this->set('uid', $uid);
return $this;
}
/**
* {@inheritdoc}
*/
public function getType() {
return $this->get('type')->value;
}
/**
* {@inheritdoc}
*/
public function setType($type) {
$this->set('type', $type);
return $this;
}
/**
* {@inheritdoc}
*/
public function getResourceId() {
return $this->get('resource_id')->value;
}
/**
* {@inheritdoc}
*/
public function setResourceId($id) {
$this->set('resource_id', $id);
return $this;
}
/**
* {@inheritdoc}
*/
public function preSave(EntityStorageInterface $storage) {
parent::preSave($storage);
// If no owner has been set explicitly, make the current user the owner.
if (!$this->getPublisher()) {
$this->setPublisherId(\Drupal::currentUser()->id());
}
// If no revision author has been set explicitly, make the media owner the
// revision author.
if (!$this->get('revision_uid')->entity) {
$this->set('revision_uid', $this->getPublisherId());
}
// Try to set fields provided by type plugin and mapped in bundle
// configuration.
$bundle = $this->entityManager()->getStorage('media_bundle')->load($this->bundle());
foreach ($bundle->field_map as $source_field => $destination_field) {
// Only save value in entity field if empty. Do not overwrite existing data.
// @TODO We might modify that in the future but let's leave it like this
// for now.
if ($this->{$destination_field} && $this->{$destination_field}->isEmpty() && ($value = $bundle->getType()->getField($this, $source_field))) {
$property_name = $this->{$destination_field}->first()->mainPropertyName();
$this->{$destination_field}->first()->{$property_name} = $value;
}
}
}
/**
* {@inheritdoc}
*/
public function preSaveRevision(EntityStorageInterface $storage, \stdClass $record) {
parent::preSaveRevision($storage, $record);
if (!$this->isNewRevision() && isset($this->original) && (!isset($record->revision_log) || $record->revision_log === '')) {
// If we are updating an existing node without adding a new revision, we
// need to make sure $entity->revision_log is reset whenever it is empty.
// Therefore, this code allows us to avoid clobbering an existing log
// entry with an empty one.
$record->revision_log = $this->original->revision_log->value;
}
}
/**
* {@inheritdoc}
*/
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields['mid'] = BaseFieldDefinition::create('integer')
->setLabel(t('Media ID'))
->setDescription(t('The media ID.'))
->setReadOnly(TRUE)
->setSetting('unsigned', TRUE);
$fields['uuid'] = BaseFieldDefinition::create('uuid')
->setLabel(t('UUID'))
->setDescription(t('The media UUID.'))
->setReadOnly(TRUE);
$fields['vid'] = BaseFieldDefinition::create('integer')
->setLabel(t('Revision ID'))
->setDescription(t('The media revision ID.'))
->setReadOnly(TRUE)
->setSetting('unsigned', TRUE);
$fields['bundle'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Bundle'))
->setDescription(t('The media bundle.'))
->setSetting('target_type', 'media_bundle')
->setReadOnly(TRUE);
$fields['langcode'] = BaseFieldDefinition::create('language')
->setLabel(t('Language code'))
->setDescription(t('The media language code.'))
->setRevisionable(TRUE);
$fields['name'] = BaseFieldDefinition::create('string')
->setLabel(t('Media name'))
->setDescription(t('The name of this media.'))
->setRequired(TRUE)
->setTranslatable(TRUE)
->setRevisionable(TRUE)
->setDefaultValue('')
->setSetting('max_length', 255)
->setDisplayOptions('form', array(
'type' => 'string',
'weight' => -5,
))
->setDisplayConfigurable('form', TRUE);
$fields['uid'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Publisher ID'))
->setDescription(t('The user ID of the media publisher.'))
->setRevisionable(TRUE)
->setDefaultValue(0)
->setSetting('target_type', 'user')
->setTranslatable(TRUE);
$fields['status'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Publishing status'))
->setDescription(t('A boolean indicating whether the media is published.'))
->setTranslatable(TRUE)
->setRevisionable(TRUE);
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Created'))
->setDescription(t('The time that the media was created.'))
->setTranslatable(TRUE)
->setRevisionable(TRUE);
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Changed'))
->setDescription(t('The time that the media was last edited.'))
->setTranslatable(TRUE)
->setRevisionable(TRUE);
$fields['type'] = BaseFieldDefinition::create('string')
->setLabel(t('Type'))
->setDescription(t('The type of this media.'))
->setRequired(TRUE)
->setSetting('max_length', 255)
->setRevisionable(TRUE)
->setTranslatable(TRUE);
$fields['resource_id'] = BaseFieldDefinition::create('string')
->setLabel(t('Resource ID'))
->setDescription(t('The unique identifier of media resource that is associated with this media.'))
->setRequired(TRUE)
->setSetting('max_length', 255)
->setRevisionable(TRUE)
->setTranslatable(TRUE);
$fields['revision_timestamp'] = BaseFieldDefinition::create('created')
->setLabel(t('Revision timestamp'))
->setDescription(t('The time that the current revision was created.'))
->setQueryable(FALSE)
->setRevisionable(TRUE);
$fields['revision_uid'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Revision publisher ID'))
->setDescription(t('The user ID of the publisher of the current revision.'))
->setSetting('target_type', 'user')
->setQueryable(FALSE)
->setRevisionable(TRUE);
$fields['revision_log'] = BaseFieldDefinition::create('string_long')
->setLabel(t('Revision Log'))
->setDescription(t('The log entry explaining the changes in this revision.'))
->setRevisionable(TRUE)
->setTranslatable(TRUE);
return $fields;
}
}