degov-8.x-2.0/modules/degov_media_video/degov_media_video.module
modules/degov_media_video/degov_media_video.module
<?php
use Drupal\degov_common\Common;
/**
* Implements hook_preprocess().
*/
function degov_media_video_preprocess(&$variables, $hook, &$info) {
// Add template suggestions and libraries implemented in this module.
Common::addThemeSuggestions($variables, $hook, $info, [
'module_name' => 'degov_media_video',
'entity_type' => 'media',
'entity_bundles' => ['video'],
'entity_view_modes' => ['usage', 'preview', 'search', 'embedded', 'reference'],
]);
}
/**
* Implements hook_preprocess_HOOK().
*/
function degov_media_video_preprocess_media__video(&$variables) {
/* @var \Drupal\media\Entity\Media $media */
$media = $variables['elements']['#media'];
$variables['video_url'] = $media->url();
$variables['created'] = $variables['elements']['#media']->created->value;
$variables['content']['video_date'] = format_date($variables['elements']['#media']->created->value, 'date_medium');
}
/**
* Implements hook_ENTITY_TYPE_presave().
*/
function degov_media_video_media_presave(Drupal\Core\Entity\EntityInterface $entity) {
$bundle = $entity->bundle();
if ($bundle == 'video' && !$entity->get('field_media_video_embed_field')->isEmpty()) {
// 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]]);
}
}
}
