media.module 4.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * @file
  4. * Provides media items.
  5. */
  6. use Drupal\Core\Access\AccessResult;
  7. use Drupal\Core\Session\AccountInterface;
  8. use Drupal\field\FieldConfigInterface;
  9. use Drupal\Core\Entity\EntityInterface;
  10. use Drupal\Core\Render\Element;
  11. use Drupal\Core\Routing\RouteMatchInterface;
  12. use Drupal\Core\Url;
  13. /**
  14. * Implements hook_help().
  15. */
  16. function media_help($route_name, RouteMatchInterface $route_match) {
  17. switch ($route_name) {
  18. case 'help.page.media':
  19. $output = '<h3>' . t('About') . '</h3>';
  20. $output .= '<p>' . t('The Media module manages the creation, editing, deletion, settings and display of media. Items are typically images, documents, slideshows, YouTube videos, tweets, Instagram photos, etc. You can reference media items from any other content on your site. For more information, see the <a href=":media">online documentation for the Media module</a>.', [':media' => 'https://www.drupal.org/docs/8/core/modules/media']) . '</p>';
  21. $output .= '<h3>' . t('Uses') . '</h3>';
  22. $output .= '<dl>';
  23. $output .= '<dt>' . t('Creating media items') . '</dt>';
  24. $output .= '<dd>' . t('When a new media item is created, the Media module records basic information about it, including the author, date of creation, and the <a href=":media-type">media type</a>. It also manages the <em>publishing options</em>, which define whether or not the item is published. Default settings can be configured for each type of media on your site.', [':media-type' => Url::fromRoute('entity.media_type.collection')->toString()]) . '</dd>';
  25. $output .= '<dt>' . t('Creating custom media types') . '</dt>';
  26. $output .= '<dd>' . t('The Media module gives users with the <em>Administer media types</em> permission the ability to <a href=":media-new">create new media types</a> in addition to the default ones already configured. Each media type has an associated media source (such as the image source) which support thumbnail generation and metadata extraction. Fields managed by the <a href=":field">Field module</a> may be added for storing that metadata, such as width and height, as well as any other associated values.', [':media-new' => Url::fromRoute('entity.media_type.add_form')->toString(), ':field' => Url::fromRoute('help.page', ['name' => 'field'])->toString()]) . '</dd>';
  27. $output .= '<dt>' . t('Creating revisions') . '</dt>';
  28. $output .= '<dd>' . t('The Media module also enables you to create multiple versions of any media item, and revert to older versions using the <em>Revision information</em> settings.') . '</dd>';
  29. $output .= '<dt>' . t('User permissions') . '</dt>';
  30. $output .= '<dd>' . t('The Media module makes a number of permissions available, which can be set by role on the <a href=":permissions">permissions page</a>.', [':permissions' => Url::fromRoute('user.admin_permissions', [], ['fragment' => 'module-media'])->toString()]) . '</dd>';
  31. $output .= '</dl>';
  32. return $output;
  33. }
  34. }
  35. /**
  36. * Implements hook_theme().
  37. */
  38. function media_theme() {
  39. return [
  40. 'media' => [
  41. 'render element' => 'elements',
  42. ],
  43. ];
  44. }
  45. /**
  46. * Implements hook_entity_access().
  47. */
  48. function media_entity_access(EntityInterface $entity, $operation, AccountInterface $account) {
  49. if ($operation === 'delete' && $entity instanceof FieldConfigInterface && $entity->getTargetEntityTypeId() === 'media') {
  50. /** @var \Drupal\media\MediaTypeInterface $media_type */
  51. $media_type = \Drupal::entityTypeManager()->getStorage('media_type')->load($entity->getTargetBundle());
  52. return AccessResult::forbiddenIf($entity->id() === 'media.' . $media_type->id() . '.' . $media_type->getSource()->getConfiguration()['source_field']);
  53. }
  54. return AccessResult::neutral();
  55. }
  56. /**
  57. * Implements hook_theme_suggestions_HOOK().
  58. */
  59. function media_theme_suggestions_media(array $variables) {
  60. $suggestions = [];
  61. $media = $variables['elements']['#media'];
  62. $sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_');
  63. $suggestions[] = 'media__' . $sanitized_view_mode;
  64. $suggestions[] = 'media__' . $media->bundle();
  65. $suggestions[] = 'media__' . $media->bundle() . '__' . $sanitized_view_mode;
  66. return $suggestions;
  67. }
  68. /**
  69. * Prepares variables for media templates.
  70. *
  71. * Default template: media.html.twig.
  72. *
  73. * @param array $variables
  74. * An associative array containing:
  75. * - elements: An array of elements to display in view mode.
  76. * - media: The media item.
  77. * - name: The label for the media item.
  78. * - view_mode: View mode; e.g., 'full', 'teaser', etc.
  79. */
  80. function template_preprocess_media(array &$variables) {
  81. $variables['media'] = $variables['elements']['#media'];
  82. $variables['view_mode'] = $variables['elements']['#view_mode'];
  83. $variables['name'] = $variables['media']->label();
  84. // Helpful $content variable for templates.
  85. foreach (Element::children($variables['elements']) as $key) {
  86. $variables['content'][$key] = $variables['elements'][$key];
  87. }
  88. }