thunder_media.module in Thunder 8.2
Same filename and directory in other branches
Contains media related functions.
File
modules/thunder_media/thunder_media.moduleView source
<?php
/**
* @file
* Contains media related functions.
*/
use Drupal\file\Entity\File;
use Drupal\Core\Form\FormStateInterface;
/**
* Implements hook_ENTITY_TYPE_create().
*/
function thunder_media_file_create(File $entity) {
$config = \Drupal::config('thunder_media.settings');
if ($config
->get('enable_filename_transliteration')) {
$pathinfo = pathinfo($entity
->getFilename());
/** @var \Drupal\pathauto\AliasCleaner $aliasCleaner */
$aliasCleaner = \Drupal::service('pathauto.alias_cleaner');
// Check for needed pathinfo array keys.
if (!empty($pathinfo['filename']) && !empty($pathinfo['extension'])) {
$cleanFilename = $aliasCleaner
->cleanString($pathinfo['filename']) . '.' . $pathinfo['extension'];
$entity
->setFilename($cleanFilename);
}
}
}
/**
* Implements hook_preprocess_views_view().
*/
function thunder_media_preprocess_views_view(&$variables) {
$thunder_media_browser = [
'image_browser',
'video_browser',
'riddle_browser',
// TODO: Legacy support, remove it with new major version.
'media_browser',
'gallery_browser',
];
if (in_array($variables['view']
->id(), $thunder_media_browser)) {
$variables['view_array']['#attached']['library'][] = 'thunder_media/entity_browser_view';
}
}
/**
* Implements hook_page_attachments().
*/
function thunder_media_page_attachments(array &$page) {
$page['#attached']['library'][] = 'thunder_media/gallery';
}
/**
* Implements hook_menu_local_actions_alter().
*/
function thunder_media_menu_local_actions_alter(&$local_actions) {
$view = \Drupal::entityTypeManager()
->getStorage('view')
->load('thunder_media');
if ($view) {
$local_actions['media.add']['appears_on'][] = 'view.thunder_media.media_page_list';
}
}
/**
* Implements hook_menu_links_discovered_alter().
*/
function thunder_media_menu_links_discovered_alter(&$links) {
$view = \Drupal::entityTypeManager()
->getStorage('view')
->load('thunder_media');
if ($view) {
$links['entity.media.collection']['route_name'] = 'view.thunder_media.media_page_list';
}
}
/**
* Implements hook_field_widget_WIDGET_ID_form_alter().
*
* Alter the widget to apply classes and identify widget buttons and their types
* for the theme.
*/
function thunder_media_field_widget_entity_browser_entity_reference_form_alter(&$element, FormStateInterface $form_state, $context) {
/* @var \Drupal\Core\Field\EntityReferenceFieldItemList $items */
$items = $context['items'];
$field_name = $items
->getName();
switch ($field_name) {
case 'field_teaser_media':
case 'field_media_images':
_thunder_media_media_field_widget_form_alter_helper($element, 'image');
break;
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function thunder_media_form_entity_browser_multiple_image_browser_form_alter(&$form, FormStateInterface $form_state) {
$form['#attached']['library'][] = 'thunder_media/entity_browser_view';
}
/**
* Implements hook_field_widget_WIDGET_ID_form_alter().
*
* Add process function to hide 'remove' button of image field widget on inline
* entity forms.
*/
function thunder_media_field_widget_image_focal_point_form_alter(&$element, FormStateInterface $form_state, $context) {
$config = \Drupal::config('thunder_media.settings');
if (!$config
->get('enable_filefield_remove_button')) {
$bundle = NULL;
/** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display */
if (!empty($form_state
->getStorage()['form_display']) && ($form_display = $form_state
->getStorage()['form_display']) && $form_display
->getTargetEntityTypeId() == 'media') {
$bundle = $form_display
->getTargetBundle();
}
elseif (isset($context['form']['#type']) && $context['form']['#type'] == 'inline_entity_form' && $context['form']['#entity_type'] == 'media') {
$bundle = $context['form']['#bundle'];
}
if ($bundle) {
/** @var \Drupal\media_entity\MediaBundleInterface $bundle */
$bundle = \Drupal::entityTypeManager()
->getStorage('media_bundle')
->load($bundle);
if ($bundle
->getTypeConfiguration()['source_field'] == $element['#field_name']) {
$element['#process'][] = 'thunder_media_inline_entity_form_image_widget_process';
}
}
}
}
/**
* Process function to hide 'Remove' button on image widget forms.
*/
function thunder_media_inline_entity_form_image_widget_process($element, FormStateInterface $form_state, $form) {
if (isset($element['remove_button'])) {
$element['remove_button']['#access'] = FALSE;
}
return $element;
}
/**
* Implements hook_inline_entity_form_entity_form_alter().
*/
function thunder_media_inline_entity_form_entity_form_alter(&$entity_form, FormStateInterface $form_state) {
if (isset($entity_form['field_media_images'])) {
$entity_form['field_media_images']['#attributes']['class'][] = 'media-form__items-widget-container';
}
}
/**
* Implements hook_form_FORM_ID_alter().
*
* Adds classes to theme media:gallery edit form.
*/
function thunder_media_form_media_gallery_edit_form_alter(&$form, FormStateInterface $form_state) {
if (isset($form['field_media_images'])) {
$form['field_media_images']['#attributes']['class'][] = 'media-form__items-widget-container';
}
}
/**
* Implements hook_form_FORM_ID_alter().
*
* Adds classes to theme media:gallery form.
*/
function thunder_media_form_media_gallery_form_alter(&$form, FormStateInterface $form_state) {
if (isset($form['field_media_images'])) {
$form['field_media_images']['#attributes']['class'][] = 'media-form__items-widget-container';
}
}
/**
* Helper function for form alter hooks to alter the media field widget.
*
* @param array $widget
* The media form field widget.
* @param string $media_type
* The type of media.
*/
function _thunder_media_media_field_widget_form_alter_helper(array &$widget, $media_type) {
// Process the media field widget.
$widget['#attributes']['class'][] = 'media-form__items-widget';
if (isset($widget['current']['items'])) {
if (count($widget['current']['items']) == 1) {
$widget['current']['#attributes']['class'][] = 'entities-list--single-item';
}
foreach ($widget['current']['items'] as &$media_item) {
$media_item['#attributes']['class'][] = 'media-form__item-widget';
$media_item['#attributes']['class'][] = 'media-form__item-widget--' . $media_type;
if (isset($media_item['remove_button'])) {
$media_item['remove_button']['#attributes']['class'][] = 'media-form__item-widget__action';
$media_item['remove_button']['#attributes']['class'][] = 'media-form__item-widget__action--remove';
// Add our own "button action type" key to be able to use a distinct
// template via suggestions in the theme.
$media_item['remove_button']['#button_action_type'] = 'media_remove';
}
if (isset($media_item['edit_button'])) {
$media_item['edit_button']['#attributes']['class'][] = 'media-form__item-widget__action';
$media_item['edit_button']['#attributes']['class'][] = 'media-form__item-widget__action--edit';
// Add our own "button action type" key to be able to use a distinct
// template via suggestions in the theme.
$media_item['edit_button']['#button_action_type'] = 'media_edit';
}
}
}
}