Audio.php in Media entity audio 8
File
src/Plugin/MediaEntity/Type/Audio.php
View source
<?php
namespace Drupal\media_entity_audio\Plugin\MediaEntity\Type;
use Drupal\media_entity\MediaInterface;
use Drupal\media_entity\MediaTypeBase;
use Drupal\Core\Form\FormStateInterface;
class Audio extends MediaTypeBase {
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$bundle = $form_state
->getFormObject()
->getEntity();
$options = [];
$allowed_field_types = [
'file',
];
foreach ($this->entityFieldManager
->getFieldDefinitions('media', $bundle
->id()) as $field_name => $field) {
if (in_array($field
->getType(), $allowed_field_types) && !$field
->getFieldStorageDefinition()
->isBaseField()) {
$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 Audio file. 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 providedFields() {
return [];
}
public function getField(MediaInterface $media, $name) {
return FALSE;
}
public function getDefaultThumbnail() {
return $this->config
->get('icon_base') . '/image.png';
}
public function thumbnail(MediaInterface $media) {
$source_field = $this->configuration['source_field'];
if ($file = $media->{$source_field}->entity) {
return $this->config
->get('icon_base') . '/image.png';
}
return $this
->getDefaultThumbnail();
}
public function getDefaultName(MediaInterface $media) {
$source_field = $this->configuration['source_field'];
if (!empty($source_field) && ($file = $media->{$source_field}->entity)) {
return $file
->getFilename();
}
return parent::getDefaultName($media);
}
}
Classes
Name |
Description |
Audio |
Provides media type plugin for Audio. |