AudioPlayerHTML5.php in Media entity audio 8
File
src/Plugin/Field/FieldFormatter/AudioPlayerHTML5.php
View source
<?php
namespace Drupal\media_entity_audio\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
class AudioPlayerHTML5 extends AudioPlayerBase {
public static function defaultSettings() {
$settings['provide_download_link'] = TRUE;
$settings['audio_attributes'] = 'controls';
return $settings;
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$form = parent::settingsForm($form, $form_state);
$form['provide_download_link'] = [
'#title' => $this
->t('Provide Download Link'),
'#type' => 'checkbox',
'#default_value' => $this
->getSetting('provide_download_link'),
];
$form['audio_attributes'] = [
'#title' => $this
->t('Audio Tag Attributes'),
'#type' => 'textfield',
'#description' => $this
->t('Give a space-separeted list of values like: <em>\'controls preload="auto" loop\'</em>. Note that if you remove the <em>\'controls\'</em> tag, your player will not show up.'),
'#default_value' => $this
->getSetting('audio_attributes'),
];
return $form;
}
public function settingsSummary() {
$summary = [];
$provide_download_link = $this
->getSetting('provide_download_link');
$audio_attributes = $this
->getSetting('audio_attributes');
if ($provide_download_link) {
$summary[] = $this
->t('Download link provided.');
}
if ($audio_attributes) {
$summary[] = $this
->t('Audio tag attributes: @tags.', [
'@tags' => $audio_attributes,
]);
}
return $summary;
}
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = array();
$provide_download_link = $this
->getSetting('provide_download_link');
$audio_attributes = $this
->getSetting('audio_attributes');
foreach ($this
->getEntitiesToView($items, $langcode) as $delta => $file) {
$item = $file->_referringItem;
$elements[$delta] = array(
'#theme' => 'media_audio_file_formatter',
'#file' => $file,
'#description' => $item->description,
'#value' => $provide_download_link,
'#extravalue' => $audio_attributes,
'#cache' => array(
'tags' => $file
->getCacheTags(),
),
);
if (isset($item->_attributes)) {
$elements[$delta] += array(
'#attributes' => array(),
);
$elements[$delta]['#attributes'] += $item->_attributes;
unset($item->_attributes);
}
}
return $elements;
}
}