media_library.module 10.1 KB
Newer Older
1 2 3 4 5 6 7
<?php

/**
 * @file
 * Contains hook implementations for the media_library module.
 */

8
use Drupal\Component\Utility\UrlHelper;
9
use Drupal\Component\Serialization\Json;
10 11 12
use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
13
use Drupal\Core\Routing\RouteMatchInterface;
14
use Drupal\Core\Template\Attribute;
15
use Drupal\Core\Url;
16
use Drupal\views\Form\ViewsForm;
17
use Drupal\views\Plugin\views\cache\CachePluginBase;
18 19
use Drupal\views\Plugin\views\query\QueryPluginBase;
use Drupal\views\Plugin\views\query\Sql;
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
use Drupal\views\ViewExecutable;

/**
 * Implements hook_help().
 *
 * @todo Update in https://www.drupal.org/project/drupal/issues/2964789
 */
function media_library_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.media_library':
      $output = '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('The Media library module overrides the /admin/content/media view to provide a rich visual interface for performing administrative operations on media. For more information, see the <a href=":media">online documentation for the Media library module</a>.', [':media' => 'https://www.drupal.org/docs/8/core/modules/media']) . '</p>';
      return $output;
  }
}

/**
 * Implements hook_theme().
 */
function media_library_theme() {
  return [
    'media__media_library' => [
      'base hook' => 'media',
    ],
  ];
}

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
/**
 * Implements hook_preprocess_view().
 *
 * Adds a link to add media above the view.
 */
function media_library_preprocess_views_view(&$variables) {
  $view = $variables['view'];
  if ($view->id() === 'media_library' && $view->current_display === 'widget') {
    $url = Url::fromRoute('media_library.upload');
    if ($url->access()) {
      $url->setOption('query', \Drupal::request()->query->all());
      $variables['header']['add_media'] = [
        '#type' => 'link',
        '#title' => t('Add media'),
        '#url' => $url,
        '#attributes' => [
          'class' => ['button', 'button-action', 'button--primary', 'use-ajax'],
          'data-dialog-type' => 'modal',
          'data-dialog-options' => Json::encode([
            'dialogClass' => 'media-library-widget-modal',
            'height' => '75%',
            'width' => '75%',
            'title' => t('Add media'),
          ]),
        ],
      ];
    }
  }
}

77 78 79 80 81 82
/**
 * Implements hook_views_post_render().
 */
function media_library_views_post_render(ViewExecutable $view, &$output, CachePluginBase $cache) {
  if ($view->id() === 'media_library') {
    $output['#attached']['library'][] = 'media_library/view';
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
    if ($view->current_display === 'widget') {
      $query = array_intersect_key(\Drupal::request()->query->all(), array_flip([
        'media_library_widget_id',
        'media_library_allowed_types',
        'media_library_remaining',
      ]));
      // If the current query contains any parameters we use to contextually
      // filter the view, ensure they persist across AJAX rebuilds.
      // The ajax_path is shared for all AJAX views on the page, but our query
      // parameters are prefixed and should not interfere with any other views.
      // @todo Rework or remove this in https://www.drupal.org/node/2983451
      if (!empty($query)) {
        $ajax_path = &$output['#attached']['drupalSettings']['views']['ajax_path'];
        $parsed_url = UrlHelper::parse($ajax_path);
        $query = array_merge($query, $parsed_url['query']);
        $ajax_path = $parsed_url['path'] . '?' . UrlHelper::buildQuery($query);
        if (isset($query['media_library_remaining'])) {
          $output['#attached']['drupalSettings']['media_library']['selection_remaining'] = (int) $query['media_library_remaining'];
        }
      }
    }
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
  }
}

/**
 * Implements hook_preprocess_media().
 */
function media_library_preprocess_media(&$variables) {
  if ($variables['view_mode'] === 'media_library') {
    /** @var \Drupal\media\MediaInterface $media */
    $media = $variables['media'];
    $variables['#cache']['contexts'][] = 'user.permissions';
    $rel = $media->access('edit') ? 'edit-form' : 'canonical';
    $variables['url'] = $media->toUrl($rel, [
      'language' => $media->language(),
    ]);
    $variables['preview_attributes'] = new Attribute();
120
    $variables['preview_attributes']->addClass('media-library-item__preview', 'js-media-library-item-preview', 'js-click-to-select-trigger');
121 122 123 124 125 126 127 128 129 130 131 132 133 134
    $variables['metadata_attributes'] = new Attribute();
    $variables['metadata_attributes']->addClass('media-library-item__attributes');
    $variables['status'] = $media->isPublished();
  }
}

/**
 * Alter the bulk form to add a more accessible label.
 *
 * @param array $form
 *   An associative array containing the structure of the form.
 * @param \Drupal\Core\Form\FormStateInterface $form_state
 *   The current state of the form.
 *
135
 * @todo Remove in https://www.drupal.org/node/2983454
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
 */
