You are here

media.module in D7 Media 8

Contains media.module.

File

media.module
View source
<?php

/**
 * @file
 * Contains media.module.
 */
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\ViewExecutable;

/**
 * Implements hook_help().
 */
function media_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {

    // Main module help for the media module.
    case 'help.page.media':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('Media module for Drupal 8') . '</p>';
      return $output;
    default:
  }
}

/**
 * Implements hook_form_alter().
 */
function media_form_alter(&$form, FormStateInterface &$form_state) {
  if ($form['#form_id'] == 'entity_browser_media_library_form') {

    // Style the submit button.
    $form['actions']['submit']['#attributes']['class'][] = 'button--primary';
    $form['actions']['submit']['#attributes']['class'][] = 'entity-browser-modal-target';
  }

  // Remove the Gallery option from create gallery view's bundle filters.
  if ($form['#id'] == 'views-exposed-form-media-library-gallery-media-select-modal' || $form['#id'] == 'views-exposed-form-media-library-gallery-user-media-select-modal') {
    unset($form['bundle_1']['#options']['gallery']);
  }
  if ($form['#form_id'] == 'media_gallery_form') {
    $form['#attached']['library'][] = 'media/view';
  }
}

/**
 * Implements hook_views_pre_render().
 *
 * Adds the media.view library to the media views.
 */
function media_views_pre_render(ViewExecutable $view) {
  if (isset($view) && ($view->storage
    ->id() == 'media_library' || $view->storage
    ->id() == 'global_media_library')) {
    $view->element['#attached']['library'][] = 'media/view';
  }
}

/**
 * Implements hook_menu_local_actions_alter().
 *
 * Adds the add media button from media_entity on library pages.
 */
function media_menu_local_actions_alter(&$local_actions) {
  $local_actions['media.add']['appears_on'][] = 'view.media_library.user_media_library';
  $local_actions['media.add']['appears_on'][] = 'view.media_library.global_media_library_page';
}

/**
 * Implements hook_entity_type_alter().
 */
function media_entity_type_alter(array &$entity_types) {
  $field_name = \Drupal::config('media_entity.bundle.gallery')
    ->get('type_configuration.source_field');
  $entity_types['media']
    ->addConstraint('GalleryMediaBundle', array(
    'sourceFieldName' => $field_name,
  ));
}

/**
 * Implements hook_menu_links_discovered_alter().
 */
function media_menu_links_discovered_alter(&$links) {

  // Media entity module provides a default view which we disable. Since it also
  // provides a link entry for it we need to update the route there to point to
  // the media library we provide.
  $links['entity.media.collection']['route_name'] = 'view.media_library.global_media_library_page';
}