drowl_media-8.x-2.0-rc0/modules/drowl_media_types/drowl_media_types.module
modules/drowl_media_types/drowl_media_types.module
<?php
/**
* @file
* DROWL media types module file.
*/
use Drupal\Core\Template\Attribute;
use Drupal\Component\Serialization\Json;
/**
* Implements hook_form_alter().
*/
function drowl_media_types_form_alter(&$form, &$form_state, $form_id) {
// Media entity form alter:
if (in_array($form_id, ['media_slide_form', 'media_slide_edit_form'])) {
// Make field dependent on field_overlay_position value:
$form['field_overlay_sizing']['#states'] = [
'invisible' => [
':input[name="field_overlay_position"]' => ['value' => 'disabled'],
],
];
$form['field_overlay_sizing_md']['#states'] = [
'invisible' => [
':input[name="field_overlay_position_md"]' => ['value' => 'disabled'],
],
];
$form['field_overlay_sizing_lg']['#states'] = [
'invisible' => [
':input[name="field_overlay_position_lg"]' => ['value' => 'disabled'],
],
];
}
}
/**
* Implements hook_preprocess_media().
*
* @todo Where is this hook coming from?
*/
function drowl_media_types_preprocess_media(&$variables) {
if (!empty($variables['media'])) {
$media = $variables['media'];
// Preprocess certain media bundle types:
$type = $media->bundle();
switch ($type) {
case 'document':
_drowl_media_types_preprocess_media_document($variables);
break;
case 'slideshow':
_drowl_media_types_preprocess_media_slideshow($variables);
break;
}
}
}
/**
* Preprocess helper function for media document.
*
* @param array $variables
* The media variables.
*/
function _drowl_media_types_preprocess_media_document(array &$variables) {
$variables['media_link_attributes'] = new Attribute();
if (!empty($variables['elements']['field_media_document'][0]['#options']['attributes'])) {
foreach ($variables['elements']['field_media_document'][0]['#options']['attributes'] as $link_field_attribute_key => $link_field_attribute_value) {
$variables['media_link_attributes']->setAttribute($link_field_attribute_key, $link_field_attribute_value);
}
}
}
/**
* Preprocess helper funcction for media slideshow.
*
* @param array $variables
* The media variables.
*/
function _drowl_media_types_preprocess_media_slideshow(array &$variables) {
$media = $variables['media'];
$options_override = [];
if ($media->hasField('field_slide_autoplay')) {
$autoplay = $media->get('field_slide_autoplay')->value;
if ($autoplay !== NULL && $autoplay != 'default') {
$options_override['autoplay'] = (bool) $autoplay;
}
// Else uses defaults set in media slide configuration.
}
if ($media->hasField('field_slide_arrows')) {
$arrows = $media->get('field_slide_arrows')->value;
if ($arrows !== NULL && $arrows != 'default') {
$options_override['arrows'] = (bool) $arrows;
}
// Else uses defaults set in media slide configuration.
}
if ($media->hasField('field_slide_dots')) {
$dots = $media->get('field_slide_dots')->value;
if ($dots !== NULL && $dots != 'default') {
$options_override['dots'] = (bool) $dots;
}
// Else uses defaults set in media slide configuration.
}
if ($media->hasField('field_slide_infinite')) {
$infinite = $media->get('field_slide_infinite')->value;
if ($infinite !== NULL && $infinite != 'default') {
$options_override['infinite'] = (bool) $infinite;
}
// Else uses defaults set in media slide configuration.
}
// Attach Library DROWL Media Slideshow library (not slick)
$variables['#attached']['library'][] = 'drowl_media_types/slideshow';
// Set the values in the field to make them available to later hook
// "drowl_paragraphs_types_preprocess_slick" where it can be set.
if (!empty($variables['elements']['field_media_slides_ref']['#build']['optionset'])) {
$optionset = $variables['elements']['field_media_slides_ref']['#build']['optionset'];
$optionset->set('drowl_media_slideshow_options_override', $options_override);
}
}
/**
* Implements hook_preprocess_HOOK().
*
* Required as follow-up to _drowl_media_types_preprocess_media_slideshow().
* IMPORTANT: This curiously runs AFTER template_preprocess_slick
* in the slick module. So we can't prepare values.
*
* @throws \Exception
*/
function drowl_media_types_preprocess_slick(&$variables) {
if ($variables['settings']['optionset'] == 'media_slideshow') {
// Only for paragraphs_slide_container slideshows:
$data_slick = $variables['content_attributes']->offsetGet('data-slick');
$options_override = $variables['element']['#optionset']->get('drowl_media_slideshow_options_override');
// Get the overrides:
if (!empty($data_slick) && (!empty($options_override))) {
// This has overrides.
// Get the configuration from the JSON array:
$data_slick = Json::decode($data_slick->value());
if (!empty($options_override)) {
// Set the general option overrides:
$data_slick = array_merge($data_slick, $options_override);
// We also have to override this in each breakpoint to make it work:
if (!empty($data_slick['responsive'])) {
foreach ($data_slick['responsive'] as $key => $responsive) {
if (!empty($responsive['settings'])) {
$data_slick['responsive'][$key]['settings'] = array_merge($responsive['settings'], $options_override);
}
}
}
}
// Re-set the data-slick variable:
// A separate hook would be better but doesn't seem to exist:
// see https://www.drupal.org/project/slick/issues/2944330
$variables['content_attributes']->setAttribute('data-slick', Json::encode($data_slick));
}
}
}
/**
* Implements hook_theme_suggestions_HOOK_alter().
*/
function drowl_media_types_theme_suggestions_media_alter(&$suggestions, $variables) {
$entity = $variables['elements']['#media'];
$sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_');
$type = $entity->bundle();
$suggestions[] = 'media__drowl_media_types';
$suggestions[] = 'media__drowl_media_types__' . $sanitized_view_mode;
$suggestions[] = 'media__drowl_media_types__' . $type;
$suggestions[] = 'media__drowl_media_types__' . $type . '__' . $sanitized_view_mode;
}
/**
* Implements hook_theme().
*/
function drowl_media_types_theme($existing, $type, $theme, $path) {
$templates = $path . '/templates';
return [
'media__drowl_media_types' => [
'base hook' => 'media',
'path' => $templates,
],
'media__drowl_media_types__document__button' => [
'base hook' => 'media',
'path' => $templates,
],
'media__drowl_media_types__document__card' => [
'base hook' => 'media',
'path' => $templates,
],
'media__drowl_media_types__document__media_object' => [
'base hook' => 'media',
'path' => $templates,
],
'media__drowl_media_types__document__tile' => [
'base hook' => 'media',
'path' => $templates,
],
'media__drowl_media_types__document' => [
'base hook' => 'media',
'path' => $templates,
],
'media__drowl_media_types__slide' => [
'base hook' => 'media',
'path' => $templates,
],
'media__drowl_media_types__slideshow' => [
'base hook' => 'media',
'path' => $templates,
],
'media__drowl_media_types__video' => [
'base hook' => 'media',
'path' => $templates,
],
'media__drowl_media_types__remote_video' => [
'base hook' => 'media',
'path' => $templates,
],
'media__drowl_media_types__vector_image' => [
'base hook' => 'media',
'path' => $templates,
],
// Restore the claro/gin media templates for the media library
// We need to override each bundle because of the suggestions order.
'media__drowl_media_types__media_library' => [
'base hook' => 'media',
'path' => $templates . '/media-library',
],
'media__drowl_media_types__image__media_library' => [
'base hook' => 'media',
'path' => $templates . '/media-library',
],
'media__drowl_media_types__slideshow__media_library' => [
'base hook' => 'media',
'path' => $templates . '/media-library',
],
'media__drowl_media_types__slide__media_library' => [
'base hook' => 'media',
'path' => $templates . '/media-library',
],
'media__drowl_media_types__video__media_library' => [
'base hook' => 'media',
'path' => $templates . '/media-library',
],
'media__drowl_media_types__remote_video__media_library' => [
'base hook' => 'media',
'path' => $templates . '/media-library',
],
'media__drowl_media_types__document__media_library' => [
'base hook' => 'media',
'path' => $templates . '/media-library',
],
'media__drowl_media_types__vector_image__media_library' => [
'base hook' => 'media',
'path' => $templates . '/media-library',
],
];
}
