annotate core/modules/media/src/MediaSourceInterface.php @ 0:4c8ae668cc8c

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