5f0861d42bde00d1271998a0b7259d8a2060ce0e
[yaffs-website] / web / modules / contrib / media_entity / src / Entity / Media.php
1 <?php
2
3 namespace Drupal\media_entity\Entity;
4
5 use Drupal\Core\Entity\ContentEntityBase;
6 use Drupal\Core\Entity\EntityStorageInterface;
7 use Drupal\Core\Entity\EntityTypeInterface;
8 use Drupal\Core\Field\BaseFieldDefinition;
9 use Drupal\media_entity\MediaInterface;
10 use Drupal\Core\Entity\EntityChangedTrait;
11 use Drupal\user\UserInterface;
12
13 /**
14  * Defines the media entity class.
15  *
16  * @ContentEntityType(
17  *   id = "media",
18  *   label = @Translation("Media"),
19  *   bundle_label = @Translation("Media bundle"),
20  *   handlers = {
21  *     "storage" = "Drupal\media_entity\MediaStorage",
22  *     "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
23  *     "list_builder" = "Drupal\Core\Entity\EntityListBuilder",
24  *     "access" = "Drupal\media_entity\MediaAccessController",
25  *     "form" = {
26  *       "default" = "Drupal\media_entity\MediaForm",
27  *       "delete" = "Drupal\media_entity\Form\MediaDeleteForm",
28  *       "edit" = "Drupal\media_entity\MediaForm"
29  *     },
30  *     "inline_form" = "Drupal\media_entity\Form\MediaInlineForm",
31  *     "translation" = "Drupal\content_translation\ContentTranslationHandler",
32  *     "views_data" = "Drupal\media_entity\MediaViewsData",
33  *     "route_provider" = {
34  *       "html" = "Drupal\Core\Entity\Routing\AdminHtmlRouteProvider",
35  *     }
36  *   },
37  *   base_table = "media",
38  *   data_table = "media_field_data",
39  *   revision_table = "media_revision",
40  *   revision_data_table = "media_field_revision",
41  *   translatable = TRUE,
42  *   render_cache = TRUE,
43  *   entity_keys = {
44  *     "id" = "mid",
45  *     "revision" = "vid",
46  *     "bundle" = "bundle",
47  *     "label" = "name",
48  *     "langcode" = "langcode",
49  *     "uuid" = "uuid"
50  *   },
51  *   bundle_entity_type = "media_bundle",
52  *   permission_granularity = "entity_type",
53  *   admin_permission = "administer media",
54  *   field_ui_base_route = "entity.media_bundle.edit_form",
55  *   links = {
56  *     "add-page" = "/media/add",
57  *     "add-form" = "/media/add/{media_bundle}",
58  *     "canonical" = "/media/{media}",
59  *     "delete-form" = "/media/{media}/delete",
60  *     "edit-form" = "/media/{media}/edit",
61  *     "admin-form" = "/admin/structure/media/manage/{media_bundle}"
62  *   }
63  * )
64  */
65 class Media extends ContentEntityBase implements MediaInterface {
66
67   use EntityChangedTrait;
68
69   /**
70    * Value that represents the media being published.
71    */
72   const PUBLISHED = 1;
73
74   /**
75    * Value that represents the media being unpublished.
76    */
77   const NOT_PUBLISHED = 0;
78
79   /**
80    * A queue based media operation to download thumbnails is being performed.
81    *
82    * @var boolean
83    */
84   protected $queued_thumbnail_download = FALSE;
85
86   /**
87    * {@inheritdoc}
88    */
89   public function getCreatedTime() {
90     return $this->get('created')->value;
91   }
92
93   /**
94    * {@inheritdoc}
95    */
96   public function setCreatedTime($timestamp) {
97     $this->set('created', $timestamp);
98     return $this;
99   }
100
101   /**
102    * {@inheritdoc}
103    */
104   public function getChangedTime() {
105     return $this->get('changed')->value;
106   }
107
108   /**
109    * {@inheritdoc}
110    */
111   public function isPublished() {
112     return (bool) $this->get('status')->value;
113   }
114
115   /**
116    * {@inheritdoc}
117    */
118   public function setPublished($published) {
119     $this->set('status', $published ? Media::PUBLISHED : Media::NOT_PUBLISHED);
120     return $this;
121   }
122
123   /**
124    * {@inheritdoc}
125    */
126   public function getPublisher() {
127     return $this->get('uid')->entity;
128   }
129
130   /**
131    * {@inheritdoc}
132    */
133   public function setQueuedThumbnailDownload() {
134     $this->queued_thumbnail_download = TRUE;
135   }
136
137   /**
138    * {@inheritdoc}
139    */
140   public function getPublisherId() {
141     return $this->get('uid')->target_id;
142   }
143
144   /**
145    * {@inheritdoc}
146    */
147   public function setPublisherId($uid) {
148     $this->set('uid', $uid);
149     return $this;
150   }
151
152   /**
153    * {@inheritdoc}
154    */
155   public function getType() {
156     return $this->bundle->entity->getType();
157   }
158
159   /**
160    * {@inheritdoc}
161    */
162   public function preSave(EntityStorageInterface $storage) {
163     parent::preSave($storage);
164
165     // If no revision author has been set explicitly, make the media owner the
166     // revision author.
167     if (!$this->get('revision_uid')->entity) {
168       $this->set('revision_uid', $this->getPublisherId());
169     }
170
171     // Set thumbnail.
172     if (!$this->get('thumbnail')->entity || !empty($this->queued_thumbnail_download)) {
173       $this->automaticallySetThumbnail();
174     }
175
176     // Try to set fields provided by type plugin and mapped in bundle
177     // configuration.
178     foreach ($this->bundle->entity->field_map as $source_field => $destination_field) {
179       // Only save value in entity field if empty. Do not overwrite existing
180       // data.
181       // @TODO We might modify that in the future but let's leave it like this
182       // for now.
183       if ($this->hasField($destination_field) && $this->{$destination_field}->isEmpty() && ($value = $this->getType()->getField($this, $source_field))) {
184         $this->set($destination_field, $value);
185       }
186     }
187
188     // Try to set a default name for this media, if there is no label provided.
189     if (empty($this->label())) {
190       $this->set('name', $this->getType()->getDefaultName($this));
191     }
192
193   }
194
195   /**
196    * {@inheritdoc}
197    */
198   public function postSave(EntityStorageInterface $storage, $update = TRUE) {
199     parent::postSave($storage, $update);
200     if (!$update && $this->bundle->entity->getQueueThumbnailDownloads()) {
201       $queue = \Drupal::queue('media_entity_thumbnail');
202       $queue->createItem(['id' => $this->id()]);
203     }
204   }
205
206   /**
207    * {@inheritdoc}
208    */
209   public function automaticallySetThumbnail() {
210     /** @var \Drupal\media_entity\MediaBundleInterface $bundle */
211     if ($this->bundle->entity->getQueueThumbnailDownloads() && $this->isNew()) {
212       $thumbnail_uri = $this->getType()->getDefaultThumbnail();
213     }
214     else {
215       $thumbnail_uri = $this->getType()->thumbnail($this);
216     }
217     $existing = \Drupal::entityQuery('file')
218       ->condition('uri', $thumbnail_uri)
219       ->execute();
220
221     if ($existing) {
222       $this->thumbnail->target_id = reset($existing);
223     }
224     else {
225       /** @var \Drupal\file\FileInterface $file */
226       $file = $this->entityTypeManager()->getStorage('file')->create(['uri' => $thumbnail_uri]);
227       if ($publisher = $this->getPublisher()) {
228         $file->setOwner($publisher);
229       }
230       $file->setPermanent();
231       $file->save();
232       $this->thumbnail->target_id = $file->id();
233     }
234
235     // TODO - We should probably use something smarter (tokens, ...).
236     $this->thumbnail->alt = t('Thumbnail');
237     $this->thumbnail->title = $this->label();
238   }
239
240   /**
241    * {@inheritdoc}
242    */
243   public function preSaveRevision(EntityStorageInterface $storage, \stdClass $record) {
244     parent::preSaveRevision($storage, $record);
245
246     if (!$this->isNewRevision() && isset($this->original) && (!isset($record->revision_log) || $record->revision_log === '')) {
247       // If we are updating an existing node without adding a new revision, we
248       // need to make sure $entity->revision_log is reset whenever it is empty.
249       // Therefore, this code allows us to avoid clobbering an existing log
250       // entry with an empty one.
251       $record->revision_log = $this->original->revision_log->value;
252     }
253   }
254
255   /**
256    * {@inheritdoc}
257    */
258   public function validate() {
259     $this->getType()->attachConstraints($this);
260     return parent::validate();
261   }
262
263   /**
264    * {@inheritdoc}
265    */
266   public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
267     $fields['mid'] = BaseFieldDefinition::create('integer')
268       ->setLabel(t('Media ID'))
269       ->setDescription(t('The media ID.'))
270       ->setReadOnly(TRUE)
271       ->setSetting('unsigned', TRUE);
272
273     $fields['uuid'] = BaseFieldDefinition::create('uuid')
274       ->setLabel(t('UUID'))
275       ->setDescription(t('The media UUID.'))
276       ->setReadOnly(TRUE);
277
278     $fields['vid'] = BaseFieldDefinition::create('integer')
279       ->setLabel(t('Revision ID'))
280       ->setDescription(t('The media revision ID.'))
281       ->setReadOnly(TRUE)
282       ->setSetting('unsigned', TRUE);
283
284     $fields['bundle'] = BaseFieldDefinition::create('entity_reference')
285       ->setLabel(t('Bundle'))
286       ->setDescription(t('The media bundle.'))
287       ->setSetting('target_type', 'media_bundle')
288       ->setReadOnly(TRUE);
289
290     $fields['langcode'] = BaseFieldDefinition::create('language')
291       ->setLabel(t('Language code'))
292       ->setDescription(t('The media language code.'))
293       ->setTranslatable(TRUE)
294       ->setRevisionable(TRUE)
295       ->setDisplayOptions('view', [
296         'type' => 'hidden',
297       ])
298       ->setDisplayOptions('form', [
299         'type' => 'language_select',
300         'weight' => 2,
301       ]);
302
303     $fields['name'] = BaseFieldDefinition::create('string')
304       ->setLabel(t('Media name'))
305       ->setDescription(t('The name of this media.'))
306       ->setRequired(TRUE)
307       ->setTranslatable(TRUE)
308       ->setRevisionable(TRUE)
309       ->setDefaultValue('')
310       ->setSetting('max_length', 255)
311       ->setDisplayOptions('form', [
312         'type' => 'string_textfield',
313         'weight' => -5,
314       ])
315       ->setDisplayConfigurable('form', TRUE)
316       ->setDisplayOptions('view', [
317         'label' => 'hidden',
318         'type' => 'string',
319         'weight' => -5,
320       ])
321       ->setDisplayConfigurable('view', TRUE);
322
323     $fields['thumbnail'] = BaseFieldDefinition::create('image')
324       ->setLabel(t('Thumbnail'))
325       ->setDescription(t('The thumbnail of the media.'))
326       ->setRevisionable(TRUE)
327       ->setDisplayOptions('view', [
328         'type' => 'image',
329         'weight' => 1,
330         'label' => 'hidden',
331         'settings' => [
332           'image_style' => 'thumbnail',
333         ],
334       ])
335       ->setDisplayConfigurable('view', TRUE)
336       ->setReadOnly(TRUE);
337
338     $fields['uid'] = BaseFieldDefinition::create('entity_reference')
339       ->setLabel(t('Publisher ID'))
340       ->setDescription(t('The user ID of the media publisher.'))
341       ->setRevisionable(TRUE)
342       ->setDefaultValueCallback('Drupal\media_entity\Entity\Media::getCurrentUserId')
343       ->setSetting('target_type', 'user')
344       ->setTranslatable(TRUE)
345       ->setDisplayOptions('view', [
346         'label' => 'hidden',
347         'type' => 'author',
348         'weight' => 0,
349       ])
350       ->setDisplayConfigurable('view', TRUE)
351       ->setDisplayOptions('form', [
352         'type' => 'entity_reference_autocomplete',
353         'weight' => 5,
354         'settings' => [
355           'match_operator' => 'CONTAINS',
356           'size' => '60',
357           'autocomplete_type' => 'tags',
358           'placeholder' => '',
359         ],
360       ])
361       ->setDisplayConfigurable('form', TRUE);
362
363     $fields['status'] = BaseFieldDefinition::create('boolean')
364       ->setLabel(t('Publishing status'))
365       ->setDescription(t('A boolean indicating whether the media is published.'))
366       ->setTranslatable(TRUE)
367       ->setRevisionable(TRUE)
368       ->setDefaultValue(TRUE);
369
370     $fields['created'] = BaseFieldDefinition::create('created')
371       ->setLabel(t('Created'))
372       ->setDescription(t('The time that the media was created.'))
373       ->setTranslatable(TRUE)
374       ->setRevisionable(TRUE)
375       ->setDisplayOptions('view', [
376         'label' => 'hidden',
377         'type' => 'timestamp',
378         'weight' => 0,
379       ])
380       ->setDisplayConfigurable('view', TRUE)
381       ->setDisplayOptions('form', [
382         'type' => 'datetime_timestamp',
383         'weight' => 10,
384       ])
385       ->setDisplayConfigurable('form', TRUE);
386
387     $fields['changed'] = BaseFieldDefinition::create('changed')
388       ->setLabel(t('Changed'))
389       ->setDescription(t('The time that the media was last edited.'))
390       ->setTranslatable(TRUE)
391       ->setRevisionable(TRUE);
392
393     $fields['revision_timestamp'] = BaseFieldDefinition::create('created')
394       ->setLabel(t('Revision timestamp'))
395       ->setDescription(t('The time that the current revision was created.'))
396       ->setQueryable(FALSE)
397       ->setRevisionable(TRUE);
398
399     $fields['revision_uid'] = BaseFieldDefinition::create('entity_reference')
400       ->setLabel(t('Revision publisher ID'))
401       ->setDescription(t('The user ID of the publisher of the current revision.'))
402       ->setSetting('target_type', 'user')
403       ->setQueryable(FALSE)
404       ->setRevisionable(TRUE);
405
406     $fields['revision_log'] = BaseFieldDefinition::create('string_long')
407       ->setLabel(t('Revision Log'))
408       ->setDescription(t('The log entry explaining the changes in this revision.'))
409       ->setRevisionable(TRUE)
410       ->setTranslatable(TRUE);
411
412     return $fields;
413   }
414
415   /**
416    * Default value callback for 'uid' base field definition.
417    *
418    * @see ::baseFieldDefinitions()
419    *
420    * @return array
421    *   An array of default values.
422    */
423   public static function getCurrentUserId() {
424     return [\Drupal::currentUser()->id()];
425   }
426
427   /**
428    * {@inheritdoc}
429    */
430   public function getRevisionCreationTime() {
431     return $this->revision_timestamp->value;
432   }
433
434   /**
435    * {@inheritdoc}
436    */
437   public function setRevisionCreationTime($timestamp) {
438     $this->revision_timestamp->value = $timestamp;
439     return $this;
440   }
441
442   /**
443    * {@inheritdoc}
444    */
445   public function getRevisionUser() {
446     return $this->revision_uid->entity;
447   }
448
449   /**
450    * {@inheritdoc}
451    */
452   public function setRevisionUser(UserInterface $account) {
453     $this->revision_uid->entity = $account;
454     return $this;
455   }
456
457   /**
458    * {@inheritdoc}
459    */
460   public function getRevisionUserId() {
461     return $this->revision_uid->target_id;
462   }
463
464   /**
465    * {@inheritdoc}
466    */
467   public function setRevisionUserId($user_id) {
468     $this->revision_uid->target_id = $user_id;
469     return $this;
470   }
471
472   /**
473    * {@inheritdoc}
474    */
475   public function getRevisionLogMessage() {
476     return $this->revision_log->value;
477   }
478
479   /**
480    * {@inheritdoc}
481    */
482   public function setRevisionLogMessage($revision_log_message) {
483     $this->revision_log->value = $revision_log_message;
484     return $this;
485   }
486
487 }