You are here

thunder_media.module in Thunder 8.5

Contains media related functions.

File

modules/thunder_media/thunder_media.module
View 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 = [
    'entity_browser',
    // TODO: Legacy support, remove it with new major version.
    'image_browser',
    'video_browser',
    'riddle_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_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_image':
    case 'field_video':
    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')) {
    $type = 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') {
      $type = $form_display
        ->getTargetBundle();
    }
    elseif (isset($context['form']['#type']) && $context['form']['#type'] == 'inline_entity_form' && $context['form']['#entity_type'] == 'media') {
      $type = $context['form']['#bundle'];
    }
    if ($type) {

      /** @var \Drupal\media\MediaTypeInterface $type */
      $type = \Drupal::entityTypeManager()
        ->getStorage('media_type')
        ->load($type);
      if ($type
        ->get('source_configuration')['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';
      }
    }
  }
}