MediaSourceInterface.php 7.5 KB

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