degov-8.x-2.0/modules/degov_media_image/degov_media_image.module
modules/degov_media_image/degov_media_image.module
<?php
use Drupal\degov_common\Common;
/**
* Implements hook_preprocess().
*/
function degov_media_image_preprocess(&$variables, $hook, &$info) {
// Add template suggestions and libraries implemented in this module.
Common::addThemeSuggestions($variables, $hook, $info, [
'module_name' => 'degov_media_image',
'entity_type' => 'media',
'entity_bundles' => ['image'],
'entity_view_modes' => ['thumbnail', 'usage', 'preview', 'search', 'full', 'embedded'],
]);
}
/**
* Implements hook_preprocess_type().
*/
function degov_media_image_preprocess_media__image(&$variables) {
/** @var Drupal\media\Entity\Media $media */
$media = $variables['elements']['#media'];
$variables['created'] = $media->created->value;
$variables['image_url'] = $media->url();
// Add the download link only to full view mode.
if (in_array($variables['elements']['#view_mode'], ['full', 'default'])) {
// Add media type name to display.
$bundle_definition = $media->getSource()->getPluginDefinition();
$variables['content']['media_type_label'] = $bundle_definition['label'];
// 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 allowed let's prepare the links.
if ($download_is_allowed) {
// First check if the image is there.
if (!$media->get('image')->isEmpty()) {
/** @var \Drupal\file\FileInterface $sourceFile */
$sourceFile = $media->get('image')->entity;
$variables['content']['download'] = degov_common_file_download_render_array($sourceFile);
}
else {
$variables['content']['download'] = [];
}
}
}
}
