drowl_media-8.x-2.0-rc0/drowl_media.module
drowl_media.module
<?php
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Url;
/**
* @file
* DROWL media module file.
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\ViewExecutable;
/**
* @file
* DROWL media enhancements.
*/
/**
* Implements template_preprocess_media().
*/
function drowl_media_preprocess_media(array &$variables) {
// Attach frontend libraries by bundle
if(!empty($variables['elements']['#media'])){
$variables['#attached']['library'][] = 'drowl_media/global';
$bundle = $variables['elements']['#media']->bundle();
if($bundle == 'document'){
$variables['#attached']['library'][] = 'drowl_media/bundle_documents';
}
if($bundle == 'remote_video' || $bundle == 'video'){
$variables['#attached']['library'][] = 'drowl_media/bundle_video';
}
if($bundle == 'slideshow'){
$variables['#attached']['library'][] = 'drowl_media/bundle_slideshow';
}
}
}
/**
* Implements hook_page_attachments().
*/
function drowl_media_page_attachments(array &$attachments) {
$isAdminRoute = \Drupal::service('router.admin_context')->isAdminRoute();
if ($isAdminRoute) {
$attachments['#attached']['library'][] = 'drowl_media/admin';
}
}
/**
* Implements hook_library_info_alter()
*/
function drowl_media_library_info_alter(&$libraries, $extension) {
// Only attach our VBO-extending library, if views_bulk_operations is enabled and the views_bulk_operations/frontUi library is actually used.
if ($extension == 'views_bulk_operations' && isset($libraries['frontUi'])) {
// Extend views_bulk_operations/adminUi
$libraries['frontUi']['dependencies'][] = 'drowl_media/admin_views_bulk_operations';
}
}
/**
* Implements hook_form_alter().
*/
function drowl_media_form_alter(&$form, FormStateInterface $form_state, $form_id) {
// Only allow a subset of image styles in the media embed widget
// for image preview.
if (strpos($form_id, 'entity_embed_dialog') === 0) {
if (!empty($form['attributes']['data-entity-embed-display-settings']['image_style'])) {
$image_styles = $form['attributes']['data-entity-embed-display-settings']['image_style']['#options'];
$prefered_image_styles = [
'page_width_25_lg_scale' => t('25% page width scaled'),
'page_width_50_lg_scale' => t('50% page width scaled'),
'page_width_25_lg' => t('25% page width cropped'),
'page_width_50_lg' => t('50% page width cropped'),
];
$allowed_image_styles = array_intersect_key($image_styles, $prefered_image_styles);
if (!empty($allowed_image_styles)) {
// If at least one prefered style exists, only use the prefered ones:
$form['attributes']['data-entity-embed-display-settings']['image_style']['#options'] = $allowed_image_styles;
}
}
}
}
/**
* Implements hook_views_pre_render().
*/
function drowl_media_views_pre_render(ViewExecutable $view) {
$admin_theme = \Drupal::config('system.theme')->get('admin');
if (isset($view) && ($view->storage->id() == 'media_library')) {
// Attach on view:
if ($admin_theme == 'claro') {
$view->element['#attached']['library'][] = 'drowl_media/admin_media_library_claro';
}
else {
$view->element['#attached']['library'][] = 'drowl_media/admin_media_library';
}
}
}
/**
* Implements hook_field_widget_single_element_form_alter().
*/
function drowl_media_field_widget_single_element_form_alter(&$element, FormStateInterface $form_state, $context) {
$admin_theme = \Drupal::config('system.theme')->get('admin');
if (!empty($context['widget']) && in_array($context['widget']->getPluginId(), [
'media_library_widget',
])) {
// Attach in media library widget:
if ($admin_theme == 'claro') {
$element['#attached']['library'][] = 'drowl_media/admin_media_library_claro';
}
else {
$element['#attached']['library'][] = 'drowl_media/admin_media_library';
}
}
}
/**
* Implements hook_ENTITY_TYPE_view().
*/
function drowl_media_media_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
// Attach the media library to all media previews in the admin route
if (!empty($build) && !empty($view_mode) && $view_mode == 'media_library') {
$build['#attached']['library'][] = 'drowl_media/admin_media_library';
}
}
/**
* Implements hook_field_widget_single_element_media_library_widget_form_alter().
*
* Adds a "Create new Slideshow" button beneath "Select slideshow" media library
* widget button. This is a *workaround* to provide a link for slideshow creation
* as "Slideshow" is a custom media source, which doesn't provide a create itself.
*/
function drowl_media_field_widget_single_element_media_library_widget_form_alter(&$element, FormStateInterface $form_state, $context) {
if (!empty($element['#target_bundles']) && in_array("slideshow", $element['#target_bundles'])) {
// Change the regular "Add"-label, so its clear to the user that this means
// "Reference to existing Slideshows"
$element["open_button"]["#value"] = t('Select Slideshow');
// Add further "Create Slideshow"-Button, opens the Slideshow create form in a new tab
$element['#field_suffix']['empty_selection'] = [
'#type' => 'link',
'#title' => t('Create new Slideshow'),
'#url' => Url::fromRoute('entity.media.add_form', ['media_type' => 'slideshow']),
'#attributes' => [
'target' => '_blank',
'class' => ['button', 'primary'],
// Preparation to open the dialog in a modal, but as we can not
// simply set the created slideshow, we didn't proceed here yet:
// 'class' => ['button', 'primary', 'use-ajax'],
// 'data-dialog-type' => 'modal',
// 'data-dialog-options' => '{width: 90%, height: 80%}',
],
'#weight' => 99,
];
}
}
