degov-8.x-2.0/modules/degov_media_audio/degov_media_audio.module
modules/degov_media_audio/degov_media_audio.module
<?php
use Drupal\degov_common\Common;
use Drupal\Core\Template\Attribute;
/**
* Implements hook_preprocess().
*/
function degov_media_audio_preprocess(&$variables, $hook, &$info) {
// Add template suggestions and libraries implemented in this module.
Common::addThemeSuggestions($variables, $hook, $info, [
'module_name' => 'degov_media_audio',
'entity_type' => 'media',
'entity_bundles' => ['audio'],
'entity_view_modes' => ['usage', 'preview', 'search', 'embedded', 'reference'],
]);
}
/**
* Implements hook_preprocess_HOOK().
*/
function degov_media_audio_preprocess_media__audio(&$variables) {
/* @var \Drupal\media\Entity\Media $media */
$media = $variables['elements']['#media'];
$variables['audio_url'] = $media->url();
$variables['audio_attributes'] = new Attribute([
'id' => 'audio-' . $media->id(),
'controls' => 'controls',
]);
$variables['content']['audio_date'] = format_date($variables['elements']['#media']->created->value, 'date_medium');
// Check if the download of the video is allowed by media settings.
$download_is_allowed = FALSE;
if ($media->hasField('field_allow_download') && !$media->get('field_allow_download')->isEmpty()) {
$download_is_allowed = $media->get('field_allow_download')->value ? TRUE : FALSE;
}
if (!$media->get('field_audio_mp3')->isEmpty()) {
/* @var \Drupal\file\Entity\File $file */
$file = $media->field_audio_mp3->entity;
if (!empty($file)) {
$variables['audio_mp3'] = file_create_url($file->getFileUri());
if (in_array($variables['elements']['#view_mode'], ['full', 'default'])) {
// If allowed let's prepare the links.
if ($download_is_allowed) {
$variables['content']['download_mp3'] = degov_common_file_download_render_array($file);
}
}
}
}
if (!$media->get('field_audio_ogg')->isEmpty()) {
/* @var \Drupal\file\Entity\File $file */
$file = $media->field_audio_ogg->entity;
if (!empty($file)) {
$variables['audio_ogg'] = file_create_url($file->getFileUri());
if (in_array($variables['elements']['#view_mode'], ['full', 'default'])) {
// If allowed let's prepare the links.
if ($download_is_allowed) {
$variables['content']['download_ogg'] = degov_common_file_download_render_array($file);
}
}
}
}
$variables['created'] = $variables['elements']['#media']->created->value;
}
/**
* Implements hook_ENTITY_TYPE_presave().
*/
function degov_media_audio_media_presave(Drupal\Core\Entity\EntityInterface $entity) {
$bundle = $entity->bundle();
if ($bundle == 'audio') {
// Check if the field for duration is enabled for the media bundle and that it is empty.
// There is no need to check the duration again and again if it is already set.
if ($entity->hasField('field_media_duration') && $entity->get('field_media_duration')->isEmpty()) {
/** @var \Drupal\degov_common\VideoUtils $videoUtils */
$videoUtils = \Drupal::service('degov_common.video_utils');
$duration = $videoUtils->getVideoDuration($entity);
$entity->set('field_media_duration', [0 => ['value' => $duration]]);
}
}
}