function media_library_form_views_form_media_library_page_alter(array &$form, FormStateInterface $form_state) {
  if (isset($form['media_bulk_form']) && isset($form['output'])) {
    /** @var \Drupal\views\ViewExecutable $view */
    $view = $form['output'][0]['#view'];
    foreach (Element::getVisibleChildren($form['media_bulk_form']) as $key) {
      if (isset($view->result[$key])) {
        $media = $view->field['media_bulk_form']->getEntity($view->result[$key]);
        $form['media_bulk_form'][$key]['#title'] = t('Select @label', [
          '@label' => $media->label(),
        ]);
      }
    }
  }
}

152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
/**
 * Implements hook_form_alter().
 */
function media_library_form_alter(array &$form, FormStateInterface $form_state, $form_id) {
  $form_object = $form_state->getFormObject();
  if ($form_object instanceof ViewsForm && strpos($form_object->getBaseFormId(), 'views_form_media_library') === 0) {
    $form['#attributes']['class'][] = 'media-library-views-form';
    if (isset($form['header'])) {
      $form['header']['#attributes']['class'][] = 'media-library-views-form__header';
      $form['header']['media_bulk_form']['#attributes']['class'][] = 'media-library-views-form__bulk_form';
    }
  }

  // Add after build to fix media library views exposed filter's submit button.
  if ($form_id === 'views_exposed_form' && $form['#id'] === 'views-exposed-form-media-library-widget') {
    $form['#after_build'][] = '_media_library_views_form_media_library_after_build';
  }
}

/**
 * After build callback for views form media library.
 */
function _media_library_views_form_media_library_after_build(array $form, FormStateInterface $form_state) {
  // Remove .form-actions from media library views exposed filter actions
  // and replace with .media-library-view--form-actions.
  //
  // This prevents the views exposed filter's 'Apply filter' submit button from
  // being moved into the dialog's buttons.
  // @see \Drupal\Core\Render\Element\Actions::processActions
  // @see Drupal.behaviors.dialog.prepareDialogButtons
  if (($key = array_search('form-actions', $form['actions']['#attributes']['class'])) !== FALSE) {
    unset($form['actions']['#attributes']['class'][$key]);
  }
  $form['actions']['#attributes']['class'][] = 'media-library-view--form-actions';
  return $form;
}

/**
 * Implements hook_views_query_alter().
 *
 * Alters the widget view's query to only show media that can be selected,
 * based on what types are allowed in the field settings.
 *
 * @todo Remove in https://www.drupal.org/node/2983454
 */
function media_library_views_query_alter(ViewExecutable $view, QueryPluginBase $query) {
  if ($query instanceof Sql && $view->id() === 'media_library' && $view->current_display === 'widget') {
    $types = _media_library_get_allowed_types();
    if ($types) {
      $entity_type = \Drupal::entityTypeManager()->getDefinition('media');
      $group = $query->setWhereGroup();
      $query->addWhere($group, $entity_type->getDataTable() . '.' . $entity_type->getKey('bundle'), $types, 'in');
    }
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * Limits the types available in the exposed filter to avoid users trying to
 * filter by a type that is un-selectable.
 *
 * @see media_library_views_query_alter()
 *
 * @todo Remove in https://www.drupal.org/node/2983454
 */
function media_library_form_views_exposed_form_alter(array &$form, FormStateInterface $form_state) {
  if (isset($form['#id']) && $form['#id'] === 'views-exposed-form-media-library-widget') {
    $types = _media_library_get_allowed_types();
    if ($types && isset($form['type']['#options'])) {
      $keys = array_flip($types);
      // Ensure that the default value (by default "All") persists.
      if (isset($form['type']['#default_value'])) {
        $keys[$form['type']['#default_value']] = TRUE;
      }
      $form['type']['#options'] = array_intersect_key($form['type']['#options'], $keys);
    }
  }
}

/**
 * Implements hook_field_ui_preconfigured_options_alter().
 */
function media_library_field_ui_preconfigured_options_alter(array &$options, $field_type) {
  // If the field is not an "entity_reference"-based field, bail out.
  $class = \Drupal::service('plugin.manager.field.field_type')->getPluginClass($field_type);
  if (!is_a($class, EntityReferenceItem::class, TRUE)) {
    return;
  }

  // Set the default field widget for media to be the Media library.
  if (!empty($options['media'])) {
    $options['media']['entity_form_display']['type'] = 'media_library_widget';
  }
}

248 249 250 251 252 253 254 255 256 257 258 259 260 261
/**
 * Implements hook_local_tasks_alter().
 *
 * Removes tasks for the Media library if the view display no longer exists.
 */
function media_library_local_tasks_alter(&$local_tasks) {
  /** @var \Symfony\Component\Routing\RouteCollection $route_collection */
  $route_collection = \Drupal::service('router')->getRouteCollection();
  foreach (['media_library.grid', 'media_library.table'] as $key) {
    if (isset($local_tasks[$key]) && !$route_collection->get($local_tasks[$key]['route_name'])) {
      unset($local_tasks[$key]);
    }
  }
}
262 263 264 265 266 267 268 269 270 271 272 273 274 275

/**
 * Determines what types are allowed based on the current request.
 *
 * @return array
 *   An array of allowed types.
 */
function _media_library_get_allowed_types() {
  $types = \Drupal::request()->query->get('media_library_allowed_types');
  if ($types && is_array($types)) {
    return array_filter($types, 'is_string');
  }
  return [];
}