drupal/core/modules/media_library/media_library.module

110 lines
3.8 KiB
Plaintext

<?php
/**
* @file
* Contains hook implementations for the media_library module.
*/
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\views\Plugin\views\cache\CachePluginBase;
use Drupal\views\ViewExecutable;
use Drupal\Core\Template\Attribute;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
/**
* 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',
],
];
}
/**
* 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';
}
}
/**
* 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();
$variables['preview_attributes']->addClass('media-library-item__preview', 'js-click-to-select__trigger');
$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.
*
* @todo Remove in https://www.drupal.org/project/drupal/issues/2969660
*/
function media_library_form_views_form_media_library_page_alter(array &$form, FormStateInterface $form_state) {
if (isset($form['media_bulk_form']) && isset($form['output'])) {
$form['#attributes']['class'][] = 'media-library-page-form';
$form['header']['#attributes']['class'][] = 'media-library-page-form__header';
/** @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(),
]);
}
}
}
}
/**
* 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]);
}
}
}