AudioStreamHTML5.php in Media entity audio 8.3
File
src/Plugin/Field/FieldFormatter/AudioStreamHTML5.php
View source
<?php
namespace Drupal\media_entity_audio\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Field\FormatterBase;
class AudioStreamHTML5 extends FormatterBase {
public static function defaultSettings() {
$settings['controls'] = TRUE;
return $settings;
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$form = parent::settingsForm($form, $form_state);
$form['controls'] = [
'#title' => $this
->t('Display controls'),
'#type' => 'checkbox',
'#default_value' => $this
->getSetting('controls'),
];
return $form;
}
public function settingsSummary() {
$summary = parent::settingsSummary();
if ($this
->getSetting('controls')) {
$summary[] = $this
->t('Audio controls displayed.');
}
else {
$summary[] = $this
->t('Audio controls not displayed.');
}
return $summary;
}
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
foreach ($items as $delta => $item) {
$elements[$delta] = [
'#theme' => 'media_audio',
'#sources' => [
$item->uri,
],
'#controls' => $this
->getSetting('controls'),
];
if (isset($item->_attributes)) {
$elements[$delta] += [
'#attributes' => [],
];
$elements[$delta]['#attributes'] += $item->_attributes;
unset($item->_attributes);
}
}
return $elements;
}
}