Slideshow.php in Media entity slideshow 8
File
src/Plugin/MediaEntity/Type/Slideshow.php
View source
<?php
namespace Drupal\media_entity_slideshow\Plugin\MediaEntity\Type;
use Drupal\media_entity\MediaInterface;
use Drupal\media_entity\MediaTypeBase;
use Drupal\Core\Form\FormStateInterface;
class Slideshow extends MediaTypeBase {
public function providedFields() {
$fields = array(
'length' => $this
->t('Slideshow length'),
);
return $fields;
}
public function getField(MediaInterface $media, $name) {
$source_field = $this->configuration['source_field'];
switch ($name) {
case 'length':
return $media->{$source_field}
->count();
}
return FALSE;
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$bundle = $form_state
->getFormObject()
->getEntity();
$options = [];
$allowed_field_types = [
'entity_reference',
];
foreach ($this->entityFieldManager
->getFieldDefinitions('media', $bundle
->id()) as $field_name => $field) {
if (in_array($field
->getType(), $allowed_field_types)) {
$storage = $field
->getFieldStorageDefinition();
if (!$storage
->isBaseField() && $storage
->getSetting('target_type') == 'media') {
$options[$field_name] = $field
->getLabel();
}
}
}
$form['source_field'] = [
'#type' => 'select',
'#title' => $this
->t('Field with source information'),
'#description' => $this
->t('Field on media entity that stores slideshow items. You can create a bundle without selecting a value for this dropdown initially. This dropdown can be populated after adding fields to the bundle.'),
'#default_value' => empty($this->configuration['source_field']) ? NULL : $this->configuration['source_field'],
'#options' => $options,
];
return $form;
}
public function attachConstraints(MediaInterface $media) {
parent::attachConstraints($media);
$source_field_name = $this->configuration['source_field'];
$media
->getTypedData()
->getDataDefinition()
->addConstraint('ItemsCount', array(
'sourceFieldName' => $source_field_name,
));
}
public function getDefaultThumbnail() {
return $this->config
->get('icon_base') . '/slideshow.png';
}
public function thumbnail(MediaInterface $media) {
$source_field = $this->configuration['source_field'];
$slideshow_item = $this->entityTypeManager
->getStorage('media')
->load($media->{$source_field}->target_id);
if (!$slideshow_item) {
return $this
->getDefaultThumbnail();
}
$bundle = $this->entityTypeManager
->getStorage('media_bundle')
->load($slideshow_item
->bundle());
if (!$bundle) {
return $this
->getDefaultThumbnail();
}
$thumbnail = $bundle
->getType()
->thumbnail($slideshow_item);
if (!$thumbnail) {
return $this
->getDefaultThumbnail();
}
return $thumbnail;
}
public function getDefaultName(MediaInterface $media) {
$length = $this
->getField($media, 'length');
if (!empty($length)) {
return $this
->formatPlural($length, '1 slide, created on @date', '@count slides, created on @date', [
'@date' => \Drupal::service('date.formatter')
->format($media
->getCreatedTime(), 'custom', 'd/M/Y - H:i:s'),
]);
}
return parent::getDefaultName($media);
}
}
Classes
Name |
Description |
Slideshow |
Provides media type plugin for Slideshows. |