MediaSourceInterface.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace Drupal\media;
  3. use Drupal\Component\Plugin\ConfigurablePluginInterface;
  4. use Drupal\Component\Plugin\PluginInspectionInterface;
  5. use Drupal\Core\Plugin\PluginFormInterface;
  6. /**
  7. * Defines the interface for media source plugins.
  8. *
  9. * Media sources provide the critical link between media items in Drupal and the
  10. * actual media itself, which typically exists independently of Drupal. Each
  11. * media source works with a certain kind of media. For example, local files and
  12. * YouTube videos can both be catalogued in a similar way as media items, but
  13. * they need very different handling to actually display them.
  14. *
  15. * Each media type needs exactly one source. A single source can be used on many
  16. * media types.
  17. *
  18. * Examples of possible sources are:
  19. * - File: handles local files,
  20. * - Image: handles local images,
  21. * - oEmbed: handles resources that are exposed through the oEmbed standard,
  22. * - YouTube: handles YouTube videos,
  23. * - SoundCould: handles SoundCloud audio,
  24. * - Instagram: handles Instagram posts,
  25. * - Twitter: handles tweets,
  26. * - ...
  27. *
  28. * Their responsibilities are:
  29. * - Defining how media is represented (stored). Media sources are not
  30. * responsible for actually storing the media. They only define how it is
  31. * represented on a media item (usually using some kind of a field).
  32. * - Providing thumbnails. Media sources that are responsible for remote
  33. * media will generally fetch the image from a third-party API and make
  34. * it available for the local usage. Media sources that represent local
  35. * media (such as images) will usually use some locally provided image.
  36. * Media sources should fall back to a pre-defined default thumbnail if
  37. * everything else fails.
  38. * - Validating a media item before it is saved. The entity constraint system
  39. * will be used to ensure the valid structure of the media item.
  40. * For example, media sources that represent remote media might check the
  41. * URL or other identifier, while sources that represent local files might
  42. * check the MIME type of the file.
  43. * - Providing a default name for a media item. This will save users from
  44. * manually entering the name when it can be reliably set automatically.
  45. * Media sources for local files will generally use the filename, while media
  46. * sources for remote resources might obtain a title attribute through a
  47. * third-party API. The name can always be overridden by the user.
  48. * - Providing metadata specific to the given media type. For example, remote
  49. * media sources generally get information available through a
  50. * third-party API and make it available to Drupal, while local media sources
  51. * can expose things such as EXIF or ID3.
  52. * - Mapping metadata to the media item. Metadata that a media source exposes
  53. * can automatically be mapped to the fields on the media item. Media
  54. * sources will be able to define how this is done.
  55. *
  56. * @see \Drupal\media\Annotation\MediaSource
  57. * @see \Drupal\media\MediaSourceBase
  58. * @see \Drupal\media\MediaSourceManager
  59. * @see \Drupal\media\MediaTypeInterface
  60. * @see \Drupal\media\MediaSourceEntityConstraintsInterface
  61. * @see \Drupal\media\MediaSourceFieldConstraintsInterface
  62. * @see plugin_api
  63. */
  64. interface MediaSourceInterface extends PluginInspectionInterface, ConfigurablePluginInterface, PluginFormInterface {
  65. /**
  66. * Default empty value for metadata fields.
  67. */
  68. const METADATA_FIELD_EMPTY = '_none';
  69. /**
  70. * Gets a list of metadata attributes provided by this plugin.
  71. *
  72. * Most media sources have associated metadata, describing attributes
  73. * such as:
  74. * - dimensions
  75. * - duration
  76. * - encoding
  77. * - date
  78. * - location
  79. * - permalink
  80. * - licensing information
  81. * - ...
  82. *
  83. * This method should list all metadata attributes that a media source MAY
  84. * offer. In other words: it is possible that a particular media item does
  85. * not contain a certain attribute. For example: an oEmbed media source can
  86. * contain both video and images. Images don't have a duration, but
  87. * videos do.
  88. *
  89. * (The term 'attributes' was chosen because it cannot be confused
  90. * with 'fields' and 'properties', both of which are concepts in Drupal's
  91. * Entity Field API.)
  92. *
  93. * @return array
  94. * Associative array with:
  95. * - keys: metadata attribute names
  96. * - values: human-readable labels for those attribute names
  97. */
  98. public function getMetadataAttributes();
  99. /**
  100. * Gets the value for a metadata attribute for a given media item.
  101. *
  102. * @param \Drupal\media\MediaInterface $media
  103. * A media item.
  104. * @param string $attribute_name
  105. * Name of the attribute to fetch.
  106. *
  107. * @return mixed|null
  108. * Metadata attribute value or NULL if unavailable.
  109. */
  110. public function getMetadata(MediaInterface $media, $attribute_name);
  111. /**
  112. * Get the source field definition for a media type.
  113. *
  114. * @param \Drupal\media\MediaTypeInterface $type
  115. * A media type.
  116. *
  117. * @return \Drupal\Core\Field\FieldDefinitionInterface|null
  118. * The source field definition, or NULL if it doesn't exist or has not been
  119. * configured yet.
  120. */
  121. public function getSourceFieldDefinition(MediaTypeInterface $type);
  122. /**
  123. * Creates the source field definition for a type.
  124. *
  125. * @param \Drupal\media\MediaTypeInterface $type
  126. * The media type.
  127. *
  128. * @return \Drupal\field\FieldConfigInterface
  129. * The unsaved field definition. The field storage definition, if new,
  130. * should also be unsaved.
  131. */
  132. public function createSourceField(MediaTypeInterface $type);
  133. }