first commit

This commit is contained in:
2024-07-15 12:33:27 +02:00
commit ce50ae282b
22084 changed files with 2623791 additions and 0 deletions

View File

@@ -0,0 +1,132 @@
<?php
namespace Drupal\media\Annotation;
use Drupal\Component\Annotation\Plugin;
/**
* Defines a media source plugin annotation object.
*
* Media sources are responsible for implementing all the logic for dealing
* with a particular type of media. They provide various universal and
* type-specific metadata about media of the type they handle.
*
* Plugin namespace: Plugin\media\Source
*
* For a working example, see \Drupal\media\Plugin\media\Source\File.
*
* @see \Drupal\media\MediaSourceInterface
* @see \Drupal\media\MediaSourceBase
* @see \Drupal\media\MediaSourceManager
* @see hook_media_source_info_alter()
* @see plugin_api
*
* @Annotation
*/
class MediaSource extends Plugin {
/**
* The plugin ID.
*
* @var string
*/
public $id;
/**
* The human-readable name of the media source.
*
* @var \Drupal\Core\Annotation\Translation
*
* @ingroup plugin_translatable
*/
public $label;
/**
* A brief description of the media source.
*
* @var \Drupal\Core\Annotation\Translation
*
* @ingroup plugin_translatable
*/
public $description = '';
/**
* The field types that can be used as a source field for this media source.
*
* @var string[]
*/
public $allowed_field_types = [];
/**
* The classes used to define media source-specific forms.
*
* An array of form class names, keyed by ID. The ID represents the operation
* the form is used for.
*
* @var string[]
*/
public $forms = [];
/**
* A filename for the default thumbnail.
*
* The thumbnails are placed in the directory defined by the config setting
* 'media.settings.icon_base_uri'. When using custom icons, make sure the
* module provides a hook_install() implementation to copy the custom icons
* to this directory. The media_install() function provides a clear example
* of how to do this.
*
* @var string
*
* @see media_install()
*/
public $default_thumbnail_filename = 'generic.png';
/**
* The metadata attribute name to provide the thumbnail URI.
*
* @var string
*/
public $thumbnail_uri_metadata_attribute = 'thumbnail_uri';
/**
* The metadata attribute name to provide the thumbnail width.
*
* @var string
*/
public $thumbnail_width_metadata_attribute = 'thumbnail_width';
/**
* The metadata attribute name to provide the thumbnail height.
*
* @var string
*/
public $thumbnail_height_metadata_attribute = 'thumbnail_height';
/**
* (optional) The metadata attribute name to provide the thumbnail alt.
*
* "Thumbnail" will be used if the attribute name is not provided.
*
* @var string|null
*/
public $thumbnail_alt_metadata_attribute;
/**
* (optional) The metadata attribute name to provide the thumbnail title.
*
* The name of the media item will be used if the attribute name is not
* provided.
*
* @var string|null
*/
public $thumbnail_title_metadata_attribute;
/**
* The metadata attribute name to provide the default name.
*
* @var string
*/
public $default_name_metadata_attribute = 'default_name';
}

View File

@@ -0,0 +1,87 @@
<?php
declare(strict_types=1);
namespace Drupal\media\Attribute;
use Drupal\Component\Plugin\Attribute\Plugin;
use Drupal\Core\StringTranslation\TranslatableMarkup;
/**
* Defines a MediaSource attribute.
*
* Media sources are responsible for implementing all the logic for dealing
* with a particular type of media. They provide various universal and
* type-specific metadata about media of the type they handle.
*
* Plugin namespace: Plugin\media\Source
*
* For a working example, see \Drupal\media\Plugin\media\Source\File.
*
* @see \Drupal\media\MediaSourceInterface
* @see \Drupal\media\MediaSourceBase
* @see \Drupal\media\MediaSourceManager
* @see hook_media_source_info_alter()
* @see plugin_api
*/
#[\Attribute(\Attribute::TARGET_CLASS)]
class MediaSource extends Plugin {
/**
* Constructs a new MediaSource attribute.
*
* @param string $id
* The attribute class ID.
* @param \Drupal\Core\StringTranslation\TranslatableMarkup $label
* The human-readable name of the media source.
* @param \Drupal\Core\StringTranslation\TranslatableMarkup|null $description
* (optional) A brief description of the media source.
* @param string[] $allowed_field_types
* (optional) The field types that can be used as a source field for this
* media source.
* @param class-string[] $forms
* (optional) The classes used to define media source-specific forms. An
* array of form class names, keyed by ID. The ID represents the operation
* the form is used for, for example, 'media_library_add'.
* @param string $default_thumbnail_filename
* (optional) A filename for the default thumbnail.
* The thumbnails are placed in the directory defined by the config setting
* 'media.settings.icon_base_uri'. When using custom icons, make sure the
* module provides a hook_install() implementation to copy the custom icons
* to this directory. The media_install() function provides a clear example
* of how to do this.
* @param string $thumbnail_uri_metadata_attribute
* (optional) The metadata attribute name to provide the thumbnail URI.
* @param string $thumbnail_width_metadata_attribute
* (optional) The metadata attribute name to provide the thumbnail width.
* @param string $thumbnail_height_metadata_attribute
* (optional) The metadata attribute name to provide the thumbnail height.
* @param string|null $thumbnail_alt_metadata_attribute
* (optional) The metadata attribute name to provide the thumbnail alt.
* "Thumbnail" will be used if the attribute name is not provided.
* @param string|null $thumbnail_title_metadata_attribute
* (optional) The metadata attribute name to provide the thumbnail title.
* The name of the media item will be used if the attribute name is not
* provided.
* @param string $default_name_metadata_attribute
* (optional) The metadata attribute name to provide the default name.
* @param class-string|null $deriver
* (optional) The deriver class.
*/
public function __construct(
public readonly string $id,
public readonly TranslatableMarkup $label,
public readonly ?TranslatableMarkup $description = NULL,
public readonly array $allowed_field_types = [],
public readonly array $forms = [],
public readonly string $default_thumbnail_filename = 'generic.png',
public readonly string $thumbnail_uri_metadata_attribute = 'thumbnail_uri',
public readonly string $thumbnail_width_metadata_attribute = 'thumbnail_width',
public readonly string $thumbnail_height_metadata_attribute = 'thumbnail_height',
public readonly ?string $thumbnail_alt_metadata_attribute = NULL,
public readonly ?string $thumbnail_title_metadata_attribute = NULL,
public readonly string $default_name_metadata_attribute = 'default_name',
public readonly ?string $deriver = NULL,
) {}
}

View File

@@ -0,0 +1,86 @@
<?php
declare(strict_types=1);
namespace Drupal\media\Attribute;
use Drupal\Core\StringTranslation\TranslatableMarkup;
/**
* Defines a OEmbedMediaSource attribute.
*
* Plugin namespace: Plugin\media\Source
*
* For a working example, see \Drupal\media\Plugin\media\Source\OEmbed.
*
* @see \Drupal\media\MediaSourceInterface
* @see \Drupal\media\MediaSourceBase
* @see \Drupal\media\MediaSourceManager
* @see hook_media_source_info_alter()
* @see plugin_api
*/
#[\Attribute(\Attribute::TARGET_CLASS)]
class OEmbedMediaSource extends MediaSource {
/**
* Constructs a new OEmbedMediaSource attribute.
*
* @param string $id
* The attribute class ID.
* @param \Drupal\Core\StringTranslation\TranslatableMarkup $label
* The human-readable name of the media source.
* @param \Drupal\Core\StringTranslation\TranslatableMarkup|null $description
* (optional) A brief description of the media source.
* @param string[] $allowed_field_types
* (optional) The field types that can be used as a source field for this
* media source.
* @param class-string[] $forms
* (optional) The classes used to define media source-specific forms. An
* array of form class names, keyed by ID. The ID represents the operation
* the form is used for, for example, 'media_library_add'.
* @param string[] $providers
* (optional) A set of provider names, exactly as they appear in the
* canonical oEmbed provider database at https://oembed.com/providers.json.
* @param string $default_thumbnail_filename
* (optional) A filename for the default thumbnail.
* The thumbnails are placed in the directory defined by the config setting
* 'media.settings.icon_base_uri'. When using custom icons, make sure the
* module provides a hook_install() implementation to copy the custom icons
* to this directory. The media_install() function provides a clear example
* of how to do this.
* @param string $thumbnail_uri_metadata_attribute
* (optional) The metadata attribute name to provide the thumbnail URI.
* @param string $thumbnail_width_metadata_attribute
* (optional) The metadata attribute name to provide the thumbnail width.
* @param string $thumbnail_height_metadata_attribute
* (optional) The metadata attribute name to provide the thumbnail height.
* @param string|null $thumbnail_alt_metadata_attribute
* (optional) The metadata attribute name to provide the thumbnail alt.
* "Thumbnail" will be used if the attribute name is not provided.
* @param string|null $thumbnail_title_metadata_attribute
* (optional) The metadata attribute name to provide the thumbnail title.
* The name of the media item will be used if the attribute name is not
* provided.
* @param string $default_name_metadata_attribute
* (optional) The metadata attribute name to provide the default name.
* @param class-string|null $deriver
* (optional) The deriver class.
*/
public function __construct(
public readonly string $id,
public readonly TranslatableMarkup $label,
public readonly ?TranslatableMarkup $description = NULL,
public readonly array $allowed_field_types = [],
public readonly array $forms = [],
public readonly array $providers = [],
public readonly string $default_thumbnail_filename = 'generic.png',
public readonly string $thumbnail_uri_metadata_attribute = 'thumbnail_uri',
public readonly string $thumbnail_width_metadata_attribute = 'thumbnail_width',
public readonly string $thumbnail_height_metadata_attribute = 'thumbnail_height',
public readonly ?string $thumbnail_alt_metadata_attribute = NULL,
public readonly ?string $thumbnail_title_metadata_attribute = NULL,
public readonly string $default_name_metadata_attribute = 'default_name',
public readonly ?string $deriver = NULL,
) {}
}

View File

@@ -0,0 +1,173 @@
<?php
namespace Drupal\media\Controller;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\ContentEntityStorageInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\filter\FilterFormatInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* Controller which renders a preview of the provided text.
*
* @internal
* This is an internal part of the media system in Drupal core and may be
* subject to change in minor releases. This class should not be
* instantiated or extended by external code.
*/
class MediaFilterController implements ContainerInjectionInterface {
/**
* The renderer service.
*
* @var \Drupal\Core\Render\RendererInterface
*/
protected $renderer;
/**
* The media storage.
*
* @var \Drupal\Core\Entity\ContentEntityStorageInterface
*/
protected $mediaStorage;
/**
* The entity repository.
*
* @var \Drupal\Core\Entity\EntityRepositoryInterface
*/
protected $entityRepository;
/**
* Constructs an MediaFilterController instance.
*
* @param \Drupal\Core\Render\RendererInterface $renderer
* The renderer service.
* @param \Drupal\Core\Entity\ContentEntityStorageInterface $media_storage
* The media storage.
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
* The entity repository.
*/
public function __construct(RendererInterface $renderer, ContentEntityStorageInterface $media_storage, EntityRepositoryInterface $entity_repository) {
$this->renderer = $renderer;
$this->mediaStorage = $media_storage;
$this->entityRepository = $entity_repository;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('renderer'),
$container->get('entity_type.manager')->getStorage('media'),
$container->get('entity.repository')
);
}
/**
* Returns a HTML response containing a preview of the text after filtering.
*
* Applies all of the given text format's filters, not just the `media_embed`
* filter, because for example `filter_align` and `filter_caption` may apply
* to it as well.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* The request object.
* @param \Drupal\filter\FilterFormatInterface $filter_format
* The text format.
*
* @return \Symfony\Component\HttpFoundation\Response
* The filtered text.
*
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
* Throws an exception if 'text' parameter is not found in the query
* string.
*
* @see \Drupal\editor\EditorController::getUntransformedText
*/
public function preview(Request $request, FilterFormatInterface $filter_format) {
self::checkCsrf($request, \Drupal::currentUser());
$text = $request->query->get('text');
$uuid = $request->query->get('uuid');
if ($text == '' || $uuid == '') {
throw new NotFoundHttpException();
}
$build = [
'#type' => 'processed_text',
'#text' => $text,
'#format' => $filter_format->id(),
];
$html = $this->renderer->renderInIsolation($build);
// Load the media item so we can embed the label in the response, for use
// in an ARIA label.
$headers = [];
if ($media = $this->entityRepository->loadEntityByUuid('media', $uuid)) {
$headers['Drupal-Media-Label'] = $this->entityRepository->getTranslationFromContext($media)->label();
}
// Note that we intentionally do not use:
// - \Drupal\Core\Cache\CacheableResponse because caching it on the server
// side is wasteful, hence there is no need for cacheability metadata.
// - \Drupal\Core\Render\HtmlResponse because there is no need for
// attachments nor cacheability metadata.
return (new Response($html, 200, $headers))
// Do not allow any intermediary to cache the response, only the end user.
->setPrivate()
// Allow the end user to cache it for up to 5 minutes.
->setMaxAge(300);
}
/**
* Checks access based on media_embed filter status on the text format.
*
* @param \Drupal\filter\FilterFormatInterface $filter_format
* The text format for which to check access.
*
* @return \Drupal\Core\Access\AccessResultInterface
* The access result.
*/
public static function formatUsesMediaEmbedFilter(FilterFormatInterface $filter_format) {
$filters = $filter_format->filters();
return AccessResult::allowedIf($filters->has('media_embed') && $filters->get('media_embed')->status)
->addCacheableDependency($filter_format);
}
/**
* Throws an AccessDeniedHttpException if the request fails CSRF validation.
*
* This is used instead of \Drupal\Core\Access\CsrfAccessCheck, in order to
* allow access for anonymous users.
*
* @todo Refactor this to an access checker.
*/
private static function checkCsrf(Request $request, AccountInterface $account) {
$header = 'X-Drupal-MediaPreview-CSRF-Token';
if (!$request->headers->has($header)) {
throw new AccessDeniedHttpException();
}
if ($account->isAnonymous()) {
// For anonymous users, just the presence of the custom header is
// sufficient protection.
return;
}
// For authenticated users, validate the token value.
$token = $request->headers->get($header);
if (!\Drupal::csrfToken()->validate($token, $header)) {
throw new AccessDeniedHttpException();
}
}
}

View File

@@ -0,0 +1,225 @@
<?php
namespace Drupal\media\Controller;
use Drupal\Component\Utility\Crypt;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\Render\HtmlResponse;
use Drupal\Core\Render\RenderContext;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Url;
use Drupal\media\IFrameMarkup;
use Drupal\media\IFrameUrlHelper;
use Drupal\media\OEmbed\ResourceException;
use Drupal\media\OEmbed\ResourceFetcherInterface;
use Drupal\media\OEmbed\UrlResolverInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
/**
* Controller which renders an oEmbed resource in a bare page (without blocks).
*
* This controller is meant to render untrusted third-party HTML returned by
* an oEmbed provider in an iframe, so as to mitigate the potential dangers of
* of displaying third-party markup (i.e., XSS). The HTML returned by this
* controller should not be trusted, and should *never* be displayed outside
* of an iframe.
*
* @internal
* This is an internal part of the media system in Drupal core and may be
* subject to change in minor releases. This class should not be
* instantiated or extended by external code.
*/
class OEmbedIframeController implements ContainerInjectionInterface {
/**
* The oEmbed resource fetcher service.
*
* @var \Drupal\media\OEmbed\ResourceFetcherInterface
*/
protected $resourceFetcher;
/**
* The oEmbed URL resolver service.
*
* @var \Drupal\media\OEmbed\UrlResolverInterface
*/
protected $urlResolver;
/**
* The renderer service.
*
* @var \Drupal\Core\Render\RendererInterface
*/
protected $renderer;
/**
* The logger channel.
*
* @var \Psr\Log\LoggerInterface
*/
protected $logger;
/**
* The iFrame URL helper service.
*
* @var \Drupal\media\IFrameUrlHelper
*/
protected $iFrameUrlHelper;
/**
* Constructs an OEmbedIframeController instance.
*
* @param \Drupal\media\OEmbed\ResourceFetcherInterface $resource_fetcher
* The oEmbed resource fetcher service.
* @param \Drupal\media\OEmbed\UrlResolverInterface $url_resolver
* The oEmbed URL resolver service.
* @param \Drupal\Core\Render\RendererInterface $renderer
* The renderer service.
* @param \Psr\Log\LoggerInterface $logger
* The logger channel.
* @param \Drupal\media\IFrameUrlHelper $iframe_url_helper
* The iFrame URL helper service.
*/
public function __construct(ResourceFetcherInterface $resource_fetcher, UrlResolverInterface $url_resolver, RendererInterface $renderer, LoggerInterface $logger, IFrameUrlHelper $iframe_url_helper) {
$this->resourceFetcher = $resource_fetcher;
$this->urlResolver = $url_resolver;
$this->renderer = $renderer;
$this->logger = $logger;
$this->iFrameUrlHelper = $iframe_url_helper;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('media.oembed.resource_fetcher'),
$container->get('media.oembed.url_resolver'),
$container->get('renderer'),
$container->get('logger.factory')->get('media'),
$container->get('media.oembed.iframe_url_helper')
);
}
/**
* Renders an oEmbed resource.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* The request object.
*
* @return \Symfony\Component\HttpFoundation\Response
* The response object.
*
* @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
* Will be thrown if either
* - the 'hash' parameter does not match the expected hash of the 'url'
* parameter;
* - the iframe_domain is set in media.settings and does not match the host
* in the request.
*/
public function render(Request $request) {
// @todo Move domain check logic to a separate method.
$allowed_domain = \Drupal::config('media.settings')->get('iframe_domain');
if ($allowed_domain) {
$allowed_host = parse_url($allowed_domain, PHP_URL_HOST);
$host = parse_url($request->getSchemeAndHttpHost(), PHP_URL_HOST);
if ($allowed_host !== $host) {
throw new BadRequestHttpException('This resource is not available');
}
}
$url = $request->query->get('url');
$max_width = $request->query->getInt('max_width');
$max_height = $request->query->getInt('max_height');
// Hash the URL and max dimensions, and ensure it is equal to the hash
// parameter passed in the query string.
$hash = $this->iFrameUrlHelper->getHash($url, $max_width, $max_height);
if (!hash_equals($hash, $request->query->get('hash', ''))) {
throw new BadRequestHttpException('This resource is not available');
}
// Return a response instead of a render array so that the frame content
// will not have all the blocks and page elements normally rendered by
// Drupal.
$response = new HtmlResponse('', HtmlResponse::HTTP_OK, [
'Content-Type' => 'text/html; charset=UTF-8',
]);
$response->addCacheableDependency(Url::createFromRequest($request));
try {
$resource_url = $this->urlResolver->getResourceUrl($url, $max_width, $max_height);
$resource = $this->resourceFetcher->fetchResource($resource_url);
$placeholder_token = Crypt::randomBytesBase64(55);
// Render the content in a new render context so that the cacheability
// metadata of the rendered HTML will be captured correctly.
$element = [
'#theme' => 'media_oembed_iframe',
'#resource' => $resource,
// Even though the resource HTML is untrusted, IFrameMarkup::create()
// will create a trusted string. The only reason this is okay is
// because we are serving it in an iframe, which will mitigate the
// potential dangers of displaying third-party markup.
'#media' => IFrameMarkup::create($resource->getHtml()),
'#cache' => [
// Add the 'rendered' cache tag as this response is not processed by
// \Drupal\Core\Render\MainContent\HtmlRenderer::renderResponse().
'tags' => ['rendered'],
],
'#attached' => [
'html_response_attachment_placeholders' => [
'styles' => '<css-placeholder token="' . $placeholder_token . '">',
],
'library' => [
'media/oembed.frame',
],
],
'#placeholder_token' => $placeholder_token,
];
$context = new RenderContext();
$content = $this->renderer->executeInRenderContext($context, function () use ($element) {
return $this->renderer->render($element);
});
$response
->setContent($content)
->setAttachments($element['#attached'])
->addCacheableDependency($resource)
->addCacheableDependency(CacheableMetadata::createFromRenderArray($element));
// Modules and themes implementing hook_media_oembed_iframe_preprocess()
// can add additional #cache and #attachments to a render array. If this
// occurs, the render context won't be empty, and we need to ensure the
// added metadata is bubbled up to the response.
// @see \Drupal\Core\Theme\ThemeManager::render()
if (!$context->isEmpty()) {
$bubbleable_metadata = $context->pop();
assert($bubbleable_metadata instanceof BubbleableMetadata);
$response->addCacheableDependency($bubbleable_metadata);
$response->addAttachments($bubbleable_metadata->getAttachments());
}
}
catch (ResourceException $e) {
// Prevent the response from being cached.
$response->setMaxAge(0);
// The oEmbed system makes heavy use of exception wrapping, so log the
// entire exception chain to help with troubleshooting.
do {
// @todo Log additional information from ResourceException, to help with
// debugging, in https://www.drupal.org/project/drupal/issues/2972846.
$this->logger->error($e->getMessage());
$e = $e->getPrevious();
} while ($e);
}
return $response;
}
}

View File

@@ -0,0 +1,578 @@
<?php
namespace Drupal\media\Entity;
use Drupal\Core\Entity\EditorialContentEntityBase;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\media\MediaInterface;
use Drupal\media\MediaSourceEntityConstraintsInterface;
use Drupal\media\MediaSourceFieldConstraintsInterface;
use Drupal\user\EntityOwnerTrait;
/**
* Defines the media entity class.
*
* @todo Remove default/fallback entity form operation when #2006348 is done.
* @see https://www.drupal.org/node/2006348.
*
* @ContentEntityType(
* id = "media",
* label = @Translation("Media"),
* label_singular = @Translation("media item"),
* label_plural = @Translation("media items"),
* label_count = @PluralTranslation(
* singular = "@count media item",
* plural = "@count media items"
* ),
* bundle_label = @Translation("Media type"),
* handlers = {
* "storage" = "Drupal\media\MediaStorage",
* "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
* "list_builder" = "Drupal\media\MediaListBuilder",
* "access" = "Drupal\media\MediaAccessControlHandler",
* "form" = {
* "default" = "Drupal\media\MediaForm",
* "add" = "Drupal\media\MediaForm",
* "edit" = "Drupal\media\MediaForm",
* "delete" = "Drupal\Core\Entity\ContentEntityDeleteForm",
* "delete-multiple-confirm" = "Drupal\Core\Entity\Form\DeleteMultipleForm",
* "revision-delete" = \Drupal\Core\Entity\Form\RevisionDeleteForm::class,
* "revision-revert" = \Drupal\Core\Entity\Form\RevisionRevertForm::class,
* },
* "views_data" = "Drupal\media\MediaViewsData",
* "route_provider" = {
* "html" = "Drupal\media\Routing\MediaRouteProvider",
* "revision" = \Drupal\Core\Entity\Routing\RevisionHtmlRouteProvider::class,
* }
* },
* base_table = "media",
* data_table = "media_field_data",
* revision_table = "media_revision",
* revision_data_table = "media_field_revision",
* translatable = TRUE,
* show_revision_ui = TRUE,
* entity_keys = {
* "id" = "mid",
* "revision" = "vid",
* "bundle" = "bundle",
* "label" = "name",
* "langcode" = "langcode",
* "uuid" = "uuid",
* "published" = "status",
* "owner" = "uid",
* },
* revision_metadata_keys = {
* "revision_user" = "revision_user",
* "revision_created" = "revision_created",
* "revision_log_message" = "revision_log_message",
* },
* bundle_entity_type = "media_type",
* permission_granularity = "bundle",
* admin_permission = "administer media",
* field_ui_base_route = "entity.media_type.edit_form",
* common_reference_target = TRUE,
* links = {
* "add-page" = "/media/add",
* "add-form" = "/media/add/{media_type}",
* "canonical" = "/media/{media}/edit",
* "collection" = "/admin/content/media",
* "delete-form" = "/media/{media}/delete",
* "delete-multiple-form" = "/media/delete",
* "edit-form" = "/media/{media}/edit",
* "revision" = "/media/{media}/revisions/{media_revision}/view",
* "revision-delete-form" = "/media/{media}/revision/{media_revision}/delete",
* "revision-revert-form" = "/media/{media}/revision/{media_revision}/revert",
* "version-history" = "/media/{media}/revisions",
* }
* )
*/
class Media extends EditorialContentEntityBase implements MediaInterface {
use EntityOwnerTrait;
use StringTranslationTrait;
/**
* {@inheritdoc}
*/
public function getName() {
$name = $this->getEntityKey('label');
if (empty($name)) {
$media_source = $this->getSource();
return $media_source->getMetadata($this, $media_source->getPluginDefinition()['default_name_metadata_attribute']);
}
return $name;
}
/**
* {@inheritdoc}
*/
public function label() {
return $this->getName();
}
/**
* {@inheritdoc}
*/
public function setName($name) {
return $this->set('name', $name);
}
/**
* {@inheritdoc}
*/
public function getCreatedTime() {
return $this->get('created')->value;
}
/**
* {@inheritdoc}
*/
public function setCreatedTime($timestamp) {
return $this->set('created', $timestamp);
}
/**
* {@inheritdoc}
*/
public function getSource() {
return $this->bundle->entity->getSource();
}
/**
* Update the thumbnail for the media item.
*
* @param bool $from_queue
* Specifies whether the thumbnail update is triggered from the queue.
*
* @return \Drupal\media\MediaInterface
* The updated media item.
*
* @internal
*
* @todo There has been some disagreement about how to handle updates to
* thumbnails. We need to decide on what the API will be for this.
* https://www.drupal.org/node/2878119
*/
protected function updateThumbnail($from_queue = FALSE) {
$this->thumbnail->target_id = $this->loadThumbnail($this->getThumbnailUri($from_queue))->id();
$this->thumbnail->width = $this->getThumbnailWidth($from_queue);
$this->thumbnail->height = $this->getThumbnailHeight($from_queue);
// Set the thumbnail alt.
$media_source = $this->getSource();
$plugin_definition = $media_source->getPluginDefinition();
$this->thumbnail->alt = '';
if (!empty($plugin_definition['thumbnail_alt_metadata_attribute'])) {
$this->thumbnail->alt = $media_source->getMetadata($this, $plugin_definition['thumbnail_alt_metadata_attribute']);
}
return $this;
}
/**
* Loads the file entity for the thumbnail.
*
* If the file entity does not exist, it will be created.
*
* @param string $thumbnail_uri
* (optional) The URI of the thumbnail, used to load or create the file
* entity. If omitted, the default thumbnail URI will be used.
*
* @return \Drupal\file\FileInterface
* The thumbnail file entity.
*/
protected function loadThumbnail($thumbnail_uri = NULL) {
$values = [
'uri' => $thumbnail_uri ?: $this->getDefaultThumbnailUri(),
];
$file_storage = $this->entityTypeManager()->getStorage('file');
$existing = $file_storage->loadByProperties($values);
if ($existing) {
$file = reset($existing);
}
else {
/** @var \Drupal\file\FileInterface $file */
$file = $file_storage->create($values);
if ($owner = $this->getOwner()) {
$file->setOwner($owner);
}
$file->setPermanent();
$file->save();
}
return $file;
}
/**
* Returns the URI of the default thumbnail.
*
* @return string
* The default thumbnail URI.
*/
protected function getDefaultThumbnailUri() {
$default_thumbnail_filename = $this->getSource()->getPluginDefinition()['default_thumbnail_filename'];
return \Drupal::config('media.settings')->get('icon_base_uri') . '/' . $default_thumbnail_filename;
}
/**
* Updates the queued thumbnail for the media item.
*
* @return \Drupal\media\MediaInterface
* The updated media item.
*
* @internal
*
* @todo If the need arises in contrib, consider making this a public API,
* by adding an interface that extends MediaInterface.
*/
public function updateQueuedThumbnail() {
$this->updateThumbnail(TRUE);
return $this;
}
/**
* Gets the URI for the thumbnail of a media item.
*
* If thumbnail fetching is queued, new media items will use the default
* thumbnail, and existing media items will use the current thumbnail, until
* the queue is processed and the updated thumbnail has been fetched.
* Otherwise, the new thumbnail will be fetched immediately.
*
* @param bool $from_queue
* Specifies whether the thumbnail is being fetched from the queue.
*
* @return string
* The file URI for the thumbnail of the media item.
*
* @internal
*/
protected function getThumbnailUri($from_queue) {
$thumbnails_queued = $this->bundle->entity->thumbnailDownloadsAreQueued();
if ($thumbnails_queued && $this->isNew()) {
return $this->getDefaultThumbnailUri();
}
elseif ($thumbnails_queued && !$from_queue) {
return $this->get('thumbnail')->entity->getFileUri();
}
$source = $this->getSource();
return $source->getMetadata($this, $source->getPluginDefinition()['thumbnail_uri_metadata_attribute']);
}
/**
* Gets the width of the thumbnail of a media item.
*
* @param bool $from_queue
* Specifies whether the thumbnail is being fetched from the queue.
*
* @return int|null
* The width of the thumbnail of the media item or NULL if the media is new
* and the thumbnails are set to be downloaded in a queue.
*
* @internal
*/
protected function getThumbnailWidth(bool $from_queue): ?int {
$thumbnails_queued = $this->bundle->entity->thumbnailDownloadsAreQueued();
if ($thumbnails_queued && $this->isNew()) {
return NULL;
}
elseif ($thumbnails_queued && !$from_queue) {
return $this->get('thumbnail')->width;
}
$source = $this->getSource();
return $source->getMetadata($this, $source->getPluginDefinition()['thumbnail_width_metadata_attribute']);
}
/**
* Gets the height of the thumbnail of a media item.
*
* @param bool $from_queue
* Specifies whether the thumbnail is being fetched from the queue.
*
* @return int|null
* The height of the thumbnail of the media item or NULL if the media is new
* and the thumbnails are set to be downloaded in a queue.
*
* @internal
*/
protected function getThumbnailHeight(bool $from_queue): ?int {
$thumbnails_queued = $this->bundle->entity->thumbnailDownloadsAreQueued();
if ($thumbnails_queued && $this->isNew()) {
return NULL;
}
elseif ($thumbnails_queued && !$from_queue) {
return $this->get('thumbnail')->height;
}
$source = $this->getSource();
return $source->getMetadata($this, $source->getPluginDefinition()['thumbnail_height_metadata_attribute']);
}
/**
* Determines if the source field value has changed.
*
* The comparison uses MediaSourceInterface::getSourceFieldValue() to ensure
* that the correct property from the source field is used.
*
* @return bool
* TRUE if the source field value changed, FALSE otherwise.
*
* @see \Drupal\media\MediaSourceInterface::getSourceFieldValue()
*
* @internal
*/
protected function hasSourceFieldChanged() {
$source = $this->getSource();
return isset($this->original) && $source->getSourceFieldValue($this) !== $source->getSourceFieldValue($this->original);
}
/**
* Determines if the thumbnail should be updated for a media item.
*
* @param bool $is_new
* Specifies whether the media item is new.
*
* @return bool
* TRUE if the thumbnail should be updated, FALSE otherwise.
*/
protected function shouldUpdateThumbnail($is_new = FALSE) {
// Update thumbnail if we don't have a thumbnail yet or when the source
// field value changes.
return !$this->get('thumbnail')->entity || $is_new || $this->hasSourceFieldChanged();
}
/**
* {@inheritdoc}
*/
public function preSave(EntityStorageInterface $storage) {
parent::preSave($storage);
if (!$this->getOwner()) {
$this->setOwnerId(0);
}
// If no thumbnail has been explicitly set, use the default thumbnail.
if ($this->get('thumbnail')->isEmpty()) {
$this->thumbnail->target_id = $this->loadThumbnail()->id();
}
}
/**
* {@inheritdoc}
*/
public function postSave(EntityStorageInterface $storage, $update = TRUE) {
parent::postSave($storage, $update);
$is_new = !$update;
foreach ($this->translations as $langcode => $data) {
if ($this->hasTranslation($langcode)) {
$translation = $this->getTranslation($langcode);
if ($translation->bundle->entity->thumbnailDownloadsAreQueued() && $translation->shouldUpdateThumbnail($is_new)) {
\Drupal::queue('media_entity_thumbnail')->createItem(['id' => $translation->id()]);
}
}
}
}
/**
* {@inheritdoc}
*/
public function preSaveRevision(EntityStorageInterface $storage, \stdClass $record) {
parent::preSaveRevision($storage, $record);
if (!$this->isNewRevision() && isset($this->original) && empty($record->revision_log_message)) {
// If we are updating an existing media item without adding a
// new revision, we need to make sure $entity->revision_log_message is
// reset whenever it is empty.
// Therefore, this code allows us to avoid clobbering an existing log
// entry with an empty one.
$this->setRevisionLogMessage($this->original->getRevisionLogMessage());
}
}
/**
* Sets the media entity's field values from the source's metadata.
*
* Fetching the metadata could be slow (e.g., if requesting it from a remote
* API), so this is called by \Drupal\media\MediaStorage::save() prior to it
* beginning the database transaction, whereas static::preSave() executes
* after the transaction has already started.
*
* @internal
* Expose this as an API in
* https://www.drupal.org/project/drupal/issues/2992426.
*/
public function prepareSave() {
// @todo If the source plugin talks to a remote API (e.g. oEmbed), this code
// might be performing a fair number of HTTP requests. This is dangerously
// brittle and should probably be handled by a queue, to avoid doing HTTP
// operations during entity save. See
// https://www.drupal.org/project/drupal/issues/2976875 for more.
// In order for metadata to be mapped correctly, $this->original must be
// set. However, that is only set once parent::save() is called, so work
// around that by setting it here.
if (!isset($this->original) && $id = $this->id()) {
$this->original = $this->entityTypeManager()
->getStorage('media')
->loadUnchanged($id);
}
$media_source = $this->getSource();
foreach ($this->translations as $langcode => $data) {
if ($this->hasTranslation($langcode)) {
$translation = $this->getTranslation($langcode);
// Try to set fields provided by the media source and mapped in
// media type config.
foreach ($translation->bundle->entity->getFieldMap() as $metadata_attribute_name => $entity_field_name) {
// Only save value in the entity if the field is empty or if the
// source field changed.
if ($translation->hasField($entity_field_name) && ($translation->get($entity_field_name)->isEmpty() || $translation->hasSourceFieldChanged())) {
$translation->set($entity_field_name, $media_source->getMetadata($translation, $metadata_attribute_name));
}
}
// Try to set a default name for this media item if no name is provided.
if ($translation->get('name')->isEmpty()) {
$translation->setName($translation->getName());
}
// Set thumbnail.
if ($translation->shouldUpdateThumbnail($this->isNew())) {
$translation->updateThumbnail();
}
}
}
}
/**
* {@inheritdoc}
*/
public function validate() {
$media_source = $this->getSource();
if ($media_source instanceof MediaSourceEntityConstraintsInterface) {
$entity_constraints = $media_source->getEntityConstraints();
$this->getTypedData()->getDataDefinition()->setConstraints($entity_constraints);
}
if ($media_source instanceof MediaSourceFieldConstraintsInterface) {
$source_field_name = $media_source->getConfiguration()['source_field'];
$source_field_constraints = $media_source->getSourceFieldConstraints();
$this->get($source_field_name)->getDataDefinition()->setConstraints($source_field_constraints);
}
return parent::validate();
}
/**
* {@inheritdoc}
*/
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$fields += static::ownerBaseFieldDefinitions($entity_type);
$fields['name'] = BaseFieldDefinition::create('string')
->setLabel(t('Name'))
->setRequired(TRUE)
->setTranslatable(TRUE)
->setRevisionable(TRUE)
->setDefaultValue('')
->setSetting('max_length', 255)
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => -5,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['thumbnail'] = BaseFieldDefinition::create('image')
->setLabel(t('Thumbnail'))
->setDescription(t('The thumbnail of the media item.'))
->setRevisionable(TRUE)
->setTranslatable(TRUE)
->setDisplayOptions('view', [
'type' => 'image',
'weight' => 5,
'label' => 'hidden',
'settings' => [
'image_style' => 'thumbnail',
],
])
->setDisplayConfigurable('view', TRUE)
->setReadOnly(TRUE);
$fields['uid']
->setLabel(t('Authored by'))
->setDescription(t('The user ID of the author.'))
->setRevisionable(TRUE)
->setDisplayOptions('form', [
'type' => 'entity_reference_autocomplete',
'weight' => 5,
'settings' => [
'match_operator' => 'CONTAINS',
'size' => '60',
'autocomplete_type' => 'tags',
'placeholder' => '',
],
])
->setDisplayConfigurable('form', TRUE)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'author',
'weight' => 0,
])
->setDisplayConfigurable('view', TRUE);
$fields['status']
->setDisplayOptions('form', [
'type' => 'boolean_checkbox',
'settings' => [
'display_label' => TRUE,
],
'weight' => 100,
])
->setDisplayConfigurable('form', TRUE);
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Authored on'))
->setDescription(t('The time the media item was created.'))
->setTranslatable(TRUE)
->setRevisionable(TRUE)
->setDefaultValueCallback(static::class . '::getRequestTime')
->setDisplayOptions('form', [
'type' => 'datetime_timestamp',
'weight' => 10,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'timestamp',
'weight' => 0,
])
->setDisplayConfigurable('view', TRUE);
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Changed'))
->setDescription(t('The time the media item was last edited.'))
->setTranslatable(TRUE)
->setRevisionable(TRUE);
return $fields;
}
/**
* {@inheritdoc}
*/
public static function getRequestTime() {
return \Drupal::time()->getRequestTime();
}
}

View File

@@ -0,0 +1,244 @@
<?php
namespace Drupal\media\Entity;
use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
use Drupal\Core\Entity\EntityWithPluginCollectionInterface;
use Drupal\Core\Plugin\DefaultSingleLazyPluginCollection;
use Drupal\media\MediaTypeInterface;
/**
* Defines the Media type configuration entity.
*
* @ConfigEntityType(
* id = "media_type",
* label = @Translation("Media type"),
* label_collection = @Translation("Media types"),
* label_singular = @Translation("media type"),
* label_plural = @Translation("media types"),
* label_count = @PluralTranslation(
* singular = "@count media type",
* plural = "@count media types"
* ),
* handlers = {
* "access" = "Drupal\media\MediaTypeAccessControlHandler",
* "form" = {
* "add" = "Drupal\media\MediaTypeForm",
* "edit" = "Drupal\media\MediaTypeForm",
* "delete" = "Drupal\media\Form\MediaTypeDeleteConfirmForm"
* },
* "list_builder" = "Drupal\media\MediaTypeListBuilder",
* "route_provider" = {
* "html" = "Drupal\Core\Entity\Routing\DefaultHtmlRouteProvider",
* "permissions" = "Drupal\user\Entity\EntityPermissionsRouteProvider",
* }
* },
* admin_permission = "administer media types",
* config_prefix = "type",
* bundle_of = "media",
* entity_keys = {
* "id" = "id",
* "label" = "label",
* "status" = "status",
* },
* config_export = {
* "id",
* "label",
* "description",
* "source",
* "queue_thumbnail_downloads",
* "new_revision",
* "source_configuration",
* "field_map",
* "status",
* },
* links = {
* "add-form" = "/admin/structure/media/add",
* "edit-form" = "/admin/structure/media/manage/{media_type}",
* "delete-form" = "/admin/structure/media/manage/{media_type}/delete",
* "entity-permissions-form" = "/admin/structure/media/manage/{media_type}/permissions",
* "collection" = "/admin/structure/media",
* },
* constraints = {
* "ImmutableProperties" = {"id", "source"},
* "MediaMappingsConstraint" = { },
* }
* )
*/
class MediaType extends ConfigEntityBundleBase implements MediaTypeInterface, EntityWithPluginCollectionInterface {
/**
* The machine name of this media type.
*
* @var string
*/
protected $id;
/**
* The human-readable name of the media type.
*
* @var string
*/
protected $label;
/**
* A brief description of this media type.
*
* @var string
*/
protected $description;
/**
* The media source ID.
*
* @var string
*/
protected $source;
/**
* Whether media items should be published by default.
*
* @var bool
*/
protected $status = TRUE;
/**
* Whether thumbnail downloads are queued.
*
* @var bool
*
* @see \Drupal\media\MediaTypeInterface::thumbnailDownloadsAreQueued()
*/
protected $queue_thumbnail_downloads = FALSE;
/**
* Default value of the 'Create new revision' checkbox of this media type.
*
* @var bool
*/
protected $new_revision = FALSE;
/**
* The media source configuration.
*
* A media source can provide a configuration form with source plugin-specific
* configuration settings, which must at least include a source_field element
* containing a the name of the source field for the media type. The source
* configuration is defined by, and used to load, the source plugin. See
* \Drupal\media\MediaTypeInterface for an explanation of media sources.
*
* @var array
*
* @see \Drupal\media\MediaTypeInterface::getSource()
*/
protected $source_configuration = [];
/**
* Lazy collection for the media source.
*
* @var \Drupal\Core\Plugin\DefaultSingleLazyPluginCollection
*/
protected $sourcePluginCollection;
/**
* The metadata field map.
*
* @var array
*
* @see \Drupal\media\MediaTypeInterface::getFieldMap()
*/
protected $field_map = [];
/**
* {@inheritdoc}
*/
public function getPluginCollections() {
return [
'source_configuration' => $this->sourcePluginCollection(),
];
}
/**
* {@inheritdoc}
*/
public function getDescription() {
return $this->description;
}
/**
* {@inheritdoc}
*/
public function setDescription($description) {
return $this->set('description', $description);
}
/**
* {@inheritdoc}
*/
public function thumbnailDownloadsAreQueued() {
return $this->queue_thumbnail_downloads;
}
/**
* {@inheritdoc}
*/
public function setQueueThumbnailDownloadsStatus($queue_thumbnail_downloads) {
return $this->set('queue_thumbnail_downloads', $queue_thumbnail_downloads);
}
/**
* {@inheritdoc}
*/
public function getSource() {
return $this->sourcePluginCollection()->get($this->source);
}
/**
* Returns media source lazy plugin collection.
*
* @return \Drupal\Core\Plugin\DefaultSingleLazyPluginCollection|null
* The tag plugin collection or NULL if the plugin ID was not set yet.
*/
protected function sourcePluginCollection() {
if (!$this->sourcePluginCollection && $this->source) {
$this->sourcePluginCollection = new DefaultSingleLazyPluginCollection(\Drupal::service('plugin.manager.media.source'), $this->source, $this->source_configuration);
}
return $this->sourcePluginCollection;
}
/**
* {@inheritdoc}
*/
public function getStatus() {
return $this->status;
}
/**
* {@inheritdoc}
*/
public function shouldCreateNewRevision() {
return $this->new_revision;
}
/**
* {@inheritdoc}
*/
public function setNewRevision($new_revision) {
return $this->set('new_revision', $new_revision);
}
/**
* {@inheritdoc}
*/
public function getFieldMap() {
return $this->field_map;
}
/**
* {@inheritdoc}
*/
public function setFieldMap(array $map) {
return $this->set('field_map', $map);
}
}

View File

@@ -0,0 +1,92 @@
<?php
namespace Drupal\media\EventSubscriber;
use Drupal\Core\Cache\CacheTagsInvalidatorInterface;
use Drupal\Core\Config\ConfigCrudEvent;
use Drupal\Core\Config\ConfigEvents;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Routing\RouteBuilderInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Listens to the config save event for media.settings.
*/
class MediaConfigSubscriber implements EventSubscriberInterface {
/**
* The route builder.
*
* @var \Drupal\Core\Routing\RouteBuilderInterface
*/
protected $routeBuilder;
/**
* The cache tags invalidator.
*
* @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface
*/
protected $cacheTagsInvalidator;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Constructs the MediaConfigSubscriber.
*
* @param \Drupal\Core\Routing\RouteBuilderInterface $router_builder
* The route builder.
* @param \Drupal\Core\Cache\CacheTagsInvalidatorInterface $cache_tags_invalidator
* The cache tags invalidator.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*/
public function __construct(RouteBuilderInterface $router_builder, CacheTagsInvalidatorInterface $cache_tags_invalidator, EntityTypeManagerInterface $entity_type_manager) {
$this->routeBuilder = $router_builder;
$this->cacheTagsInvalidator = $cache_tags_invalidator;
$this->entityTypeManager = $entity_type_manager;
}
/**
* Updates entity type definitions and ensures routes are rebuilt when needed.
*
* @param \Drupal\Core\Config\ConfigCrudEvent $event
* The ConfigCrudEvent to process.
*/
public function onSave(ConfigCrudEvent $event) {
$saved_config = $event->getConfig();
if ($saved_config->getName() === 'media.settings' && $event->isChanged('standalone_url')) {
$this->cacheTagsInvalidator->invalidateTags([
// The configuration change triggers entity type definition changes,
// which in turn triggers routes to appear or disappear.
// @see media_entity_type_alter()
'entity_types',
// The 'rendered' cache tag needs to be explicitly invalidated to ensure
// that all links to Media entities are re-rendered. Ideally, this would
// not be necessary; invalidating the 'entity_types' cache tag should be
// sufficient. But that cache tag would then need to be on nearly
// everything, resulting in excessive complexity. We prefer pragmatism.
'rendered',
]);
// @todo Remove this when invalidating the 'entity_types' cache tag is
// respected by the entity type plugin manager. See
// https://www.drupal.org/project/drupal/issues/3001284 and
// https://www.drupal.org/project/drupal/issues/3013659.
$this->entityTypeManager->clearCachedDefinitions();
$this->routeBuilder->setRebuildNeeded();
}
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents(): array {
$events[ConfigEvents::SAVE][] = ['onSave'];
return $events;
}
}

View File

@@ -0,0 +1,338 @@
<?php
namespace Drupal\media\Form;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\editor\EditorInterface;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\HtmlCommand;
use Drupal\editor\Ajax\EditorDialogSave;
use Drupal\Core\Ajax\CloseModalDialogCommand;
use Drupal\filter\Plugin\FilterInterface;
use Drupal\image\Plugin\Field\FieldType\ImageItem;
use Drupal\media\MediaInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
/**
* Provides a media embed dialog for text editors.
*
* Depending on the configuration of the filters associated with the text
* editor, this dialog allows users to set the alt text, alignment, and
* captioning status for embedded media items.
*
* @deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. There is no
* replacement.
*
* @see https://www.drupal.org/node/3291493
*
* @internal
* This is an internal part of the media system in Drupal core and may be
* subject to change in minor releases. This class should not be
* instantiated or extended by external code.
*/
class EditorMediaDialog extends FormBase {
/**
* The entity repository.
*
* @var \Drupal\Core\Entity\EntityRepositoryInterface
*/
protected $entityRepository;
/**
* The entity display repository.
*
* @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface
*/
protected $entityDisplayRepository;
/**
* Constructs a EditorMediaDialog object.
*
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
* The entity repository.
* @param \Drupal\Core\Entity\EntityDisplayRepositoryInterface $entity_display_repository
* The entity display repository.
*/
public function __construct(EntityRepositoryInterface $entity_repository, EntityDisplayRepositoryInterface $entity_display_repository) {
@trigger_error(__NAMESPACE__ . '\EditorMediaDialog is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. There is no replacement. See https://www.drupal.org/node/3291493', E_USER_DEPRECATED);
$this->entityRepository = $entity_repository;
$this->entityDisplayRepository = $entity_display_repository;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity.repository'),
$container->get('entity_display.repository')
);
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'editor_media_dialog';
}
/**
* {@inheritdoc}
*
* @param array $form
* A nested array form elements comprising the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
* @param \Drupal\editor\EditorInterface $editor
* The text editor to which this dialog corresponds.
*/
public function buildForm(array $form, FormStateInterface $form_state, ?EditorInterface $editor = NULL) {
// This form is special, in that the default values do not come from the
// server side, but from the client side, from a text editor. We must cache
// this data in form state, because when the form is rebuilt, we will be
// receiving values from the form, instead of the values from the text
// editor. If we don't cache it, this data will be lost. By convention,
// the data that the text editor sends to any dialog is in the
// 'editor_object' key.
if (isset($form_state->getUserInput()['editor_object'])) {
$editor_object = $form_state->getUserInput()['editor_object'];
// The data that the text editor sends to any dialog is in
// the 'editor_object' key.
$media_embed_element = $editor_object['attributes'];
$form_state->set('media_embed_element', $media_embed_element);
$has_caption = $editor_object['hasCaption'];
$form_state
->set('hasCaption', $has_caption)
->setCached(TRUE);
}
else {
// Retrieve the user input from form state.
$media_embed_element = $form_state->get('media_embed_element');
$has_caption = $form_state->get('hasCaption');
}
$form['#tree'] = TRUE;
$form['#attached']['library'][] = 'editor/drupal.editor.dialog';
$form['#prefix'] = '<div id="editor-media-dialog-form">';
$form['#suffix'] = '</div>';
$filters = $editor->getFilterFormat()->filters();
$filter_html = $filters->get('filter_html');
$filter_align = $filters->get('filter_align');
$filter_caption = $filters->get('filter_caption');
$media_embed_filter = $filters->get('media_embed');
$allowed_attributes = [];
if ($filter_html->status) {
$restrictions = $filter_html->getHTMLRestrictions();
$allowed_attributes = $restrictions['allowed']['drupal-media'];
}
$media = $this->entityRepository->loadEntityByUuid('media', $media_embed_element['data-entity-uuid']);
if ($image_field_name = $this->getMediaImageSourceFieldName($media)) {
// We'll want the alt text from the same language as the host.
if (!empty($editor_object['hostEntityLangcode']) && $media->hasTranslation($editor_object['hostEntityLangcode'])) {
$media = $media->getTranslation($editor_object['hostEntityLangcode']);
}
$settings = $media->{$image_field_name}->getItemDefinition()->getSettings();
$alt = $media_embed_element['alt'] ?? NULL;
$form['alt'] = [
'#type' => 'textfield',
'#title' => $this->t('Alternate text'),
'#default_value' => $alt,
'#description' => $this->t('Short description of the image used by screen readers and displayed when the image is not loaded. This is important for accessibility.'),
'#required_error' => $this->t('Alternative text is required.<br />(Only in rare cases should this be left empty. To create empty alternative text, enter <code>""</code> — two double quotes without any content).'),
'#maxlength' => 2048,
'#placeholder' => $media->{$image_field_name}->alt,
'#parents' => ['attributes', 'alt'],
'#access' => !empty($settings['alt_field']) && ($filter_html->status === FALSE || !empty($allowed_attributes['alt'])),
];
}
// When Drupal core's filter_align is being used, the text editor offers the
// ability to change the alignment.
$form['align'] = [
'#title' => $this->t('Align'),
'#type' => 'radios',
'#options' => [
'none' => $this->t('None'),
'left' => $this->t('Left'),
'center' => $this->t('Center'),
'right' => $this->t('Right'),
],
'#default_value' => empty($media_embed_element['data-align']) ? 'none' : $media_embed_element['data-align'],
'#attributes' => ['class' => ['container-inline']],
'#parents' => ['attributes', 'data-align'],
'#access' => $filter_align->status && ($filter_html->status === FALSE || !empty($allowed_attributes['data-align'])),
];
// When Drupal core's filter_caption is being used, the text editor offers
// the ability to in-place edit the media's caption: show a toggle.
$form['caption'] = [
'#title' => $this->t('Caption'),
'#type' => 'checkbox',
'#default_value' => $has_caption === 'true',
'#parents' => ['hasCaption'],
'#access' => $filter_caption->status && ($filter_html->status === FALSE || !empty($allowed_attributes['data-caption'])),
];
$view_mode_options = array_intersect_key($this->entityDisplayRepository->getViewModeOptionsByBundle('media', $media->bundle()), $media_embed_filter->settings['allowed_view_modes']);
$default_view_mode = static::getViewModeDefaultValue($view_mode_options, $media_embed_filter, $media_embed_element['data-view-mode'] ?? NULL);
$form['view_mode'] = [
'#title' => $this->t("Display"),
'#type' => 'select',
'#options' => $view_mode_options,
'#default_value' => $default_view_mode,
'#parents' => ['attributes', 'data-view-mode'],
'#access' => count($view_mode_options) >= 2,
];
// Store the default from the MediaEmbed filter, so that if the selected
// view mode matches the default, we can drop the 'data-view-mode'
// attribute.
$form_state->set('filter_default_view_mode', $media_embed_filter->settings['default_view_mode']);
if ((empty($form['alt']) || $form['alt']['#access'] === FALSE) && $form['align']['#access'] === FALSE && $form['caption']['#access'] === FALSE && $form['view_mode']['#access'] === FALSE) {
$format = $editor->getFilterFormat();
$warning = $this->t('There is nothing to configure for this media.');
$form['no_access_notice'] = ['#markup' => $warning];
if ($format->access('update')) {
$arguments = [
'@warning' => $warning,
'@edit_url' => $format->toUrl('edit-form')->toString(),
'%format' => $format->label(),
];
$form['no_access_notice']['#markup'] = $this->t('@warning <a href="@edit_url">Edit the text format %format</a> to modify the attributes that can be overridden.', $arguments);
}
}
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['save_modal'] = [
'#type' => 'submit',
'#value' => $this->t('Save'),
// No regular submit-handler. This form only works via JavaScript.
'#submit' => [],
'#ajax' => [
'callback' => '::submitForm',
'event' => 'click',
],
// Prevent this hidden element from being tabbable.
'#attributes' => [
'tabindex' => -1,
],
];
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$response = new AjaxResponse();
// When the `alt` attribute is set to two double quotes, transform it to the
// empty string: two double quotes signify "empty alt attribute". See above.
if (trim($form_state->getValue(['attributes', 'alt'], '')) === '""') {
$form_state->setValue(['attributes', 'alt'], '""');
}
// The `alt` attribute is optional: if it isn't set, the default value
// simply will not be overridden. It's important to set it to FALSE
// instead of unsetting the value. This way we explicitly inform
// the client side about the new value.
if ($form_state->hasValue(['attributes', 'alt']) && trim($form_state->getValue(['attributes', 'alt'])) === '') {
$form_state->setValue(['attributes', 'alt'], FALSE);
}
// If the selected view mode matches the default on the filter, remove the
// attribute.
if (!empty($form_state->get('filter_default_view_mode')) && $form_state->getValue(['attributes', 'data-view-mode']) === $form_state->get('filter_default_view_mode')) {
$form_state->setValue(['attributes', 'data-view-mode'], FALSE);
}
if ($form_state->getErrors()) {
unset($form['#prefix'], $form['#suffix']);
$form['status_messages'] = [
'#type' => 'status_messages',
'#weight' => -10,
];
$response->addCommand(new HtmlCommand('#editor-media-dialog-form', $form));
}
else {
// Only send back the relevant values.
$values = [
'hasCaption' => $form_state->getValue('hasCaption'),
'attributes' => $form_state->getValue('attributes'),
];
$response->addCommand(new EditorDialogSave($values));
$response->addCommand(new CloseModalDialogCommand());
}
return $response;
}
/**
* Gets the default value for the view mode form element.
*
* @param array $view_mode_options
* The array of options for the view mode form element.
* @param \Drupal\filter\Plugin\FilterInterface $media_embed_filter
* The media embed filter.
* @param string $media_element_view_mode_attribute
* The data-view-mode attribute on the <drupal-media> element.
*
* @return string|null
* The default value for the view mode form element.
*/
public static function getViewModeDefaultValue(array $view_mode_options, FilterInterface $media_embed_filter, $media_element_view_mode_attribute) {
// The select element won't display without at least two options, so if
// that's the case, just return NULL.
if (count($view_mode_options) < 2) {
return NULL;
}
$filter_default_view_mode = $media_embed_filter->settings['default_view_mode'];
// If the current media embed ($media_embed_element) has a set view mode,
// we want to use that as the default in the select form element,
// otherwise we'll want to use the default for all embedded media.
if (!empty($media_element_view_mode_attribute) && array_key_exists($media_element_view_mode_attribute, $view_mode_options)) {
return $media_element_view_mode_attribute;
}
elseif (array_key_exists($filter_default_view_mode, $view_mode_options)) {
return $filter_default_view_mode;
}
return NULL;
}
/**
* Gets the name of an image media item's source field.
*
* @param \Drupal\media\MediaInterface $media
* The media item being embedded.
*
* @return string|null
* The name of the image source field configured for the media item, or
* NULL if the source field is not an image field.
*/
protected function getMediaImageSourceFieldName(MediaInterface $media) {
$field_definition = $media->getSource()
->getSourceFieldDefinition($media->bundle->entity);
$item_class = $field_definition->getItemDefinition()->getClass();
if (is_a($item_class, ImageItem::class, TRUE)) {
return $field_definition->getName();
}
return NULL;
}
}

View File

@@ -0,0 +1,121 @@
<?php
namespace Drupal\media\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\TypedConfigManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\ConfigTarget;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\media\IFrameUrlHelper;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a form to configure Media settings.
*
* @internal
*/
class MediaSettingsForm extends ConfigFormBase {
/**
* The iFrame URL helper service.
*
* @var \Drupal\media\IFrameUrlHelper
*/
protected $iFrameUrlHelper;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* MediaSettingsForm constructor.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory service.
* @param \Drupal\Core\Config\TypedConfigManagerInterface $typedConfigManager
* The typed config manager.
* @param \Drupal\media\IFrameUrlHelper $iframe_url_helper
* The iFrame URL helper service.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*/
public function __construct(ConfigFactoryInterface $config_factory, TypedConfigManagerInterface $typedConfigManager, IFrameUrlHelper $iframe_url_helper, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($config_factory, $typedConfigManager);
$this->iFrameUrlHelper = $iframe_url_helper;
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('config.factory'),
$container->get('config.typed'),
$container->get('media.oembed.iframe_url_helper'),
$container->get('entity_type.manager')
);
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'media_settings_form';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return ['media.settings'];
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$domain = $this->config('media.settings')->get('iframe_domain');
if (!$this->iFrameUrlHelper->isSecure($domain)) {
$message = $this->t('It is potentially insecure to display oEmbed content in a frame that is served from the same domain as your main Drupal site, as this may allow execution of third-party code. Refer to <a href="https://oembed.com/#section3">oEmbed Security Considerations</a>.');
$this->messenger()->addWarning($message);
}
$description = '<p>' . $this->t('Displaying media assets from third-party services, such as YouTube or Twitter, can be risky. This is because many of these services return arbitrary HTML to represent those assets, and that HTML may contain executable JavaScript code. If handled improperly, this can increase the risk of your site being compromised.') . '</p>';
$description .= '<p>' . $this->t('In order to mitigate the risks, third-party assets are displayed in an iFrame, which effectively sandboxes any executable code running inside it. For even more security, the iFrame can be served from an alternate domain (that also points to your Drupal site), which you can configure on this page. This helps safeguard cookies and other sensitive information.') . '</p>';
$form['security'] = [
'#type' => 'details',
'#title' => $this->t('Security'),
'#description' => $description,
'#open' => TRUE,
];
// @todo Figure out how and if we should validate that this domain actually
// points back to Drupal.
// See https://www.drupal.org/project/drupal/issues/2965979 for more info.
$form['security']['iframe_domain'] = [
'#type' => 'url',
'#title' => $this->t('iFrame domain'),
'#size' => 40,
'#maxlength' => 255,
'#config_target' => new ConfigTarget('media.settings', 'iframe_domain', toConfig: fn(?string $value) => $value ?: NULL),
'#description' => $this->t('Enter a different domain from which to serve oEmbed content, including the <em>http://</em> or <em>https://</em> prefix. This domain needs to point back to this site, or existing oEmbed content may not display correctly, or at all.'),
];
$form['security']['standalone_url'] = [
'#prefix' => '<hr>',
'#type' => 'checkbox',
'#title' => $this->t('Standalone media URL'),
'#config_target' => 'media.settings:standalone_url',
'#description' => $this->t("Allow users to access @media-entities at /media/{id}.", ['@media-entities' => $this->entityTypeManager->getDefinition('media')->getPluralLabel()]),
];
return parent::buildForm($form, $form_state);
}
}

View File

@@ -0,0 +1,71 @@
<?php
namespace Drupal\media\Form;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityDeleteForm;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a form for media type deletion.
*
* @internal
*/
class MediaTypeDeleteConfirmForm extends EntityDeleteForm {
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Constructs a new MediaTypeDeleteConfirm object.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity_type.manager')
);
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$num_entities = $this->entityTypeManager->getStorage('media')->getQuery()
->accessCheck(FALSE)
->condition('bundle', $this->entity->id())
->count()
->execute();
if ($num_entities) {
$form['#title'] = $this->getQuestion();
$form['description'] = [
'#type' => 'inline_template',
'#template' => '<p>{{ message }}</p>',
'#context' => [
'message' => $this->formatPlural($num_entities,
'%type is used by @count media item on your site. You can not remove this media type until you have removed all of the %type media items.',
'%type is used by @count media items on your site. You can not remove this media type until you have removed all of the %type media items.',
['%type' => $this->entity->label()]),
],
];
return $form;
}
return parent::buildForm($form, $form_state);
}
}

View File

@@ -0,0 +1,24 @@
<?php
namespace Drupal\media;
use Drupal\Component\Render\MarkupInterface;
use Drupal\Component\Render\MarkupTrait;
/**
* Defines an object that wraps oEmbed markup for use in an iFrame.
*
* This object is not constructed with a known safe string as the strings come
* from an external site. It must not be used outside the Media module's oEmbed
* iframe rendering.
*
* @internal
* This object is an internal part of the oEmbed system and should only be
* used in \Drupal\media\Controller\OEmbedIframeController.
*
* @see \Drupal\media\Controller\OEmbedIframeController
*/
class IFrameMarkup implements MarkupInterface {
use MarkupTrait;
}

View File

@@ -0,0 +1,84 @@
<?php
namespace Drupal\media;
use Drupal\Component\Utility\Crypt;
use Drupal\Core\PrivateKey;
use Drupal\Core\Routing\RequestContext;
use Drupal\Core\Site\Settings;
/**
* Providers helper functions for displaying oEmbed resources in an iFrame.
*
* @internal
* This is an internal part of the oEmbed system and should only be used by
* oEmbed-related code in Drupal core.
*/
class IFrameUrlHelper {
/**
* The request context service.
*
* @var \Drupal\Core\Routing\RequestContext
*/
protected $requestContext;
/**
* The private key service.
*
* @var \Drupal\Core\PrivateKey
*/
protected $privateKey;
/**
* IFrameUrlHelper constructor.
*
* @param \Drupal\Core\Routing\RequestContext $request_context
* The request context service.
* @param \Drupal\Core\PrivateKey $private_key
* The private key service.
*/
public function __construct(RequestContext $request_context, PrivateKey $private_key) {
$this->requestContext = $request_context;
$this->privateKey = $private_key;
}
/**
* Hashes an oEmbed resource URL.
*
* @param string $url
* The resource URL.
* @param int $max_width
* (optional) The maximum width of the resource.
* @param int $max_height
* (optional) The maximum height of the resource.
*
* @return string
* The hashed URL.
*/
public function getHash($url, $max_width = NULL, $max_height = NULL) {
return Crypt::hmacBase64("$url:$max_width:$max_height", $this->privateKey->get() . Settings::getHashSalt());
}
/**
* Checks if an oEmbed URL can be securely displayed in an frame.
*
* @param string $url
* The URL to check.
*
* @return bool
* TRUE if the URL is considered secure, otherwise FALSE.
*/
public function isSecure($url) {
if (!$url) {
return FALSE;
}
$url_host = parse_url($url, PHP_URL_HOST);
$system_host = parse_url($this->requestContext->getCompleteBaseUrl(), PHP_URL_HOST);
// The URL is secure if its domain is not the same as the domain of the base
// URL of the current request.
return $url_host && $system_host && $url_host !== $system_host;
}
}

View File

@@ -0,0 +1,162 @@
<?php
namespace Drupal\media;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityAccessControlHandler;
use Drupal\Core\Entity\EntityHandlerInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Defines an access control handler for media items.
*/
class MediaAccessControlHandler extends EntityAccessControlHandler implements EntityHandlerInterface {
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Constructs a MediaAccessControlHandler object.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type definition.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*/
public function __construct(EntityTypeInterface $entity_type, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($entity_type);
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@inheritdoc}
*/
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static(
$entity_type,
$container->get('entity_type.manager'),
);
}
/**
* {@inheritdoc}
*/
protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
/** @var \Drupal\media\MediaInterface $entity */
// Allow admin permission to override all operations.
if ($account->hasPermission($this->entityType->getAdminPermission())) {
return AccessResult::allowed()->cachePerPermissions();
}
$type = $entity->bundle();
$is_owner = ($account->id() && $account->id() === $entity->getOwnerId());
switch ($operation) {
case 'view':
if ($entity->isPublished()) {
$access_result = AccessResult::allowedIf($account->hasPermission('view media'))
->cachePerPermissions()
->addCacheableDependency($entity);
if (!$access_result->isAllowed()) {
$access_result->setReason("The 'view media' permission is required when the media item is published.");
}
}
elseif ($account->hasPermission('view own unpublished media')) {
$access_result = AccessResult::allowedIf($is_owner)
->cachePerPermissions()
->cachePerUser()
->addCacheableDependency($entity);
if (!$access_result->isAllowed()) {
$access_result->setReason("The user must be the owner and the 'view own unpublished media' permission is required when the media item is unpublished.");
}
}
else {
$access_result = AccessResult::neutral()
->cachePerPermissions()
->addCacheableDependency($entity)
->setReason("The user must be the owner and the 'view own unpublished media' permission is required when the media item is unpublished.");
}
return $access_result;
case 'update':
if ($account->hasPermission('edit any ' . $type . ' media')) {
return AccessResult::allowed()->cachePerPermissions();
}
if ($account->hasPermission('edit own ' . $type . ' media') && $is_owner) {
return AccessResult::allowed()->cachePerPermissions()->cachePerUser()->addCacheableDependency($entity);
}
// @todo Deprecate this permission in
// https://www.drupal.org/project/drupal/issues/2925459.
if ($account->hasPermission('update any media')) {
return AccessResult::allowed()->cachePerPermissions();
}
if ($account->hasPermission('update media') && $is_owner) {
return AccessResult::allowed()->cachePerPermissions()->cachePerUser()->addCacheableDependency($entity);
}
return AccessResult::neutral("The following permissions are required: 'update any media' OR 'update own media' OR '$type: edit any media' OR '$type: edit own media'.")->cachePerPermissions();
case 'delete':
if ($account->hasPermission('delete any ' . $type . ' media')) {
return AccessResult::allowed()->cachePerPermissions();
}
if ($account->hasPermission('delete own ' . $type . ' media') && $is_owner) {
return AccessResult::allowed()->cachePerPermissions()->cachePerUser()->addCacheableDependency($entity);
}
// @todo Deprecate this permission in
// https://www.drupal.org/project/drupal/issues/2925459.
if ($account->hasPermission('delete any media')) {
return AccessResult::allowed()->cachePerPermissions();
}
if ($account->hasPermission('delete media') && $is_owner) {
return AccessResult::allowed()->cachePerPermissions()->cachePerUser()->addCacheableDependency($entity);
}
return AccessResult::neutral("The following permissions are required: 'delete any media' OR 'delete own media' OR '$type: delete any media' OR '$type: delete own media'.")->cachePerPermissions();
case 'view all revisions':
case 'view revision':
if ($account->hasPermission('view any ' . $type . ' media revisions') || $account->hasPermission("view all media revisions")) {
// Check the access to this revision and if the media passed in is not
// the default revision then access to that too.
$entity_access = $entity->access('view', $account, TRUE);
if (!$entity->isDefaultRevision()) {
$media_storage = $this->entityTypeManager->getStorage($entity->getEntityTypeId());
$entity_access->andIf($this->access($media_storage->load($entity->id()), 'view', $account, TRUE));
}
return AccessResult::allowed()->cachePerPermissions()->andIf($entity_access);
}
return AccessResult::neutral()->cachePerPermissions();
case 'revert':
return AccessResult::allowedIfHasPermission($account, 'revert any ' . $type . ' media revisions')
->cachePerPermissions()->addCacheableDependency($entity);
case 'delete revision':
return AccessResult::allowedIfHasPermission($account, 'delete any ' . $type . ' media revisions')
->cachePerPermissions()->addCacheableDependency($entity);
default:
return AccessResult::neutral()->cachePerPermissions();
}
}
/**
* {@inheritdoc}
*/
protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
$permissions = [
'administer media',
'create media',
'create ' . $entity_bundle . ' media',
];
return AccessResult::allowedIfHasPermissions($account, $permissions, 'OR');
}
}

View File

@@ -0,0 +1,72 @@
<?php
namespace Drupal\media;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
/**
* Provides a BC layer for modules providing old configurations.
*
* @internal
* This class is only meant to fix outdated media configuration and its
* methods should not be invoked directly. It will be removed once all the
* associated updates have been removed.
*/
class MediaConfigUpdater {
/**
* Flag determining whether deprecations should be triggered.
*
* @var bool
*/
private $deprecationsEnabled = FALSE;
/**
* Stores which deprecations were triggered.
*
* @var bool
*/
private $triggeredDeprecations = [];
/**
* Sets the deprecations enabling status.
*
* @param bool $enabled
* Whether deprecations should be enabled.
*/
public function setDeprecationsEnabled(bool $enabled): void {
$this->deprecationsEnabled = $enabled;
}
/**
* Processes oembed type fields.
*
* @param \Drupal\Core\Entity\Display\EntityViewDisplayInterface $view_display
* The view display.
*
* @return bool
* Whether the display was updated.
*/
public function processOembedEagerLoadField(EntityViewDisplayInterface $view_display): bool {
$changed = FALSE;
foreach ($view_display->getComponents() as $field => $component) {
if (array_key_exists('type', $component)
&& ($component['type'] === 'oembed')
&& !array_key_exists('loading', $component['settings'])) {
$component['settings']['loading']['attribute'] = 'eager';
$view_display->setComponent($field, $component);
$changed = TRUE;
}
}
$deprecations_triggered = &$this->triggeredDeprecations['3212351'][$view_display->id()];
if ($this->deprecationsEnabled && $changed && !$deprecations_triggered) {
$deprecations_triggered = TRUE;
@trigger_error(sprintf('The oEmbed loading attribute update for view display "%s" is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. Profile, module and theme provided configuration should be updated. See https://www.drupal.org/node/3275103', $view_display->id()), E_USER_DEPRECATED);
}
return $changed;
}
}

View File

@@ -0,0 +1,88 @@
<?php
namespace Drupal\media;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Form\FormStateInterface;
/**
* Form controller for the media edit forms.
*
* @internal
*/
class MediaForm extends ContentEntityForm {
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
/** @var \Drupal\media\MediaTypeInterface $media_type */
$media_type = $this->entity->bundle->entity;
if ($this->operation === 'edit') {
$form['#title'] = $this->t('Edit %type_label @label', [
'%type_label' => $media_type->label(),
'@label' => $this->entity->label(),
]);
}
// Media author information for administrators.
if (isset($form['uid']) || isset($form['created'])) {
$form['author'] = [
'#type' => 'details',
'#title' => $this->t('Authoring information'),
'#group' => 'advanced',
'#attributes' => [
'class' => ['media-form-author'],
],
'#weight' => 90,
'#optional' => TRUE,
];
}
if (isset($form['uid'])) {
$form['uid']['#group'] = 'author';
}
if (isset($form['created'])) {
$form['created']['#group'] = 'author';
}
$form['#attached']['library'][] = 'media/form';
return $form;
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
$saved = parent::save($form, $form_state);
$context = ['@type' => $this->entity->bundle(), '%label' => $this->entity->label(), 'link' => $this->entity->toLink($this->t('View'))->toString()];
$logger = $this->logger('media');
$t_args = ['@type' => $this->entity->bundle->entity->label(), '%label' => $this->entity->toLink($this->entity->label())->toString()];
if ($saved === SAVED_NEW) {
$logger->info('@type: added %label.', $context);
$this->messenger()->addStatus($this->t('@type %label has been created.', $t_args));
}
else {
$logger->info('@type: updated %label.', $context);
$this->messenger()->addStatus($this->t('@type %label has been updated.', $t_args));
}
// Redirect the user to the media overview if the user has the 'access media
// overview' permission. If not, redirect to the canonical URL of the media
// item.
if ($this->currentUser()->hasPermission('access media overview')) {
$form_state->setRedirectUrl($this->entity->toUrl('collection'));
}
else {
$form_state->setRedirectUrl($this->entity->toUrl());
}
return $saved;
}
}

View File

@@ -0,0 +1,67 @@
<?php
namespace Drupal\media;
use Drupal\Core\Entity\EntityChangedInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityPublishedInterface;
use Drupal\Core\Entity\RevisionLogInterface;
use Drupal\user\EntityOwnerInterface;
/**
* Provides an interface defining an entity for media items.
*/
interface MediaInterface extends ContentEntityInterface, EntityChangedInterface, RevisionLogInterface, EntityOwnerInterface, EntityPublishedInterface {
/**
* Gets the media item name.
*
* @return string
* The name of the media item.
*/
public function getName();
/**
* Sets the media item name.
*
* @param string $name
* The name of the media item.
*
* @return $this
*/
public function setName($name);
/**
* Returns the media item creation timestamp.
*
* @todo Remove and use the new interface when #2833378 is done.
* @see https://www.drupal.org/node/2833378
*
* @return int
* Creation timestamp of the media item.
*/
public function getCreatedTime();
/**
* Sets the media item creation timestamp.
*
* @todo Remove and use the new interface when #2833378 is done.
* @see https://www.drupal.org/node/2833378
*
* @param int $timestamp
* The media creation timestamp.
*
* @return $this
* The called media item.
*/
public function setCreatedTime($timestamp);
/**
* Returns the media source.
*
* @return \Drupal\media\MediaSourceInterface
* The media source.
*/
public function getSource();
}

View File

@@ -0,0 +1,162 @@
<?php
namespace Drupal\media;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityListBuilder;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a listing of media items.
*/
class MediaListBuilder extends EntityListBuilder {
/**
* The date formatter service.
*
* @var \Drupal\Core\Datetime\DateFormatterInterface
*/
protected $dateFormatter;
/**
* The language manager service.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;
/**
* Indicates whether the 'thumbnail' image style exists.
*
* @var bool
*/
protected $thumbnailStyleExists = FALSE;
/**
* Constructs a new MediaListBuilder object.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type definition.
* @param \Drupal\Core\Entity\EntityStorageInterface $storage
* The entity storage class.
* @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
* The date formatter service.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager service.
* @param \Drupal\Core\Entity\EntityStorageInterface $image_style_storage
* The entity storage class for image styles.
*/
public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, DateFormatterInterface $date_formatter, LanguageManagerInterface $language_manager, EntityStorageInterface $image_style_storage) {
parent::__construct($entity_type, $storage);
$this->dateFormatter = $date_formatter;
$this->languageManager = $language_manager;
$this->thumbnailStyleExists = !empty($image_style_storage->load('thumbnail'));
}
/**
* {@inheritdoc}
*/
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
$entity_type_manager = $container->get('entity_type.manager');
return new static(
$entity_type,
$entity_type_manager->getStorage($entity_type->id()),
$container->get('date.formatter'),
$container->get('language_manager'),
$entity_type_manager->getStorage('image_style')
);
}
/**
* {@inheritdoc}
*/
public function buildHeader() {
$header = [];
if ($this->thumbnailStyleExists) {
$header['thumbnail'] = [
'data' => $this->t('Thumbnail'),
'class' => [RESPONSIVE_PRIORITY_LOW],
];
}
$header += [
'name' => $this->t('Media Name'),
'type' => [
'data' => $this->t('Type'),
'class' => [RESPONSIVE_PRIORITY_MEDIUM],
],
'author' => [
'data' => $this->t('Author'),
'class' => [RESPONSIVE_PRIORITY_LOW],
],
'status' => $this->t('Status'),
'changed' => [
'data' => $this->t('Updated'),
'class' => [RESPONSIVE_PRIORITY_LOW],
],
];
// Enable language column if multiple languages are added.
if ($this->languageManager->isMultilingual()) {
$header['language'] = [
'data' => $this->t('Language'),
'class' => [RESPONSIVE_PRIORITY_LOW],
];
}
return $header + parent::buildHeader();
}
/**
* {@inheritdoc}
*/
public function buildRow(EntityInterface $entity) {
/** @var \Drupal\media\MediaInterface $entity */
if ($this->thumbnailStyleExists) {
$row['thumbnail'] = [];
if ($thumbnail_uri = $entity->getSource()->getMetadata($entity, 'thumbnail_uri')) {
$row['thumbnail']['data'] = [
'#theme' => 'image_style',
'#style_name' => 'thumbnail',
'#uri' => $thumbnail_uri,
'#height' => 50,
];
}
}
$row['name']['data'] = [
'#type' => 'link',
'#title' => $entity->label(),
'#url' => $entity->toUrl(),
];
$row['type'] = $entity->bundle->entity->label();
$row['author']['data'] = [
'#theme' => 'username',
'#account' => $entity->getOwner(),
];
$row['status'] = $entity->isPublished() ? $this->t('Published') : $this->t('Unpublished');
$row['changed'] = $this->dateFormatter->format($entity->getChangedTime(), 'short');
if ($this->languageManager->isMultilingual()) {
$row['language'] = $this->languageManager->getLanguageName($entity->language()->getId());
}
return $row + parent::buildRow($entity);
}
/**
* {@inheritdoc}
*/
protected function getEntityIds() {
$query = $this->getStorage()->getQuery()
->accessCheck(TRUE)
->sort('changed', 'DESC');
// Only add the pager if a limit is specified.
if ($this->limit) {
$query->pager($this->limit);
}
return $query->execute();
}
}

View File

@@ -0,0 +1,97 @@
<?php
namespace Drupal\media;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\BundlePermissionHandlerTrait;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides dynamic permissions for each media type.
*/
class MediaPermissions implements ContainerInjectionInterface {
use BundlePermissionHandlerTrait;
use StringTranslationTrait;
/**
* The entity type manager service.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* MediaPermissions constructor.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager service.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container->get('entity_type.manager'));
}
/**
* Returns an array of media type permissions.
*
* @return array
* The media type permissions.
*
* @see \Drupal\user\PermissionHandlerInterface::getPermissions()
*/
public function mediaTypePermissions() {
// Generate media permissions for all media types.
$media_types = $this->entityTypeManager->getStorage('media_type')->loadMultiple();
return $this->generatePermissions($media_types, [$this, 'buildPermissions']);
}
/**
* Returns a list of media permissions for a given media type.
*
* @param \Drupal\media\MediaTypeInterface $type
* The media type.
*
* @return array
* An associative array of permission names and descriptions.
*/
protected function buildPermissions(MediaTypeInterface $type) {
$type_id = $type->id();
$type_params = ['%type_name' => $type->label()];
return [
"create $type_id media" => [
'title' => $this->t('%type_name: Create new media', $type_params),
],
"edit own $type_id media" => [
'title' => $this->t('%type_name: Edit own media', $type_params),
],
"edit any $type_id media" => [
'title' => $this->t('%type_name: Edit any media', $type_params),
],
"delete own $type_id media" => [
'title' => $this->t('%type_name: Delete own media', $type_params),
],
"delete any $type_id media" => [
'title' => $this->t('%type_name: Delete any media', $type_params),
],
"view any $type_id media revisions" => [
'title' => $this->t('%type_name: View any media revision pages', $type_params),
],
"revert any $type_id media revisions" => [
'title' => $this->t('Revert %type_name: Revert media revisions', $type_params),
],
"delete any $type_id media revisions" => [
'title' => $this->t('Delete %type_name: Delete media revisions', $type_params),
],
];
}
}

View File

@@ -0,0 +1,368 @@
<?php
namespace Drupal\media;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Entity\Display\EntityFormDisplayInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\FieldTypePluginManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Plugin\PluginBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Base implementation of media source plugin.
*/
abstract class MediaSourceBase extends PluginBase implements MediaSourceInterface, ContainerFactoryPluginInterface {
/**
* Plugin label.
*
* @var string
*/
protected $label;
/**
* The entity type manager service.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The entity field manager service.
*
* @var \Drupal\Core\Entity\EntityFieldManagerInterface
*/
protected $entityFieldManager;
/**
* The field type plugin manager service.
*
* @var \Drupal\Core\Field\FieldTypePluginManagerInterface
*/
protected $fieldTypeManager;
/**
* The config factory service.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* Constructs a new class instance.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* Entity type manager service.
* @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
* Entity field manager service.
* @param \Drupal\Core\Field\FieldTypePluginManagerInterface $field_type_manager
* The field type plugin manager service.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory service.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, FieldTypePluginManagerInterface $field_type_manager, ConfigFactoryInterface $config_factory) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entity_type_manager;
$this->entityFieldManager = $entity_field_manager;
$this->fieldTypeManager = $field_type_manager;
$this->configFactory = $config_factory;
// Add the default configuration of the media source to the plugin.
$this->setConfiguration($configuration);
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('entity_type.manager'),
$container->get('entity_field.manager'),
$container->get('plugin.manager.field.field_type'),
$container->get('config.factory')
);
}
/**
* {@inheritdoc}
*/
public function setConfiguration(array $configuration) {
$this->configuration = NestedArray::mergeDeep(
$this->defaultConfiguration(),
$configuration
);
}
/**
* {@inheritdoc}
*/
public function getConfiguration() {
return $this->configuration;
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'source_field' => '',
];
}
/**
* {@inheritdoc}
*/
public function getMetadata(MediaInterface $media, $attribute_name) {
switch ($attribute_name) {
case 'default_name':
return 'media:' . $media->bundle() . ':' . $media->uuid();
case 'thumbnail_uri':
$default_thumbnail_filename = $this->pluginDefinition['default_thumbnail_filename'];
return $this->configFactory->get('media.settings')->get('icon_base_uri') . '/' . $default_thumbnail_filename;
}
return NULL;
}
/**
* {@inheritdoc}
*/
public function calculateDependencies() {
return [];
}
/**
* Get the source field options for the media type form.
*
* This returns all fields related to media entities, filtered by the allowed
* field types in the media source annotation.
*
* @return string[]
* A list of source field options for the media type form.
*/
protected function getSourceFieldOptions() {
// If there are existing fields to choose from, allow the user to reuse one.
$options = [];
foreach ($this->entityFieldManager->getFieldStorageDefinitions('media') as $field_name => $field) {
$allowed_type = in_array($field->getType(), $this->pluginDefinition['allowed_field_types'], TRUE);
if ($allowed_type && !$field->isBaseField()) {
$options[$field_name] = $field->getLabel();
}
}
return $options;
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$options = $this->getSourceFieldOptions();
$form['source_field'] = [
'#type' => 'select',
'#title' => $this->t('Field with source information'),
'#default_value' => $this->configuration['source_field'],
'#empty_option' => $this->t('- Create -'),
'#options' => $options,
'#description' => $this->t('Select the field that will store essential information about the media item. If "Create" is selected a new field will be automatically created.'),
];
if (!$options && $form_state->get('operation') === 'add') {
$form['source_field']['#access'] = FALSE;
$field_definition = $this->fieldTypeManager->getDefinition(reset($this->pluginDefinition['allowed_field_types']));
$form['source_field_message'] = [
'#markup' => $this->t('%field_type field will be automatically created on this type to store the essential information about the media item.', [
'%field_type' => $field_definition['label'],
]),
];
}
elseif ($form_state->get('operation') === 'edit') {
$form['source_field']['#access'] = FALSE;
$fields = $this->entityFieldManager->getFieldDefinitions('media', $form_state->get('type')->id());
$form['source_field_message'] = [
'#markup' => $this->t('%field_name field is used to store the essential information about the media item.', [
'%field_name' => $fields[$this->configuration['source_field']]->getLabel(),
]),
];
}
return $form;
}
/**
* {@inheritdoc}
*/
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
foreach (array_intersect_key($form_state->getValues(), $this->configuration) as $config_key => $config_value) {
$this->configuration[$config_key] = $config_value;
}
// If no source field is explicitly set, create it now.
if (empty($this->configuration['source_field'])) {
$field_storage = $this->createSourceFieldStorage();
$field_storage->save();
$this->configuration['source_field'] = $field_storage->getName();
}
}
/**
* Creates the source field storage definition.
*
* By default, the first field type listed in the plugin definition's
* allowed_field_types array will be the generated field's type.
*
* @return \Drupal\field\FieldStorageConfigInterface
* The unsaved field storage definition.
*/
protected function createSourceFieldStorage() {
return $this->entityTypeManager
->getStorage('field_storage_config')
->create([
'entity_type' => 'media',
'field_name' => $this->getSourceFieldName(),
'type' => reset($this->pluginDefinition['allowed_field_types']),
]);
}
/**
* Returns the source field storage definition.
*
* @return \Drupal\Core\Field\FieldStorageDefinitionInterface|null
* The field storage definition or NULL if it doesn't exists.
*/
protected function getSourceFieldStorage() {
// Nothing to do if no source field is configured yet.
$field = $this->configuration['source_field'];
if ($field) {
// Even if we do know the name of the source field, there's no
// guarantee that it exists.
$fields = $this->entityFieldManager->getFieldStorageDefinitions('media');
return $fields[$field] ?? NULL;
}
return NULL;
}
/**
* {@inheritdoc}
*/
public function getSourceFieldDefinition(MediaTypeInterface $type) {
// Nothing to do if no source field is configured yet.
$field = $this->configuration['source_field'];
if ($field) {
// Even if we do know the name of the source field, there is no
// guarantee that it already exists.
$fields = $this->entityFieldManager->getFieldDefinitions('media', $type->id());
return $fields[$field] ?? NULL;
}
return NULL;
}
/**
* {@inheritdoc}
*/
public function createSourceField(MediaTypeInterface $type) {
$storage = $this->getSourceFieldStorage() ?: $this->createSourceFieldStorage();
return $this->entityTypeManager
->getStorage('field_config')
->create([
'field_storage' => $storage,
'bundle' => $type->id(),
'label' => $this->pluginDefinition['label'],
'required' => TRUE,
]);
}
/**
* Determine the name of the source field.
*
* @return string
* The source field name. If one is already stored in configuration, it is
* returned. Otherwise, a new, unused one is generated.
*/
protected function getSourceFieldName() {
// If the Field UI module is installed, and has a specific prefix
// configured, use that. Otherwise, just default to using 'field_' as
// a prefix, which is the default that Field UI ships with.
$prefix = $this->configFactory->get('field_ui.settings')
->get('field_prefix') ?? 'field_';
// Some media sources are using a deriver, so their plugin IDs may contain
// a separator (usually ':') which is not allowed in field names.
$base_id = $prefix . 'media_' . str_replace(static::DERIVATIVE_SEPARATOR, '_', $this->getPluginId());
$tries = 0;
$storage = $this->entityTypeManager->getStorage('field_storage_config');
// Iterate at least once, until no field with the generated ID is found.
do {
$id = $base_id;
// If we've tried before, increment and append the suffix.
if ($tries) {
$id .= '_' . $tries;
}
$field = $storage->load('media.' . $id);
$tries++;
} while ($field);
return $id;
}
/**
* {@inheritdoc}
*/
public function getSourceFieldValue(MediaInterface $media) {
$source_field = $this->configuration['source_field'];
if (empty($source_field)) {
throw new \RuntimeException('Source field for media source is not defined.');
}
$items = $media->get($source_field);
if ($items->isEmpty()) {
return NULL;
}
$field_item = $items->first();
return $field_item->{$field_item->mainPropertyName()};
}
/**
* {@inheritdoc}
*/
public function prepareViewDisplay(MediaTypeInterface $type, EntityViewDisplayInterface $display) {
$display->setComponent($this->getSourceFieldDefinition($type)->getName(), [
'label' => 'visually_hidden',
]);
}
/**
* {@inheritdoc}
*/
public function prepareFormDisplay(MediaTypeInterface $type, EntityFormDisplayInterface $display) {
// Make sure the source field is placed just after the "name" basefield.
$name_component = $display->getComponent('name');
$source_field_weight = ($name_component && isset($name_component['weight'])) ? $name_component['weight'] + 5 : -50;
$display->setComponent($this->getSourceFieldDefinition($type)->getName(), [
'weight' => $source_field_weight,
]);
}
}

View File

@@ -0,0 +1,30 @@
<?php
namespace Drupal\media;
/**
* Defines an interface for a media source with entity constraints.
*
* This allows a media source to optionally add entity validation constraints
* for media items. To add constraints at the source field level, a media source
* can also implement MediaSourceFieldConstraintsInterface.
*
* @see \Drupal\media\MediaSourceInterface
* @see \Drupal\media\MediaSourceFieldConstraintsInterface.php
* @see \Drupal\media\MediaSourceBase
* @see \Drupal\media\Entity\Media
*/
interface MediaSourceEntityConstraintsInterface extends MediaSourceInterface {
/**
* Gets media source-specific validation constraints for a media item.
*
* @return \Symfony\Component\Validator\Constraint[]
* An array of validation constraint definitions, keyed by plugin IDs. The
* corresponding values are options for each validation plugin.
* Each constraint definition can be used for instantiating
* \Symfony\Component\Validator\Constraint objects.
*/
public function getEntityConstraints();
}

View File

@@ -0,0 +1,30 @@
<?php
namespace Drupal\media;
/**
* Defines an interface for a media source with source field constraints.
*
* This allows a media source to optionally add source field validation
* constraints for media items. To add constraints at the entity level, a
* media source can also implement MediaSourceEntityConstraintsInterface.
*
* @see \Drupal\media\MediaSourceInterface
* @see \Drupal\media\MediaSourceEntityConstraintsInterface
* @see \Drupal\media\MediaSourceBase
* @see \Drupal\media\Entity\Media
*/
interface MediaSourceFieldConstraintsInterface extends MediaSourceInterface {
/**
* Gets media source-specific validation constraints for a source field.
*
* @return \Symfony\Component\Validator\Constraint[]
* An array of validation constraint definitions, keyed by plugin IDs. The
* corresponding values are options for each validation plugin.
* Each constraint definition can be used for instantiating
* \Symfony\Component\Validator\Constraint objects.
*/
public function getSourceFieldConstraints();
}

View File

@@ -0,0 +1,195 @@
<?php
namespace Drupal\media;
use Drupal\Component\Plugin\ConfigurableInterface;
use Drupal\Component\Plugin\DependentPluginInterface;
use Drupal\Component\Plugin\PluginInspectionInterface;
use Drupal\Core\Entity\Display\EntityFormDisplayInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Plugin\PluginFormInterface;
/**
* Defines the interface for media source plugins.
*
* Media sources provide the critical link between media items in Drupal and the
* actual media itself, which typically exists independently of Drupal. Each
* media source works with a certain kind of media. For example, local files and
* YouTube videos can both be catalogued in a similar way as media items, but
* they need very different handling to actually display them.
*
* Each media type needs exactly one source. A single source can be used on many
* media types.
*
* Examples of possible sources are:
* - File: handles local files,
* - Image: handles local images,
* - oEmbed: handles resources that are exposed through the oEmbed standard,
* - YouTube: handles YouTube videos,
* - SoundCloud: handles SoundCloud audio,
* - Instagram: handles Instagram posts,
* - Twitter: handles tweets,
* - ...
*
* Their responsibilities are:
* - Defining how media is represented (stored). Media sources are not
* responsible for actually storing the media. They only define how it is
* represented on a media item (usually using some kind of a field).
* - Providing thumbnails. Media sources that are responsible for remote
* media will generally fetch the image from a third-party API and make
* it available for the local usage. Media sources that represent local
* media (such as images) will usually use some locally provided image.
* Media sources should fall back to a pre-defined default thumbnail if
* everything else fails.
* - Validating a media item before it is saved. The entity constraint system
* will be used to ensure the valid structure of the media item.
* For example, media sources that represent remote media might check the
* URL or other identifier, while sources that represent local files might
* check the MIME type of the file.
* - Providing a default name for a media item. This will save users from
* manually entering the name when it can be reliably set automatically.
* Media sources for local files will generally use the filename, while media
* sources for remote resources might obtain a title attribute through a
* third-party API. The name can always be overridden by the user.
* - Providing metadata specific to the given media type. For example, remote
* media sources generally get information available through a
* third-party API and make it available to Drupal, while local media sources
* can expose things such as EXIF or ID3.
* - Mapping metadata to the media item. Metadata that a media source exposes
* can automatically be mapped to the fields on the media item. Media
* sources will be able to define how this is done.
*
* @see \Drupal\media\Annotation\MediaSource
* @see \Drupal\media\MediaSourceBase
* @see \Drupal\media\MediaSourceManager
* @see \Drupal\media\MediaTypeInterface
* @see \Drupal\media\MediaSourceEntityConstraintsInterface
* @see \Drupal\media\MediaSourceFieldConstraintsInterface
* @see plugin_api
*/
interface MediaSourceInterface extends PluginInspectionInterface, ConfigurableInterface, DependentPluginInterface, PluginFormInterface {
/**
* Default empty value for metadata fields.
*/
const METADATA_FIELD_EMPTY = '_none';
/**
* Gets a list of metadata attributes provided by this plugin.
*
* Most media sources have associated metadata, describing attributes
* such as:
* - dimensions
* - duration
* - encoding
* - date
* - location
* - permalink
* - licensing information
* - ...
*
* This method should list all metadata attributes that a media source MAY
* offer. In other words: it is possible that a particular media item does
* not contain a certain attribute. For example: an oEmbed media source can
* contain both video and images. Images don't have a duration, but
* videos do.
*
* (The term 'attributes' was chosen because it cannot be confused
* with 'fields' and 'properties', both of which are concepts in Drupal's
* Entity Field API.)
*
* @return array
* Associative array with:
* - keys: metadata attribute names
* - values: human-readable labels for those attribute names
*/
public function getMetadataAttributes();
/**
* Gets the value for a metadata attribute for a given media item.
*
* @param \Drupal\media\MediaInterface $media
* A media item.
* @param string $attribute_name
* Name of the attribute to fetch.
*
* @return mixed|null
* Metadata attribute value or NULL if unavailable.
*/
public function getMetadata(MediaInterface $media, $attribute_name);
/**
* Get the source field definition for a media type.
*
* @param \Drupal\media\MediaTypeInterface $type
* A media type.
*
* @return \Drupal\Core\Field\FieldDefinitionInterface|null
* The source field definition, or NULL if it doesn't exist or has not been
* configured yet.
*/
public function getSourceFieldDefinition(MediaTypeInterface $type);
/**
* Creates the source field definition for a type.
*
* @param \Drupal\media\MediaTypeInterface $type
* The media type.
*
* @return \Drupal\field\FieldConfigInterface
* The unsaved field definition. The field storage definition, if new,
* should also be unsaved.
*/
public function createSourceField(MediaTypeInterface $type);
/**
* Prepares the media type fields for this source in the view display.
*
* This method should normally call
* \Drupal\Core\Entity\Display\EntityDisplayInterface::setComponent() or
* \Drupal\Core\Entity\Display\EntityDisplayInterface::removeComponent() to
* configure the media type fields in the view display.
*
* @param \Drupal\media\MediaTypeInterface $type
* The media type which is using this source.
* @param \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display
* The display which should be prepared.
*
* @see \Drupal\Core\Entity\Display\EntityDisplayInterface::setComponent()
* @see \Drupal\Core\Entity\Display\EntityDisplayInterface::removeComponent()
*/
public function prepareViewDisplay(MediaTypeInterface $type, EntityViewDisplayInterface $display);
/**
* Prepares the media type fields for this source in the form display.
*
* This method should normally call
* \Drupal\Core\Entity\Display\EntityDisplayInterface::setComponent() or
* \Drupal\Core\Entity\Display\EntityDisplayInterface::removeComponent() to
* configure the media type fields in the form display.
*
* @param \Drupal\media\MediaTypeInterface $type
* The media type which is using this source.
* @param \Drupal\Core\Entity\Display\EntityFormDisplayInterface $display
* The display which should be prepared.
*
* @see \Drupal\Core\Entity\Display\EntityDisplayInterface::setComponent()
* @see \Drupal\Core\Entity\Display\EntityDisplayInterface::removeComponent()
*/
public function prepareFormDisplay(MediaTypeInterface $type, EntityFormDisplayInterface $display);
/**
* Get the primary value stored in the source field.
*
* @param MediaInterface $media
* A media item.
*
* @return mixed
* The source value, or NULL if the media item's source field is empty.
*
* @throws \RuntimeException
* If the source field for the media source is not defined.
*/
public function getSourceFieldValue(MediaInterface $media);
}

View File

@@ -0,0 +1,33 @@
<?php
namespace Drupal\media;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\media\Attribute\MediaSource;
/**
* Manages media source plugins.
*/
class MediaSourceManager extends DefaultPluginManager {
/**
* Constructs a new MediaSourceManager.
*
* @param \Traversable $namespaces
* An object that implements \Traversable which contains the root paths
* keyed by the corresponding namespace to look for plugin implementations.
* @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
* Cache backend instance to use.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.
*/
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
parent::__construct('Plugin/media/Source', $namespaces, $module_handler, MediaSourceInterface::class, MediaSource::class, '\Drupal\media\Annotation\MediaSource');
$this->alterInfo('media_source_info');
$this->setCacheBackend($cache_backend, 'media_source_plugins');
}
}

View File

@@ -0,0 +1,32 @@
<?php
namespace Drupal\media;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
/**
* Defines the storage handler class for media.
*
* The default storage is overridden to handle metadata fetching outside of the
* database transaction.
*/
class MediaStorage extends SqlContentEntityStorage {
/**
* {@inheritdoc}
*/
public function save(EntityInterface $media) {
// For backwards compatibility, modules that override the Media entity
// class, are not required to implement the prepareSave() method.
// @todo For Drupal 8.7, consider throwing a deprecation notice if the
// method doesn't exist. See
// https://www.drupal.org/project/drupal/issues/2992426 for further
// discussion.
if (method_exists($media, 'prepareSave')) {
$media->prepareSave();
}
return parent::save($media);
}
}

View File

@@ -0,0 +1,34 @@
<?php
namespace Drupal\media;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityAccessControlHandler;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AccountInterface;
/**
* Defines the access control handler for the "Media Type" entity type.
*
* @see \Drupal\media\Entity\MediaType
*/
class MediaTypeAccessControlHandler extends EntityAccessControlHandler {
/**
* {@inheritdoc}
*/
protected $viewLabelOperation = TRUE;
/**
* {@inheritdoc}
*/
protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
if ($operation === 'view label') {
return AccessResult::allowedIfHasPermission($account, 'view media');
}
else {
return parent::checkAccess($entity, $operation, $account);
}
}
}

View File

@@ -0,0 +1,405 @@
<?php
namespace Drupal\media;
use Drupal\Component\Plugin\PluginManagerInterface;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\ReplaceCommand;
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\SubformState;
use Drupal\language\Entity\ContentLanguageSettings;
use Drupal\media\Entity\MediaType;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Form controller for media type forms.
*
* @internal
*/
class MediaTypeForm extends EntityForm {
/**
* Media source plugin manager.
*
* @var \Drupal\Component\Plugin\PluginManagerInterface
*/
protected $sourceManager;
/**
* Entity field manager service.
*
* @var \Drupal\Core\Entity\EntityFieldManagerInterface
*/
protected $entityFieldManager;
/**
* Entity display repository service.
*
* @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface
*/
protected $entityDisplayRepository;
/**
* Constructs a new class instance.
*
* @param \Drupal\Component\Plugin\PluginManagerInterface $source_manager
* Media source plugin manager.
* @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
* Entity field manager service.
* @param \Drupal\Core\Entity\EntityDisplayRepositoryInterface $entityDisplayRepository
* Entity display repository service.
*/
public function __construct(PluginManagerInterface $source_manager, EntityFieldManagerInterface $entity_field_manager, EntityDisplayRepositoryInterface $entityDisplayRepository) {
$this->sourceManager = $source_manager;
$this->entityFieldManager = $entity_field_manager;
$this->entityDisplayRepository = $entityDisplayRepository;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('plugin.manager.media.source'),
$container->get('entity_field.manager'),
$container->get('entity_display.repository')
);
}
/**
* Ajax callback triggered by the type provider select element.
*/
public function ajaxHandlerData(array $form, FormStateInterface $form_state) {
$response = new AjaxResponse();
$response->addCommand(new ReplaceCommand('#source-dependent', $form['source_dependent']));
return $response;
}
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
// Source is not set when the entity is initially created.
/** @var \Drupal\media\MediaSourceInterface $source */
$source = $this->entity->get('source') ? $this->entity->getSource() : NULL;
if ($this->operation === 'add') {
$form['#title'] = $this->t('Add media type');
}
$form['label'] = [
'#title' => $this->t('Name'),
'#type' => 'textfield',
'#default_value' => $this->entity->label(),
'#description' => $this->t('The human-readable name for this media type, displayed on the <em>Media types</em> page.'),
'#required' => TRUE,
'#size' => 30,
];
$form['id'] = [
'#type' => 'machine_name',
'#default_value' => $this->entity->id(),
'#maxlength' => 32,
'#disabled' => !$this->entity->isNew(),
'#machine_name' => [
'exists' => [MediaType::class, 'load'],
],
'#description' => $this->t('Unique machine-readable name: lowercase letters, numbers, and underscores only.'),
];
$form['description'] = [
'#title' => $this->t('Description'),
'#type' => 'textarea',
'#default_value' => $this->entity->getDescription(),
'#description' => $this->t('Displays on the <em>Media types</em> page.'),
];
$plugins = $this->sourceManager->getDefinitions();
$options = [];
foreach ($plugins as $plugin_id => $definition) {
$options[$plugin_id] = $definition['label'];
}
$form['source_dependent'] = [
'#type' => 'container',
'#attributes' => ['id' => 'source-dependent'],
];
if (!$this->entity->isNew()) {
$source_description = $this->t('<em>The media source cannot be changed after the media type is created.</em>');
}
else {
$source_description = $this->t('Media source that is responsible for additional logic related to this media type.');
}
$form['source_dependent']['source'] = [
'#type' => 'select',
'#title' => $this->t('Media source'),
'#default_value' => $source ? $source->getPluginId() : NULL,
'#options' => $options,
'#description' => $source_description,
'#ajax' => ['callback' => '::ajaxHandlerData'],
'#required' => TRUE,
// Once the media type is created, its source plugin cannot be changed
// anymore.
'#disabled' => !$this->entity->isNew(),
];
if ($source) {
// Media source plugin configuration.
$form['source_dependent']['source_configuration'] = [
'#type' => 'fieldset',
'#title' => $this->t('Media source configuration'),
'#tree' => TRUE,
];
$form['source_dependent']['source_configuration'] = $source->buildConfigurationForm($form['source_dependent']['source_configuration'], $this->getSourceSubFormState($form, $form_state));
}
// Field mapping configuration.
$form['source_dependent']['field_map'] = [
'#type' => 'fieldset',
'#title' => $this->t('Field mapping'),
'#tree' => TRUE,
'description' => [
'#markup' => '<p>' . $this->t('Media sources can provide metadata fields such as title, caption, size information, credits, etc. Media can automatically save this metadata information to entity fields, which can be configured below. Information will only be mapped if the entity field is empty.') . '</p>',
],
];
if (empty($source) || empty($source->getMetadataAttributes())) {
$form['source_dependent']['field_map']['#access'] = FALSE;
}
else {
$options = [MediaSourceInterface::METADATA_FIELD_EMPTY => $this->t('- Skip field -')];
$source_field_name = $source->getSourceFieldDefinition($this->entity)?->getName();
foreach ($this->entityFieldManager->getFieldDefinitions('media', $this->entity->id()) as $field_name => $field) {
// The source field cannot be the target of a field mapping, because
// this would cause it to be overwritten, probably with invalid data.
if ($field_name === $source_field_name) {
continue;
}
if (!($field instanceof BaseFieldDefinition) || $field_name === 'name') {
$options[$field_name] = $field->getLabel();
}
}
natcasesort($options);
$field_map = $this->entity->getFieldMap();
foreach ($source->getMetadataAttributes() as $metadata_attribute_name => $metadata_attribute_label) {
$form['source_dependent']['field_map'][$metadata_attribute_name] = [
'#type' => 'select',
'#title' => $metadata_attribute_label,
'#options' => $options,
'#default_value' => $field_map[$metadata_attribute_name] ?? MediaSourceInterface::METADATA_FIELD_EMPTY,
];
}
}
$form['additional_settings'] = [
'#type' => 'vertical_tabs',
'#attached' => [
'library' => ['media/type_form'],
],
];
$form['workflow'] = [
'#type' => 'details',
'#title' => $this->t('Publishing options'),
'#group' => 'additional_settings',
];
$form['workflow']['options'] = [
'#type' => 'checkboxes',
'#title' => $this->t('Default options'),
'#default_value' => $this->getWorkflowOptions(),
'#options' => [
'status' => $this->t('Published'),
'new_revision' => $this->t('Create new revision'),
'queue_thumbnail_downloads' => $this->t('Queue thumbnail downloads'),
],
];
$form['workflow']['options']['status']['#description'] = $this->t('Media will be automatically published when created.');
$form['workflow']['options']['new_revision']['#description'] = $this->t('Automatically create new revisions. Users with the "Administer media" permission will be able to override this option.');
$form['workflow']['options']['queue_thumbnail_downloads']['#description'] = $this->t('Download thumbnails via a queue. When using remote media sources, the thumbnail generation could be a slow process. Using a queue allows for this process to be handled in the background.');
if ($this->moduleHandler->moduleExists('language')) {
$form['language'] = [
'#type' => 'details',
'#title' => $this->t('Language settings'),
'#group' => 'additional_settings',
];
$language_configuration = ContentLanguageSettings::loadByEntityTypeBundle('media', $this->entity->id());
$form['language']['language_configuration'] = [
'#type' => 'language_configuration',
'#entity_information' => [
'entity_type' => 'media',
'bundle' => $this->entity->id(),
],
'#default_value' => $language_configuration,
];
}
return $form;
}
/**
* Prepares workflow options to be used in the 'checkboxes' form element.
*
* @return array
* Array of options ready to be used in #options.
*/
protected function getWorkflowOptions() {
$workflow_options = [
'status' => $this->entity->getStatus(),
'new_revision' => $this->entity->shouldCreateNewRevision(),
'queue_thumbnail_downloads' => $this->entity->thumbnailDownloadsAreQueued(),
];
// Prepare workflow options to be used for 'checkboxes' form element.
$keys = array_keys(array_filter($workflow_options));
return array_combine($keys, $keys);
}
/**
* Gets subform state for the media source configuration subform.
*
* @param array $form
* Full form array.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* Parent form state.
*
* @return \Drupal\Core\Form\SubformStateInterface
* Sub-form state for the media source configuration form.
*/
protected function getSourceSubFormState(array $form, FormStateInterface $form_state) {
return SubformState::createForSubform($form['source_dependent']['source_configuration'], $form, $form_state)
->set('operation', $this->operation)
->set('type', $this->entity);
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
if (isset($form['source_dependent']['source_configuration'])) {
// Let the selected plugin validate its settings.
$this->entity->getSource()->validateConfigurationForm($form['source_dependent']['source_configuration'], $this->getSourceSubFormState($form, $form_state));
}
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$form_state->setValue('field_map', array_filter(
$form_state->getValue('field_map', []),
function ($item) {
return $item != MediaSourceInterface::METADATA_FIELD_EMPTY;
}
));
parent::submitForm($form, $form_state);
$this->entity->setQueueThumbnailDownloadsStatus((bool) $form_state->getValue(['options', 'queue_thumbnail_downloads']))
->setStatus((bool) $form_state->getValue(['options', 'status']))
->setNewRevision((bool) $form_state->getValue(['options', 'new_revision']));
if (isset($form['source_dependent']['source_configuration'])) {
// Let the selected plugin save its settings.
$this->entity->getSource()->submitConfigurationForm($form['source_dependent']['source_configuration'], $this->getSourceSubFormState($form, $form_state));
}
}
/**
* {@inheritdoc}
*/
protected function actions(array $form, FormStateInterface $form_state) {
$actions = parent::actions($form, $form_state);
// If the media source has not been chosen yet, turn the submit button into
// a button. This rebuilds the form with the media source's configuration
// form visible, instead of saving the media type. This allows users to
// create a media type without JavaScript enabled. With JavaScript enabled,
// this rebuild occurs during an AJAX request.
// @see \Drupal\media\MediaTypeForm::ajaxHandlerData()
if (empty($this->getEntity()->get('source'))) {
$actions['submit']['#type'] = 'button';
}
$actions['submit']['#value'] = $this->t('Save');
$actions['delete']['#value'] = $this->t('Delete');
$actions['delete']['#access'] = $this->entity->access('delete');
return $actions;
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
$status = parent::save($form, $form_state);
/** @var \Drupal\media\MediaTypeInterface $media_type */
$media_type = $this->entity;
// If the media source is using a source field, ensure it's
// properly created.
$source = $media_type->getSource();
$source_field = $source->getSourceFieldDefinition($media_type);
if (!$source_field) {
$source_field = $source->createSourceField($media_type);
/** @var \Drupal\field\FieldStorageConfigInterface $storage */
$storage = $source_field->getFieldStorageDefinition();
if ($storage->isNew()) {
$storage->save();
}
$source_field->save();
// Add the new field to the default form and view displays for this
// media type.
if ($source_field->isDisplayConfigurable('form')) {
$display = $this->entityDisplayRepository->getFormDisplay('media', $media_type->id());
$source->prepareFormDisplay($media_type, $display);
$display->save();
}
if ($source_field->isDisplayConfigurable('view')) {
$display = $this->entityDisplayRepository->getViewDisplay('media', $media_type->id());
// Remove all default components.
foreach (array_keys($display->getComponents()) as $name) {
$display->removeComponent($name);
}
$source->prepareViewDisplay($media_type, $display);
$display->save();
}
}
$t_args = ['%name' => $media_type->label()];
if ($status === SAVED_UPDATED) {
$this->messenger()->addStatus($this->t('The media type %name has been updated.', $t_args));
}
elseif ($status === SAVED_NEW) {
$this->messenger()->addStatus($this->t('The media type %name has been added.', $t_args));
$this->logger('media')->notice('Added media type %name.', $t_args);
}
// Override the "status" base field default value, for this media type.
$fields = $this->entityFieldManager->getFieldDefinitions('media', $media_type->id());
/** @var \Drupal\media\MediaInterface $media */
$media = $this->entityTypeManager->getStorage('media')->create(['bundle' => $media_type->id()]);
$value = (bool) $form_state->getValue(['options', 'status']);
if ($media->status->value != $value) {
$fields['status']->getConfig($media_type->id())->setDefaultValue($value)->save();
}
$form_state->setRedirectUrl($media_type->toUrl('collection'));
}
}

View File

@@ -0,0 +1,101 @@
<?php
namespace Drupal\media;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Entity\EntityDescriptionInterface;
use Drupal\Core\Entity\RevisionableEntityBundleInterface;
/**
* Provides an interface defining a media type entity.
*
* Media types are bundles for media items. They are used to group media with
* the same semantics. Media types are not about where media comes from. They
* are about the semantics that media has in the context of a given Drupal site.
*
* Media sources, on the other hand, are aware where media comes from and know
* how to represent and handle it in Drupal's context. They are aware of the low
* level details, while the media types don't care about them at all. That said,
* media types can not exist without media sources.
*
* Consider the following examples:
* - oEmbed media source which can represent any oEmbed resource. Media types
* that could be used with this source are "Videos", "Charts", "Music", etc.
* All of them are retrieved using the same protocol, but they represent very
* different things.
* - Media sources that represent files could be used with media types like
* "Invoices", "Subtitles", "Meeting notes", etc. They are all files stored on
* some kind of storage, but their meaning and uses in a Drupal site are
* different.
*
* @see \Drupal\media\MediaSourceInterface
*/
interface MediaTypeInterface extends ConfigEntityInterface, EntityDescriptionInterface, RevisionableEntityBundleInterface {
/**
* Returns whether thumbnail downloads are queued.
*
* When using remote media sources, the thumbnail generation could be a slow
* process. Using a queue allows for this process to be handled in the
* background.
*
* @return bool
* TRUE if thumbnails are queued for download later, FALSE if they should be
* downloaded now.
*/
public function thumbnailDownloadsAreQueued();
/**
* Sets a flag to indicate that thumbnails should be downloaded via a queue.
*
* @param bool $queue_thumbnail_downloads
* The queue downloads flag.
*
* @return $this
*/
public function setQueueThumbnailDownloadsStatus($queue_thumbnail_downloads);
/**
* Returns the media source plugin.
*
* @return \Drupal\media\MediaSourceInterface
* The media source.
*/
public function getSource();
/**
* Sets whether new revisions should be created by default.
*
* @param bool $new_revision
* TRUE if media items of this type should create new revisions by default.
*
* @return $this
*/
public function setNewRevision($new_revision);
/**
* Returns the metadata field map.
*
* Field mapping allows site builders to map media item-related metadata to
* entity fields. This information will be used when saving a given media item
* and if metadata values will be available they are going to be automatically
* copied to the corresponding entity fields.
*
* @return array
* Field mapping array provided by media source with metadata attribute
* names as keys and entity field names as values.
*/
public function getFieldMap();
/**
* Sets the metadata field map.
*
* @param array $map
* Field mapping array with metadata attribute names as keys and entity
* field names as values.
*
* @return $this
*/
public function setFieldMap(array $map);
}

View File

@@ -0,0 +1,49 @@
<?php
namespace Drupal\media;
use Drupal\Core\Config\Entity\ConfigEntityListBuilder;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Url;
/**
* Provides a listing of media types.
*/
class MediaTypeListBuilder extends ConfigEntityListBuilder {
/**
* {@inheritdoc}
*/
public function buildHeader() {
$header['title'] = $this->t('Name');
$header['description'] = [
'data' => $this->t('Description'),
'class' => [RESPONSIVE_PRIORITY_MEDIUM],
];
return $header + parent::buildHeader();
}
/**
* {@inheritdoc}
*/
public function buildRow(EntityInterface $entity) {
$row['title'] = [
'data' => $entity->label(),
'class' => ['menu-label'],
];
$row['description']['data'] = ['#markup' => $entity->getDescription()];
return $row + parent::buildRow($entity);
}
/**
* {@inheritdoc}
*/
public function render() {
$build = parent::render();
$build['table']['#empty'] = $this->t('No media types available. <a href=":url">Add media type</a>.', [
':url' => Url::fromRoute('entity.media_type.add_form')->toString(),
]);
return $build;
}
}

View File

@@ -0,0 +1,34 @@
<?php
namespace Drupal\media;
use Drupal\views\EntityViewsData;
/**
* Provides the Views data for the media entity type.
*/
class MediaViewsData extends EntityViewsData {
/**
* {@inheritdoc}
*/
public function getViewsData() {
$data = parent::getViewsData();
$data['media_field_data']['table']['wizard_id'] = 'media';
$data['media_field_revision']['table']['wizard_id'] = 'media_revision';
$data['media_field_data']['status_extra'] = [
'title' => $this->t('Published status or admin user'),
'help' => $this->t('Filters out unpublished media if the current user cannot view it.'),
'filter' => [
'field' => 'status',
'id' => 'media_status',
'label' => $this->t('Published status or admin user'),
],
];
return $data;
}
}

View File

@@ -0,0 +1,182 @@
<?php
namespace Drupal\media\OEmbed;
use Drupal\Component\Utility\UrlHelper;
/**
* Value object for oEmbed provider endpoints.
*
* @internal
* This class is an internal part of the oEmbed system and should only be
* instantiated by instances of Drupal\media\OEmbed\Provider.
*/
class Endpoint {
/**
* The endpoint's URL.
*
* @var string
*/
protected $url;
/**
* The provider this endpoint belongs to.
*
* @var \Drupal\media\OEmbed\Provider
*/
protected $provider;
/**
* List of URL schemes supported by the provider.
*
* @var string[]
*/
protected $schemes;
/**
* List of supported formats. Only 'json' and 'xml' are allowed.
*
* @var string[]
*
* @see https://oembed.com/#section2
*/
protected $formats;
/**
* Whether the provider supports oEmbed discovery.
*
* @var bool
*/
protected $supportsDiscovery;
/**
* Endpoint constructor.
*
* @param string $url
* The endpoint URL. May contain a @code '{format}' @endcode placeholder.
* @param \Drupal\media\OEmbed\Provider $provider
* The provider this endpoint belongs to.
* @param string[] $schemes
* List of URL schemes supported by the provider.
* @param string[] $formats
* List of supported formats. Can be "json", "xml" or both.
* @param bool $supports_discovery
* Whether the provider supports oEmbed discovery.
*
* @throws \InvalidArgumentException
* If the endpoint URL is empty.
*/
public function __construct($url, Provider $provider, array $schemes = [], array $formats = [], $supports_discovery = FALSE) {
$this->provider = $provider;
$this->schemes = $schemes;
$this->formats = $formats = array_map('mb_strtolower', $formats);
// Assert that only the supported formats are present.
assert(array_diff($formats, ['json', 'xml']) == []);
// Use the first provided format to build the endpoint URL. If no formats
// are provided, default to JSON.
$this->url = str_replace('{format}', reset($this->formats) ?: 'json', $url);
if (!UrlHelper::isValid($this->url, TRUE) || !UrlHelper::isExternal($this->url)) {
throw new \InvalidArgumentException('oEmbed endpoint must have a valid external URL');
}
$this->supportsDiscovery = (bool) $supports_discovery;
}
/**
* Returns the endpoint URL.
*
* The URL will be built with the first available format. If the endpoint
* does not provide any formats, JSON will be used.
*
* @return string
* The endpoint URL.
*/
public function getUrl() {
return $this->url;
}
/**
* Returns the provider this endpoint belongs to.
*
* @return \Drupal\media\OEmbed\Provider
* The provider object.
*/
public function getProvider() {
return $this->provider;
}
/**
* Returns list of URL schemes supported by the provider.
*
* @return string[]
* List of schemes.
*/
public function getSchemes() {
return $this->schemes;
}
/**
* Returns list of supported formats.
*
* @return string[]
* List of formats.
*/
public function getFormats() {
return $this->formats;
}
/**
* Returns whether the provider supports oEmbed discovery.
*
* @return bool
* Returns TRUE if the provides discovery, otherwise FALSE.
*/
public function supportsDiscovery() {
return $this->supportsDiscovery;
}
/**
* Tries to match a URL against the endpoint schemes.
*
* @param string $url
* Media item URL.
*
* @return bool
* TRUE if the URL matches against the endpoint schemes, otherwise FALSE.
*/
public function matchUrl($url) {
foreach ($this->getSchemes() as $scheme) {
// Convert scheme into a valid regular expression.
$regexp = str_replace(['.', '*', '?'], ['\.', '.*', '\?'], $scheme);
if (preg_match("|^$regexp$|", $url)) {
return TRUE;
}
}
return FALSE;
}
/**
* Builds and returns the endpoint URL.
*
* In most situations this function should not be used. Your are probably
* looking for \Drupal\media\OEmbed\UrlResolver::getResourceUrl(), because it
* is alterable and also cached.
*
* @param string $url
* The canonical media URL.
*
* @return string
* URL of the oEmbed endpoint.
*
* @see \Drupal\media\OEmbed\UrlResolver::getResourceUrl()
*/
public function buildResourceUrl($url) {
$query = ['url' => $url];
return $this->getUrl() . '?' . UrlHelper::buildQuery($query);
}
}

View File

@@ -0,0 +1,100 @@
<?php
namespace Drupal\media\OEmbed;
use Drupal\Component\Utility\UrlHelper;
/**
* Value object for oEmbed providers.
*/
class Provider {
/**
* The provider name.
*
* @var string
*/
protected $name;
/**
* The provider URL.
*
* @var string
*/
protected $url;
/**
* The provider endpoints.
*
* @var \Drupal\media\OEmbed\Endpoint[]
*/
protected $endpoints = [];
/**
* Provider constructor.
*
* @param string $name
* The provider name.
* @param string $url
* The provider URL.
* @param array[] $endpoints
* List of endpoints this provider exposes.
*
* @throws \Drupal\media\OEmbed\ProviderException
*/
public function __construct($name, $url, array $endpoints) {
$this->name = $name;
if (!UrlHelper::isValid($url, TRUE) || !UrlHelper::isExternal($url)) {
throw new ProviderException('Provider @name does not define a valid external URL.', $this);
}
$this->url = $url;
try {
foreach ($endpoints as $endpoint) {
$endpoint += ['formats' => [], 'schemes' => [], 'discovery' => FALSE];
$this->endpoints[] = new Endpoint($endpoint['url'], $this, $endpoint['schemes'], $endpoint['formats'], $endpoint['discovery']);
}
}
catch (\InvalidArgumentException $e) {
// Just skip all the invalid endpoints.
// @todo Log the exception message to help with debugging in
// https://www.drupal.org/project/drupal/issues/2972846.
}
if (empty($this->endpoints)) {
throw new ProviderException('Provider @name does not define any valid endpoints.', $this);
}
}
/**
* Returns the provider name.
*
* @return string
* Name of the provider.
*/
public function getName() {
return $this->name;
}
/**
* Returns the provider URL.
*
* @return string
* URL of the provider.
*/
public function getUrl() {
return $this->url;
}
/**
* Returns the provider endpoints.
*
* @return \Drupal\media\OEmbed\Endpoint[]
* List of endpoints this provider exposes.
*/
public function getEndpoints() {
return $this->endpoints;
}
}

View File

@@ -0,0 +1,40 @@
<?php
namespace Drupal\media\OEmbed;
/**
* Exception thrown if an oEmbed provider causes an error.
*
* @internal
* This is an internal part of the oEmbed system and should only be used by
* oEmbed-related code in Drupal core.
*/
class ProviderException extends \Exception {
/**
* Information about the oEmbed provider which caused the exception.
*
* @var \Drupal\media\OEmbed\Provider
*
* @see \Drupal\media\OEmbed\ProviderRepositoryInterface::get()
*/
protected $provider;
/**
* ProviderException constructor.
*
* @param string $message
* The exception message. '@name' will be replaced with the provider name
* if available, or '<unknown>' if not.
* @param \Drupal\media\OEmbed\Provider $provider
* (optional) The provider information.
* @param \Exception $previous
* (optional) The previous exception, if any.
*/
public function __construct($message, ?Provider $provider = NULL, ?\Exception $previous = NULL) {
$this->provider = $provider;
$message = str_replace('@name', $provider ? $provider->getName() : '<unknown>', $message);
parent::__construct($message, 0, $previous);
}
}

View File

@@ -0,0 +1,161 @@
<?php
namespace Drupal\media\OEmbed;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\KeyValueStore\KeyValueFactoryInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use GuzzleHttp\ClientInterface;
use Psr\Http\Client\ClientExceptionInterface;
/**
* Retrieves and caches information about oEmbed providers.
*/
class ProviderRepository implements ProviderRepositoryInterface {
/**
* How long the provider data should be cached, in seconds.
*
* @var int
*/
protected $maxAge;
/**
* The HTTP client.
*
* @var \GuzzleHttp\Client
*/
protected $httpClient;
/**
* URL of a JSON document which contains a database of oEmbed providers.
*
* @var string
*/
protected $providersUrl;
/**
* The time service.
*
* @var \Drupal\Component\Datetime\TimeInterface
*/
protected $time;
/**
* The key-value store.
*
* @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface
*/
protected $keyValue;
/**
* The logger channel.
*
* @var \Drupal\Core\Logger\LoggerChannelInterface
*/
protected $logger;
/**
* Constructs a ProviderRepository instance.
*
* @param \GuzzleHttp\ClientInterface $http_client
* The HTTP client.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory service.
* @param \Drupal\Component\Datetime\TimeInterface $time
* The time service.
* @param \Drupal\Core\KeyValueStore\KeyValueFactoryInterface $key_value_factory
* The key-value store factory.
* @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_factory
* The logger channel factory.
* @param int $max_age
* (optional) How long the cache data should be kept. Defaults to a week.
*/
public function __construct(ClientInterface $http_client, ConfigFactoryInterface $config_factory, TimeInterface $time, KeyValueFactoryInterface $key_value_factory, LoggerChannelFactoryInterface $logger_factory, int $max_age = 604800) {
$this->httpClient = $http_client;
$this->providersUrl = $config_factory->get('media.settings')->get('oembed_providers_url');
$this->time = $time;
$this->maxAge = $max_age;
$this->keyValue = $key_value_factory->get('media');
$this->logger = $logger_factory->get('media');
}
/**
* {@inheritdoc}
*/
public function getAll() {
$current_time = $this->time->getCurrentTime();
$stored = $this->keyValue->get('oembed_providers');
// If we have stored data that hasn't yet expired, return that. We need to
// store the data in a key-value store because, if the remote provider
// database is unavailable, we'd rather return stale data than throw an
// exception. This means we cannot use a normal cache backend or expirable
// key-value store, since those could delete the stale data at any time.
if ($stored && $stored['expires'] > $current_time) {
return $stored['data'];
}
try {
$response = $this->httpClient->request('GET', $this->providersUrl);
}
catch (ClientExceptionInterface $e) {
if (isset($stored['data'])) {
// Use the stale data to fall back gracefully, but warn site
// administrators that we used stale data.
$this->logger->warning('Remote oEmbed providers could not be retrieved due to error: @error. Using previously stored data. This may contain out of date information.', [
'@error' => $e->getMessage(),
]);
return $stored['data'];
}
// We have no previous data and the request failed.
throw new ProviderException("Could not retrieve the oEmbed provider database from $this->providersUrl", NULL, $e);
}
$providers = Json::decode((string) $response->getBody());
if (!is_array($providers) || empty($providers)) {
if (isset($stored['data'])) {
// Use the stale data to fall back gracefully, but as above, warn site
// administrators that we used stale data.
$this->logger->warning('Remote oEmbed providers database returned invalid or empty list. Using previously stored data. This may contain out of date information.');
return $stored['data'];
}
// We have no previous data and the current data is corrupt.
throw new ProviderException('Remote oEmbed providers database returned invalid or empty list.');
}
$keyed_providers = [];
foreach ($providers as $provider) {
try {
$name = (string) $provider['provider_name'];
$keyed_providers[$name] = new Provider($provider['provider_name'], $provider['provider_url'], $provider['endpoints']);
}
catch (ProviderException $e) {
// Skip invalid providers, but log the exception message to help with
// debugging.
$this->logger->warning($e->getMessage());
}
}
$this->keyValue->set('oembed_providers', [
'data' => $keyed_providers,
'expires' => $current_time + $this->maxAge,
]);
return $keyed_providers;
}
/**
* {@inheritdoc}
*/
public function get($provider_name) {
$providers = $this->getAll();
if (!isset($providers[$provider_name])) {
throw new \InvalidArgumentException("Unknown provider '$provider_name'");
}
return $providers[$provider_name];
}
}

View File

@@ -0,0 +1,40 @@
<?php
namespace Drupal\media\OEmbed;
/**
* Defines an interface for a collection of oEmbed provider information.
*
* The provider repository is responsible for fetching information about all
* available oEmbed providers, most likely pulled from the online database at
* https://oembed.com/providers.json, and creating \Drupal\media\OEmbed\Provider
* value objects for each provider.
*/
interface ProviderRepositoryInterface {
/**
* Returns information on all available oEmbed providers.
*
* @return \Drupal\media\OEmbed\Provider[]
* Returns an array of provider value objects, keyed by provider name.
*
* @throws \Drupal\media\OEmbed\ProviderException
* If the oEmbed provider information cannot be retrieved.
*/
public function getAll();
/**
* Returns information for a specific oEmbed provider.
*
* @param string $provider_name
* The name of the provider.
*
* @return \Drupal\media\OEmbed\Provider
* A value object containing information about the provider.
*
* @throws \InvalidArgumentException
* If there is no known oEmbed provider with the specified name.
*/
public function get($provider_name);
}

View File

@@ -0,0 +1,529 @@
<?php
namespace Drupal\media\OEmbed;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\Cache\CacheableDependencyTrait;
use Drupal\Core\Url;
/**
* Value object representing an oEmbed resource.
*
* Data received from an oEmbed provider could be insecure. For example,
* resources of the 'rich' type provide an HTML representation which is not
* sanitized by this object in any way. Any values you retrieve from this object
* should be treated as potentially dangerous user input and carefully validated
* and sanitized before being displayed or otherwise manipulated by your code.
*
* Valid resource types are defined in the oEmbed specification and represented
* by the TYPE_* constants in this class.
*
* @see https://oembed.com/#section2
*
* @internal
* This class is an internal part of the oEmbed system and should only be
* instantiated by
* \Drupal\media\OEmbed\ResourceFetcherInterface::fetchResource().
*/
class Resource implements CacheableDependencyInterface {
use CacheableDependencyTrait;
/**
* The resource type for link resources.
*/
const TYPE_LINK = 'link';
/**
* The resource type for photo resources.
*/
const TYPE_PHOTO = 'photo';
/**
* The resource type for rich resources.
*/
const TYPE_RICH = 'rich';
/**
* The resource type for video resources.
*/
const TYPE_VIDEO = 'video';
/**
* The resource type. Can be one of the static::TYPE_* constants.
*
* @var string
*/
protected $type;
/**
* The resource provider.
*
* @var \Drupal\media\OEmbed\Provider
*/
protected $provider;
/**
* A text title, describing the resource.
*
* @var string
*/
protected $title;
/**
* The name of the author/owner of the resource.
*
* @var string
*/
protected $authorName;
/**
* A URL for the author/owner of the resource.
*
* @var string
*/
protected $authorUrl;
/**
* A URL to a thumbnail image representing the resource.
*
* The thumbnail must respect any maxwidth and maxheight parameters passed
* to the oEmbed endpoint. If this parameter is present, thumbnail_width and
* thumbnail_height must also be present.
*
* @var string
*
* @see \Drupal\media\OEmbed\UrlResolverInterface::getResourceUrl()
* @see https://oembed.com/#section2
*/
protected $thumbnailUrl;
/**
* The width of the thumbnail, in pixels.
*
* If this parameter is present, thumbnail_url and thumbnail_height must also
* be present.
*
* @var int
*/
protected $thumbnailWidth;
/**
* The height of the thumbnail, in pixels.
*
* If this parameter is present, thumbnail_url and thumbnail_width must also
* be present.
*
* @var int
*/
protected $thumbnailHeight;
/**
* The width of the resource, in pixels.
*
* @var int
*/
protected $width;
/**
* The height of the resource, in pixels.
*
* @var int
*/
protected $height;
/**
* The resource URL. Only applies to 'photo' and 'link' resources.
*
* @var string
*/
protected $url;
/**
* The HTML representation of the resource.
*
* Only applies to 'rich' and 'video' resources.
*
* @var string
*/
protected $html;
/**
* Resource constructor.
*
* @param \Drupal\media\OEmbed\Provider $provider
* (optional) The resource provider.
* @param string $title
* (optional) A text title, describing the resource.
* @param string $author_name
* (optional) The name of the author/owner of the resource.
* @param string $author_url
* (optional) A URL for the author/owner of the resource.
* @param int $cache_age
* (optional) The suggested cache lifetime for this resource, in seconds.
* @param string $thumbnail_url
* (optional) A URL to a thumbnail image representing the resource. If this
* parameter is present, $thumbnail_width and $thumbnail_height must also be
* present.
* @param int $thumbnail_width
* (optional) The width of the thumbnail, in pixels. If this parameter is
* present, $thumbnail_url and $thumbnail_height must also be present.
* @param int $thumbnail_height
* (optional) The height of the thumbnail, in pixels. If this parameter is
* present, $thumbnail_url and $thumbnail_width must also be present.
*/
protected function __construct(?Provider $provider = NULL, $title = NULL, $author_name = NULL, $author_url = NULL, $cache_age = NULL, $thumbnail_url = NULL, $thumbnail_width = NULL, $thumbnail_height = NULL) {
$this->provider = $provider;
$this->title = $title;
$this->authorName = $author_name;
$this->authorUrl = $author_url;
if (isset($cache_age) && is_numeric($cache_age)) {
// If the cache age is too big, it can overflow the 'expire' column of
// database cache backends, causing SQL exceptions. To prevent that,
// arbitrarily limit the cache age to 5 years. That should be enough.
$this->cacheMaxAge = Cache::mergeMaxAges((int) $cache_age, 157680000);
}
if ($thumbnail_url) {
$this->thumbnailUrl = $thumbnail_url;
$this->setThumbnailDimensions($thumbnail_width, $thumbnail_height);
}
}
/**
* Creates a link resource.
*
* @param string $url
* (optional) The URL of the resource.
* @param \Drupal\media\OEmbed\Provider $provider
* (optional) The resource provider.
* @param string $title
* (optional) A text title, describing the resource.
* @param string $author_name
* (optional) The name of the author/owner of the resource.
* @param string $author_url
* (optional) A URL for the author/owner of the resource.
* @param int $cache_age
* (optional) The suggested cache lifetime for this resource, in seconds.
* @param string $thumbnail_url
* (optional) A URL to a thumbnail image representing the resource. If this
* parameter is present, $thumbnail_width and $thumbnail_height must also be
* present.
* @param int $thumbnail_width
* (optional) The width of the thumbnail, in pixels. If this parameter is
* present, $thumbnail_url and $thumbnail_height must also be present.
* @param int $thumbnail_height
* (optional) The height of the thumbnail, in pixels. If this parameter is
* present, $thumbnail_url and $thumbnail_width must also be present.
*
* @return static
*/
public static function link($url = NULL, ?Provider $provider = NULL, $title = NULL, $author_name = NULL, $author_url = NULL, $cache_age = NULL, $thumbnail_url = NULL, $thumbnail_width = NULL, $thumbnail_height = NULL) {
$resource = new static($provider, $title, $author_name, $author_url, $cache_age, $thumbnail_url, $thumbnail_width, $thumbnail_height);
$resource->type = self::TYPE_LINK;
$resource->url = $url;
return $resource;
}
/**
* Creates a photo resource.
*
* @param string $url
* The URL of the photo.
* @param int $width
* The width of the photo, in pixels.
* @param int $height
* (optional) The height of the photo, in pixels.
* @param \Drupal\media\OEmbed\Provider $provider
* (optional) The resource provider.
* @param string $title
* (optional) A text title, describing the resource.
* @param string $author_name
* (optional) The name of the author/owner of the resource.
* @param string $author_url
* (optional) A URL for the author/owner of the resource.
* @param int $cache_age
* (optional) The suggested cache lifetime for this resource, in seconds.
* @param string $thumbnail_url
* (optional) A URL to a thumbnail image representing the resource. If this
* parameter is present, $thumbnail_width and $thumbnail_height must also be
* present.
* @param int $thumbnail_width
* (optional) The width of the thumbnail, in pixels. If this parameter is
* present, $thumbnail_url and $thumbnail_height must also be present.
* @param int $thumbnail_height
* (optional) The height of the thumbnail, in pixels. If this parameter is
* present, $thumbnail_url and $thumbnail_width must also be present.
*
* @return static
*/
public static function photo($url, $width, $height = NULL, ?Provider $provider = NULL, $title = NULL, $author_name = NULL, $author_url = NULL, $cache_age = NULL, $thumbnail_url = NULL, $thumbnail_width = NULL, $thumbnail_height = NULL) {
if (empty($url)) {
throw new \InvalidArgumentException('Photo resources must provide a URL.');
}
$resource = static::link($url, $provider, $title, $author_name, $author_url, $cache_age, $thumbnail_url, $thumbnail_width, $thumbnail_height);
$resource->type = self::TYPE_PHOTO;
$resource->setDimensions($width, $height);
return $resource;
}
/**
* Creates a rich resource.
*
* @param string $html
* The HTML representation of the resource.
* @param int $width
* The width of the resource, in pixels.
* @param int $height
* (optional) The height of the resource, in pixels.
* @param \Drupal\media\OEmbed\Provider $provider
* (optional) The resource provider.
* @param string $title
* (optional) A text title, describing the resource.
* @param string $author_name
* (optional) The name of the author/owner of the resource.
* @param string $author_url
* (optional) A URL for the author/owner of the resource.
* @param int $cache_age
* (optional) The suggested cache lifetime for this resource, in seconds.
* @param string $thumbnail_url
* (optional) A URL to a thumbnail image representing the resource. If this
* parameter is present, $thumbnail_width and $thumbnail_height must also be
* present.
* @param int $thumbnail_width
* (optional) The width of the thumbnail, in pixels. If this parameter is
* present, $thumbnail_url and $thumbnail_height must also be present.
* @param int $thumbnail_height
* (optional) The height of the thumbnail, in pixels. If this parameter is
* present, $thumbnail_url and $thumbnail_width must also be present.
*
* @return static
*/
public static function rich($html, $width, $height = NULL, ?Provider $provider = NULL, $title = NULL, $author_name = NULL, $author_url = NULL, $cache_age = NULL, $thumbnail_url = NULL, $thumbnail_width = NULL, $thumbnail_height = NULL) {
if (empty($html)) {
throw new \InvalidArgumentException('The resource must provide an HTML representation.');
}
$resource = new static($provider, $title, $author_name, $author_url, $cache_age, $thumbnail_url, $thumbnail_width, $thumbnail_height);
$resource->type = self::TYPE_RICH;
$resource->html = $html;
$resource->setDimensions($width, $height);
return $resource;
}
/**
* Creates a video resource.
*
* @param string $html
* The HTML required to display the video.
* @param int $width
* The width of the video, in pixels.
* @param int $height
* (optional) The height of the video, in pixels.
* @param \Drupal\media\OEmbed\Provider $provider
* (optional) The resource provider.
* @param string $title
* (optional) A text title, describing the resource.
* @param string $author_name
* (optional) The name of the author/owner of the resource.
* @param string $author_url
* (optional) A URL for the author/owner of the resource.
* @param int $cache_age
* (optional) The suggested cache lifetime for this resource, in seconds.
* @param string $thumbnail_url
* (optional) A URL to a thumbnail image representing the resource. If this
* parameter is present, $thumbnail_width and $thumbnail_height must also be
* present.
* @param int $thumbnail_width
* (optional) The width of the thumbnail, in pixels. If this parameter is
* present, $thumbnail_url and $thumbnail_height must also be present.
* @param int $thumbnail_height
* (optional) The height of the thumbnail, in pixels. If this parameter is
* present, $thumbnail_url and $thumbnail_width must also be present.
*
* @return static
*/
public static function video($html, $width, $height = NULL, ?Provider $provider = NULL, $title = NULL, $author_name = NULL, $author_url = NULL, $cache_age = NULL, $thumbnail_url = NULL, $thumbnail_width = NULL, $thumbnail_height = NULL) {
$resource = static::rich($html, $width, $height, $provider, $title, $author_name, $author_url, $cache_age, $thumbnail_url, $thumbnail_width, $thumbnail_height);
$resource->type = self::TYPE_VIDEO;
return $resource;
}
/**
* Returns the resource type.
*
* @return string
* The resource type. Will be one of the self::TYPE_* constants.
*/
public function getType() {
return $this->type;
}
/**
* Returns the title of the resource.
*
* @return string|null
* The title of the resource, if known.
*/
public function getTitle() {
return $this->title;
}
/**
* Returns the name of the resource author.
*
* @return string|null
* The name of the resource author, if known.
*/
public function getAuthorName() {
return $this->authorName;
}
/**
* Returns the URL of the resource author.
*
* @return \Drupal\Core\Url|null
* The absolute URL of the resource author, or NULL if none is provided.
*/
public function getAuthorUrl() {
return $this->authorUrl ? Url::fromUri($this->authorUrl)->setAbsolute() : NULL;
}
/**
* Returns the resource provider, if known.
*
* @return \Drupal\media\OEmbed\Provider|null
* The resource provider, or NULL if the provider is not known.
*/
public function getProvider() {
return $this->provider;
}
/**
* Returns the URL of the resource's thumbnail image.
*
* @return \Drupal\Core\Url|null
* The absolute URL of the thumbnail image, or NULL if there isn't one.
*/
public function getThumbnailUrl() {
return $this->thumbnailUrl ? Url::fromUri($this->thumbnailUrl)->setAbsolute() : NULL;
}
/**
* Returns the width of the resource's thumbnail image.
*
* @return int|null
* The thumbnail width in pixels, or NULL if there is no thumbnail.
*/
public function getThumbnailWidth() {
return $this->thumbnailWidth;
}
/**
* Returns the height of the resource's thumbnail image.
*
* @return int|null
* The thumbnail height in pixels, or NULL if there is no thumbnail.
*/
public function getThumbnailHeight() {
return $this->thumbnailHeight;
}
/**
* Returns the width of the resource.
*
* @return int|null
* The width of the resource in pixels, or NULL if the resource has no
* width.
*/
public function getWidth() {
return $this->width;
}
/**
* Returns the height of the resource.
*
* @return int|null
* The height of the resource in pixels, or NULL if the resource has no
* height.
*/
public function getHeight() {
return $this->height;
}
/**
* Returns the URL of the resource. Only applies to 'photo' resources.
*
* @return \Drupal\Core\Url|null
* The resource URL, if it has one.
*/
public function getUrl() {
if ($this->url) {
return Url::fromUri($this->url)->setAbsolute();
}
return NULL;
}
/**
* Returns the HTML representation of the resource.
*
* Only applies to 'rich' and 'video' resources.
*
* @return string|null
* The HTML representation of the resource, if it has one.
*/
public function getHtml() {
return isset($this->html) ? (string) $this->html : NULL;
}
/**
* Sets the thumbnail dimensions.
*
* @param int $width
* The width of the resource.
* @param int $height
* The height of the resource.
*
* @throws \InvalidArgumentException
* If either $width or $height are not numbers greater than zero.
*/
protected function setThumbnailDimensions($width, $height) {
$width = (int) $width;
$height = (int) $height;
if ($width > 0 && $height > 0) {
$this->thumbnailWidth = $width;
$this->thumbnailHeight = $height;
}
else {
throw new \InvalidArgumentException('The thumbnail dimensions must be numbers greater than zero.');
}
}
/**
* Sets the dimensions.
*
* @param int|null $width
* The width of the resource.
* @param int|null $height
* The height of the resource.
*
* @throws \InvalidArgumentException
* If either $width or $height are not numbers greater than zero.
*/
protected function setDimensions($width, $height) {
if ((isset($width) && $width <= 0) || (isset($height) && $height <= 0)) {
throw new \InvalidArgumentException('The dimensions must be NULL or numbers greater than zero.');
}
$this->width = isset($width) ? (int) $width : NULL;
$this->height = isset($height) ? (int) $height : NULL;
}
}

View File

@@ -0,0 +1,67 @@
<?php
namespace Drupal\media\OEmbed;
/**
* Exception thrown if an oEmbed resource cannot be fetched or parsed.
*
* @internal
* This is an internal part of the oEmbed system and should only be used by
* oEmbed-related code in Drupal core.
*/
class ResourceException extends \Exception {
/**
* The URL of the resource.
*
* @var string
*/
protected $url;
/**
* The resource data.
*
* @var array
*/
protected $data = [];
/**
* ResourceException constructor.
*
* @param string $message
* The exception message.
* @param string $url
* The URL of the resource. Can be the actual endpoint URL or the canonical
* URL.
* @param array $data
* (optional) The raw resource data, if available.
* @param \Exception $previous
* (optional) The previous exception, if any.
*/
public function __construct($message, $url, array $data = [], ?\Exception $previous = NULL) {
$this->url = $url;
$this->data = $data;
parent::__construct($message, 0, $previous);
}
/**
* Gets the URL of the resource which caused the exception.
*
* @return string
* The URL of the resource.
*/
public function getUrl() {
return $this->url;
}
/**
* Gets the raw resource data, if available.
*
* @return array
* The resource data.
*/
public function getData() {
return $this->data;
}
}

View File

@@ -0,0 +1,246 @@
<?php
namespace Drupal\media\OEmbed;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Cache\CacheBackendInterface;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\RequestOptions;
use Psr\Http\Client\ClientExceptionInterface;
// cspell:ignore nocdata
/**
* Fetches and caches oEmbed resources.
*/
class ResourceFetcher implements ResourceFetcherInterface {
/**
* The HTTP client.
*
* @var \GuzzleHttp\Client
*/
protected $httpClient;
/**
* The oEmbed provider repository service.
*
* @var \Drupal\media\OEmbed\ProviderRepositoryInterface
*/
protected $providers;
/**
* The cache backend.
*
* @var \Drupal\Core\Cache\CacheBackendInterface
*/
protected $cacheBackend;
/**
* Constructs a ResourceFetcher object.
*
* @param \GuzzleHttp\ClientInterface $http_client
* The HTTP client.
* @param \Drupal\media\OEmbed\ProviderRepositoryInterface $providers
* The oEmbed provider repository service.
* @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
* The cache backend.
*/
public function __construct(ClientInterface $http_client, ProviderRepositoryInterface $providers, CacheBackendInterface $cache_backend) {
$this->httpClient = $http_client;
$this->providers = $providers;
$this->cacheBackend = $cache_backend;
}
/**
* {@inheritdoc}
*/
public function fetchResource($url) {
$cache_id = "media:oembed_resource:$url";
$cached = $this->cacheBackend->get($cache_id);
if ($cached) {
return $this->createResource($cached->data, $url);
}
try {
$response = $this->httpClient->request('GET', $url, [
RequestOptions::TIMEOUT => 5,
]);
}
catch (ClientExceptionInterface $e) {
throw new ResourceException('Could not retrieve the oEmbed resource.', $url, [], $e);
}
[$format] = $response->getHeader('Content-Type');
$content = (string) $response->getBody();
if (strstr($format, 'text/xml') || strstr($format, 'application/xml')) {
$data = $this->parseResourceXml($content, $url);
}
// By default, try to parse the resource data as JSON.
else {
$data = Json::decode($content);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new ResourceException('Error decoding oEmbed resource: ' . json_last_error_msg(), $url);
}
}
if (empty($data) || !is_array($data)) {
throw new ResourceException('The oEmbed resource could not be decoded.', $url);
}
$this->cacheBackend->set($cache_id, $data);
return $this->createResource($data, $url);
}
/**
* Creates a Resource object from raw resource data.
*
* @param array $data
* The resource data returned by the provider.
* @param string $url
* The URL of the resource.
*
* @return \Drupal\media\OEmbed\Resource
* A value object representing the resource.
*
* @throws \Drupal\media\OEmbed\ResourceException
* If the resource cannot be created.
*/
protected function createResource(array $data, $url) {
$data += [
'title' => NULL,
'author_name' => NULL,
'author_url' => NULL,
'provider_name' => NULL,
'cache_age' => NULL,
'thumbnail_url' => NULL,
'thumbnail_width' => NULL,
'thumbnail_height' => NULL,
'width' => NULL,
'height' => NULL,
'url' => NULL,
'html' => NULL,
'version' => NULL,
];
if ($data['version'] !== '1.0') {
throw new ResourceException("Resource version must be '1.0'", $url, $data);
}
// Prepare the arguments to pass to the factory method.
$provider = $data['provider_name'] ? $this->providers->get($data['provider_name']) : NULL;
// The Resource object will validate the data we create it with and throw an
// exception if anything looks wrong. For better debugging, catch those
// exceptions and wrap them in a more specific and useful exception.
try {
switch ($data['type']) {
case Resource::TYPE_LINK:
return Resource::link(
$data['url'],
$provider,
$data['title'],
$data['author_name'],
$data['author_url'],
$data['cache_age'],
$data['thumbnail_url'],
$data['thumbnail_width'],
$data['thumbnail_height']
);
case Resource::TYPE_PHOTO:
return Resource::photo(
$data['url'],
$data['width'],
$data['height'],
$provider,
$data['title'],
$data['author_name'],
$data['author_url'],
$data['cache_age'],
$data['thumbnail_url'],
$data['thumbnail_width'],
$data['thumbnail_height']
);
case Resource::TYPE_RICH:
return Resource::rich(
$data['html'],
$data['width'],
$data['height'],
$provider,
$data['title'],
$data['author_name'],
$data['author_url'],
$data['cache_age'],
$data['thumbnail_url'],
$data['thumbnail_width'],
$data['thumbnail_height']
);
case Resource::TYPE_VIDEO:
return Resource::video(
$data['html'],
$data['width'],
$data['height'],
$provider,
$data['title'],
$data['author_name'],
$data['author_url'],
$data['cache_age'],
$data['thumbnail_url'],
$data['thumbnail_width'],
$data['thumbnail_height']
);
default:
throw new ResourceException('Unknown resource type: ' . $data['type'], $url, $data);
}
}
catch (\InvalidArgumentException $e) {
throw new ResourceException($e->getMessage(), $url, $data, $e);
}
}
/**
* Parses XML resource data.
*
* @param string $data
* The raw XML for the resource.
* @param string $url
* The resource URL.
*
* @return array
* The parsed resource data.
*
* @throws \Drupal\media\OEmbed\ResourceException
* If the resource data could not be parsed.
*/
protected function parseResourceXml($data, $url) {
// Enable userspace error handling.
$was_using_internal_errors = libxml_use_internal_errors(TRUE);
libxml_clear_errors();
$content = simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA);
// Restore the previous error handling behavior.
libxml_use_internal_errors($was_using_internal_errors);
$error = libxml_get_last_error();
if ($error) {
libxml_clear_errors();
throw new ResourceException($error->message, $url);
}
elseif ($content === FALSE) {
throw new ResourceException('The fetched resource could not be parsed.', $url);
}
// Convert XML to JSON so that the parsed resource has a consistent array
// structure, regardless of any XML attributes or quirks of the XML parser.
$data = Json::encode($content);
return Json::decode($data);
}
}

View File

@@ -0,0 +1,32 @@
<?php
namespace Drupal\media\OEmbed;
/**
* Defines an interface for an oEmbed resource fetcher service.
*
* The resource fetcher's only responsibility is to retrieve oEmbed resource
* data from an endpoint URL (i.e., as returned by
* \Drupal\media\OEmbed\UrlResolverInterface::getResourceUrl()) and return a
* \Drupal\media\OEmbed\Resource value object.
*/
interface ResourceFetcherInterface {
/**
* Fetches an oEmbed resource.
*
* @param string $url
* Endpoint-specific URL of the oEmbed resource.
*
* @return \Drupal\media\OEmbed\Resource
* A resource object built from the oEmbed resource data.
*
* @see https://oembed.com/#section2
*
* @throws \Drupal\media\OEmbed\ResourceException
* If the oEmbed endpoint is not reachable or the response returns an
* unexpected Content-Type header.
*/
public function fetchResource($url);
}

View File

@@ -0,0 +1,210 @@
<?php
namespace Drupal\media\OEmbed;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use GuzzleHttp\ClientInterface;
use Psr\Http\Client\ClientExceptionInterface;
// cspell:ignore omitscript
/**
* Converts oEmbed media URLs into endpoint-specific resource URLs.
*/
class UrlResolver implements UrlResolverInterface {
/**
* The HTTP client.
*
* @var \GuzzleHttp\Client
*/
protected $httpClient;
/**
* The OEmbed provider repository service.
*
* @var \Drupal\media\OEmbed\ProviderRepositoryInterface
*/
protected $providers;
/**
* The OEmbed resource fetcher service.
*
* @var \Drupal\media\OEmbed\ResourceFetcherInterface
*/
protected $resourceFetcher;
/**
* The module handler service.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* Static cache of discovered oEmbed resource URLs, keyed by canonical URL.
*
* A discovered resource URL is the actual endpoint URL for a specific media
* object, fetched from its canonical URL.
*
* @var string[]
*/
protected $urlCache = [];
/**
* The cache backend.
*
* @var \Drupal\Core\Cache\CacheBackendInterface
*/
protected $cacheBackend;
/**
* Constructs a UrlResolver object.
*
* @param \Drupal\media\OEmbed\ProviderRepositoryInterface $providers
* The oEmbed provider repository service.
* @param \Drupal\media\OEmbed\ResourceFetcherInterface $resource_fetcher
* The OEmbed resource fetcher service.
* @param \GuzzleHttp\ClientInterface $http_client
* The HTTP client.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler service.
* @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
* The cache backend.
*/
public function __construct(ProviderRepositoryInterface $providers, ResourceFetcherInterface $resource_fetcher, ClientInterface $http_client, ModuleHandlerInterface $module_handler, CacheBackendInterface $cache_backend) {
$this->providers = $providers;
$this->resourceFetcher = $resource_fetcher;
$this->httpClient = $http_client;
$this->moduleHandler = $module_handler;
$this->cacheBackend = $cache_backend;
}
/**
* Runs oEmbed discovery and returns the endpoint URL if successful.
*
* @param string $url
* The resource's URL.
*
* @return string|bool
* URL of the oEmbed endpoint, or FALSE if the discovery was unsuccessful.
*/
protected function discoverResourceUrl($url) {
try {
$response = $this->httpClient->get($url);
}
catch (ClientExceptionInterface) {
return FALSE;
}
$document = Html::load((string) $response->getBody());
$xpath = new \DOMXpath($document);
return $this->findUrl($xpath, 'json') ?: $this->findUrl($xpath, 'xml');
}
/**
* Tries to find the oEmbed URL in a DOM.
*
* @param \DOMXPath $xpath
* Page HTML as DOMXPath.
* @param string $format
* Format of oEmbed resource. Possible values are 'json' and 'xml'.
*
* @return bool|string
* A URL to an oEmbed resource or FALSE if not found.
*/
protected function findUrl(\DOMXPath $xpath, $format) {
$result = $xpath->query("//link[@type='application/$format+oembed']");
return $result->length ? $result->item(0)->getAttribute('href') : FALSE;
}
/**
* {@inheritdoc}
*/
public function getProviderByUrl($url) {
// Check the URL against every scheme of every endpoint of every provider
// until we find a match.
foreach ($this->providers->getAll() as $provider_info) {
foreach ($provider_info->getEndpoints() as $endpoint) {
if ($endpoint->matchUrl($url)) {
return $provider_info;
}
}
}
$resource_url = $this->discoverResourceUrl($url);
if ($resource_url) {
return $this->resourceFetcher->fetchResource($resource_url)->getProvider();
}
throw new ResourceException('No matching provider found.', $url);
}
/**
* {@inheritdoc}
*/
public function getResourceUrl($url, $max_width = NULL, $max_height = NULL) {
// Try to get the resource URL from the static cache.
if (isset($this->urlCache[$url])) {
return $this->urlCache[$url];
}
// Try to get the resource URL from the persistent cache.
$cache_id = "media:oembed_resource_url:$url:$max_width:$max_height";
$cached = $this->cacheBackend->get($cache_id);
if ($cached) {
$this->urlCache[$url] = $cached->data;
return $this->urlCache[$url];
}
$provider = $this->getProviderByUrl($url);
$resource_url = $this->getEndpointMatchingUrl($url, $provider);
$parsed_url = UrlHelper::parse($resource_url);
if ($max_width) {
$parsed_url['query']['maxwidth'] = $max_width;
}
if ($max_height) {
$parsed_url['query']['maxheight'] = $max_height;
}
// Let other modules alter the resource URL, because some oEmbed providers
// provide extra parameters in the query string. For example, Instagram also
// supports the 'omitscript' parameter.
$this->moduleHandler->alter('oembed_resource_url', $parsed_url, $provider);
$resource_url = $parsed_url['path'] . '?' . UrlHelper::buildQuery($parsed_url['query']);
$this->urlCache[$url] = $resource_url;
$this->cacheBackend->set($cache_id, $resource_url);
return $resource_url;
}
/**
* For the given media item URL find an endpoint with schemes that match.
*
* @param string $url
* The media URL used to lookup the matching endpoint.
* @param \Drupal\media\OEmbed\Provider $provider
* The oEmbed provider for the asset.
*
* @return string
* The resource URL.
*/
protected function getEndpointMatchingUrl($url, Provider $provider) {
$endpoints = $provider->getEndpoints();
$resource_url = reset($endpoints)->buildResourceUrl($url);
foreach ($endpoints as $endpoint) {
if ($endpoint->matchUrl($url)) {
$resource_url = $endpoint->buildResourceUrl($url);
break;
}
}
return $resource_url ?? reset($endpoints)->buildResourceUrl($url);
}
}

View File

@@ -0,0 +1,45 @@
<?php
namespace Drupal\media\OEmbed;
/**
* Defines the interface for the oEmbed URL resolver service.
*
* The URL resolver is responsible for converting oEmbed-compatible media asset
* URLs into canonical resource URLs, at which an oEmbed representation of the
* asset can be retrieved.
*/
interface UrlResolverInterface {
/**
* Tries to determine the oEmbed provider for a media asset URL.
*
* @param string $url
* The media asset URL.
*
* @return \Drupal\media\OEmbed\Provider
* The oEmbed provider for the asset.
*
* @throws \Drupal\media\OEmbed\ResourceException
* If the provider cannot be determined.
* @throws \Drupal\media\OEmbed\ProviderException
* If tne oEmbed provider causes an error.
*/
public function getProviderByUrl($url);
/**
* Builds the resource URL for a media asset URL.
*
* @param string $url
* The media asset URL.
* @param int $max_width
* (optional) Maximum width of the oEmbed resource, in pixels.
* @param int $max_height
* (optional) Maximum height of the oEmbed resource, in pixels.
*
* @return string
* Returns the resource URL corresponding to the given media item URL.
*/
public function getResourceUrl($url, $max_width = NULL, $max_height = NULL);
}

View File

@@ -0,0 +1,94 @@
<?php
namespace Drupal\media\Plugin\Derivative;
use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Generates media-related local tasks.
*/
class DynamicLocalTasks extends DeriverBase implements ContainerDeriverInterface {
use StringTranslationTrait;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The media settings config.
*
* @var \Drupal\Core\Config\ImmutableConfig
*/
protected $config;
/**
* Creates a DynamicLocalTasks object.
*
* @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
* The translation manager.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory.
*/
public function __construct(TranslationInterface $string_translation, EntityTypeManagerInterface $entity_type_manager, ConfigFactoryInterface $config_factory) {
$this->stringTranslation = $string_translation;
$this->entityTypeManager = $entity_type_manager;
$this->config = $config_factory->get('media.settings');
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, $base_plugin_id) {
return new static(
$container->get('string_translation'),
$container->get('entity_type.manager'),
$container->get('config.factory')
);
}
/**
* {@inheritdoc}
*/
public function getDerivativeDefinitions($base_plugin_definition) {
// Provide an edit_form task if standalone media URLs are enabled.
$this->derivatives["entity.media.canonical"] = [
'route_name' => "entity.media.canonical",
'title' => $this->t('Edit'),
'base_route' => "entity.media.canonical",
'weight' => 1,
] + $base_plugin_definition;
if ($this->config->get('standalone_url')) {
$this->derivatives["entity.media.canonical"]['title'] = $this->t('View');
$this->derivatives["entity.media.edit_form"] = [
'route_name' => "entity.media.edit_form",
'title' => $this->t('Edit'),
'base_route' => 'entity.media.canonical',
'weight' => 2,
] + $base_plugin_definition;
}
$this->derivatives["entity.media.delete_form"] = [
'route_name' => "entity.media.delete_form",
'title' => $this->t('Delete'),
'base_route' => "entity.media.canonical",
'weight' => 10,
] + $base_plugin_definition;
return $this->derivatives;
}
}

View File

@@ -0,0 +1,63 @@
<?php
namespace Drupal\media\Plugin\EntityReferenceSelection;
use Drupal\Core\Entity\Attribute\EntityReferenceSelection;
use Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection;
use Drupal\Core\StringTranslation\TranslatableMarkup;
/**
* Provides specific access control for the media entity type.
*/
#[EntityReferenceSelection(
id: "default:media",
label: new TranslatableMarkup("Media selection"),
entity_types: ["media"],
group: "default",
weight: 1
)]
class MediaSelection extends DefaultSelection {
/**
* {@inheritdoc}
*/
protected function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') {
$query = parent::buildEntityQuery($match, $match_operator);
// Ensure that users with insufficient permission cannot see unpublished
// entities.
if (!$this->currentUser->hasPermission('administer media')) {
$query->condition('status', 1);
}
return $query;
}
/**
* {@inheritdoc}
*/
public function createNewEntity($entity_type_id, $bundle, $label, $uid) {
$media = parent::createNewEntity($entity_type_id, $bundle, $label, $uid);
// In order to create a referenceable media, it needs to published.
/** @var \Drupal\media\MediaInterface $media */
$media->setPublished();
return $media;
}
/**
* {@inheritdoc}
*/
public function validateReferenceableNewEntities(array $entities) {
$entities = parent::validateReferenceableNewEntities($entities);
// Mirror the conditions checked in buildEntityQuery().
if (!$this->currentUser->hasPermission('administer media')) {
$entities = array_filter($entities, function ($media) {
/** @var \Drupal\media\MediaInterface $media */
return $media->isPublished();
});
}
return $entities;
}
}

View File

@@ -0,0 +1,212 @@
<?php
namespace Drupal\media\Plugin\Field\FieldFormatter;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Field\Attribute\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\File\FileUrlGeneratorInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\image\ImageStyleStorageInterface;
use Drupal\image\Plugin\Field\FieldFormatter\ImageFormatter;
use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem;
use Drupal\Core\Render\RendererInterface;
use Drupal\media\MediaInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
/**
* Plugin implementation of the 'media_thumbnail' formatter.
*/
#[FieldFormatter(
id: 'media_thumbnail',
label: new TranslatableMarkup('Thumbnail'),
field_types: [
'entity_reference',
],
)]
class MediaThumbnailFormatter extends ImageFormatter {
/**
* The renderer service.
*
* @var \Drupal\Core\Render\RendererInterface
*/
protected $renderer;
/**
* Constructs a MediaThumbnailFormatter object.
*
* @param string $plugin_id
* The plugin_id for the formatter.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
* The definition of the field to which the formatter is associated.
* @param array $settings
* The formatter settings.
* @param string $label
* The formatter label display setting.
* @param string $view_mode
* The view mode.
* @param array $third_party_settings
* Any third party settings.
* @param \Drupal\Core\Session\AccountInterface $current_user
* The current user.
* @param \Drupal\image\ImageStyleStorageInterface $image_style_storage
* The image style entity storage handler.
* @param \Drupal\Core\File\FileUrlGeneratorInterface $file_url_generator
* The file URL generator.
* @param \Drupal\Core\Render\RendererInterface $renderer
* The renderer service.
*/
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, AccountInterface $current_user, ImageStyleStorageInterface $image_style_storage, FileUrlGeneratorInterface $file_url_generator, RendererInterface $renderer) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings, $current_user, $image_style_storage, $file_url_generator);
$this->renderer = $renderer;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$plugin_id,
$plugin_definition,
$configuration['field_definition'],
$configuration['settings'],
$configuration['label'],
$configuration['view_mode'],
$configuration['third_party_settings'],
$container->get('current_user'),
$container->get('entity_type.manager')->getStorage('image_style'),
$container->get('file_url_generator'),
$container->get('renderer')
);
}
/**
* {@inheritdoc}
*
* This has to be overridden because FileFormatterBase expects $item to be
* of type \Drupal\file\Plugin\Field\FieldType\FileItem and calls
* isDisplayed() which is not in FieldItemInterface.
*/
protected function needsEntityLoad(EntityReferenceItem $item) {
return !$item->hasNewEntity();
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$element = parent::settingsForm($form, $form_state);
$link_types = [
'content' => $this->t('Content'),
'media' => $this->t('Media item'),
];
$element['image_link']['#options'] = $link_types;
return $element;
}
/**
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = parent::settingsSummary();
// The parent class adds summary text if the image_link setting is
// 'content'. Here we only have to add summary text if the setting
// is 'media'.
if ($this->getSetting('image_link') === 'media') {
$summary[] = $this->t('Linked to media item');
}
return $summary;
}
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
$media_items = $this->getEntitiesToView($items, $langcode);
// Early opt-out if the field is empty.
if (empty($media_items)) {
return $elements;
}
$image_style_setting = $this->getSetting('image_style');
/** @var \Drupal\media\MediaInterface[] $media_items */
foreach ($media_items as $delta => $media) {
$elements[$delta] = [
'#theme' => 'image_formatter',
'#item' => $media->get('thumbnail')->first(),
'#item_attributes' => [
'loading' => $this->getSetting('image_loading')['attribute'],
],
'#image_style' => $this->getSetting('image_style'),
'#url' => $this->getMediaThumbnailUrl($media, $items->getEntity()),
];
// Add cacheability of each item in the field.
$this->renderer->addCacheableDependency($elements[$delta], $media);
}
// Add cacheability of the image style setting.
if ($this->getSetting('image_link') && ($image_style = $this->imageStyleStorage->load($image_style_setting))) {
$this->renderer->addCacheableDependency($elements, $image_style);
}
return $elements;
}
/**
* {@inheritdoc}
*/
public static function isApplicable(FieldDefinitionInterface $field_definition) {
// This formatter is only available for entity types that reference
// media items.
return ($field_definition->getFieldStorageDefinition()->getSetting('target_type') == 'media');
}
/**
* {@inheritdoc}
*/
protected function checkAccess(EntityInterface $entity) {
return $entity->access('view', NULL, TRUE)
->andIf(parent::checkAccess($entity));
}
/**
* Get the URL for the media thumbnail.
*
* @param \Drupal\media\MediaInterface $media
* The media item.
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity that the field belongs to.
*
* @return \Drupal\Core\Url|null
* The URL object for the media item or null if we don't want to add
* a link.
*/
protected function getMediaThumbnailUrl(MediaInterface $media, EntityInterface $entity) {
$url = NULL;
$image_link_setting = $this->getSetting('image_link');
// Check if the formatter involves a link.
if ($image_link_setting == 'content') {
if (!$entity->isNew()) {
$url = $entity->toUrl();
}
}
elseif ($image_link_setting === 'media') {
$url = $media->toUrl();
}
return $url;
}
}

View File

@@ -0,0 +1,353 @@
<?php
namespace Drupal\media\Plugin\Field\FieldFormatter;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Field\Attribute\FieldFormatter;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Url;
use Drupal\media\Entity\MediaType;
use Drupal\media\IFrameUrlHelper;
use Drupal\media\OEmbed\Resource;
use Drupal\media\OEmbed\ResourceException;
use Drupal\media\OEmbed\ResourceFetcherInterface;
use Drupal\media\OEmbed\UrlResolverInterface;
use Drupal\media\Plugin\media\Source\OEmbedInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Plugin implementation of the 'oembed' formatter.
*
* @internal
* This is an internal part of the oEmbed system and should only be used by
* oEmbed-related code in Drupal core.
*/
#[FieldFormatter(
id: 'oembed',
label: new TranslatableMarkup('oEmbed content'),
field_types: [
'link',
'string',
'string_long',
],
)]
class OEmbedFormatter extends FormatterBase {
/**
* The messenger service.
*
* @var \Drupal\Core\Messenger\MessengerInterface
*/
protected $messenger;
/**
* The oEmbed resource fetcher.
*
* @var \Drupal\media\OEmbed\ResourceFetcherInterface
*/
protected $resourceFetcher;
/**
* The oEmbed URL resolver service.
*
* @var \Drupal\media\OEmbed\UrlResolverInterface
*/
protected $urlResolver;
/**
* The logger service.
*
* @var \Psr\Log\LoggerInterface
*/
protected $logger;
/**
* The media settings config.
*
* @var \Drupal\Core\Config\ImmutableConfig
*/
protected $config;
/**
* The iFrame URL helper service.
*
* @var \Drupal\media\IFrameUrlHelper
*/
protected $iFrameUrlHelper;
/**
* Constructs an OEmbedFormatter instance.
*
* @param string $plugin_id
* The plugin ID for the formatter.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
* The definition of the field to which the formatter is associated.
* @param array $settings
* The formatter settings.
* @param string $label
* The formatter label display setting.
* @param string $view_mode
* The view mode.
* @param array $third_party_settings
* Any third party settings.
* @param \Drupal\Core\Messenger\MessengerInterface $messenger
* The messenger service.
* @param \Drupal\media\OEmbed\ResourceFetcherInterface $resource_fetcher
* The oEmbed resource fetcher service.
* @param \Drupal\media\OEmbed\UrlResolverInterface $url_resolver
* The oEmbed URL resolver service.
* @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_factory
* The logger factory service.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory service.
* @param \Drupal\media\IFrameUrlHelper $iframe_url_helper
* The iFrame URL helper service.
*/
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, MessengerInterface $messenger, ResourceFetcherInterface $resource_fetcher, UrlResolverInterface $url_resolver, LoggerChannelFactoryInterface $logger_factory, ConfigFactoryInterface $config_factory, IFrameUrlHelper $iframe_url_helper) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
$this->messenger = $messenger;
$this->resourceFetcher = $resource_fetcher;
$this->urlResolver = $url_resolver;
$this->logger = $logger_factory->get('media');
$this->config = $config_factory->get('media.settings');
$this->iFrameUrlHelper = $iframe_url_helper;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$plugin_id,
$plugin_definition,
$configuration['field_definition'],
$configuration['settings'],
$configuration['label'],
$configuration['view_mode'],
$configuration['third_party_settings'],
$container->get('messenger'),
$container->get('media.oembed.resource_fetcher'),
$container->get('media.oembed.url_resolver'),
$container->get('logger.factory'),
$container->get('config.factory'),
$container->get('media.oembed.iframe_url_helper')
);
}
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return [
'max_width' => 0,
'max_height' => 0,
'loading' => [
'attribute' => 'lazy',
],
] + parent::defaultSettings();
}
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$element = [];
$max_width = $this->getSetting('max_width');
$max_height = $this->getSetting('max_height');
foreach ($items as $delta => $item) {
$main_property = $item->getFieldDefinition()->getFieldStorageDefinition()->getMainPropertyName();
$value = $item->{$main_property};
if (empty($value)) {
continue;
}
try {
$resource_url = $this->urlResolver->getResourceUrl($value, $max_width, $max_height);
$resource = $this->resourceFetcher->fetchResource($resource_url);
}
catch (ResourceException $exception) {
$this->logger->error("Could not retrieve the remote URL (@url): %error", [
'@url' => $value,
'%error' => $exception->getPrevious() ? $exception->getPrevious()->getMessage() : $exception->getMessage(),
'exception' => $exception,
]);
continue;
}
if ($resource->getType() === Resource::TYPE_LINK) {
$element[$delta] = [
'#title' => $resource->getTitle(),
'#type' => 'link',
'#url' => Url::fromUri($value),
];
}
elseif ($resource->getType() === Resource::TYPE_PHOTO) {
$element[$delta] = [
'#theme' => 'image',
'#uri' => $resource->getUrl()->toString(),
'#width' => $resource->getWidth(),
'#height' => $resource->getHeight(),
'#attributes' => [
'loading' => $this->getSetting('loading')['attribute'],
],
];
}
else {
$url = Url::fromRoute('media.oembed_iframe', [], [
'absolute' => TRUE,
'query' => [
'url' => $value,
'max_width' => $max_width,
'max_height' => $max_height,
'hash' => $this->iFrameUrlHelper->getHash($value, $max_width, $max_height),
],
]);
$domain = $this->config->get('iframe_domain');
if ($domain) {
$url->setOption('base_url', $domain);
}
// Render videos and rich content in an iframe for security reasons.
// @see: https://oembed.com/#section3
$element[$delta] = [
'#type' => 'html_tag',
'#tag' => 'iframe',
'#attributes' => [
'src' => $url->toString(),
'scrolling' => FALSE,
// External service is not supposed to send something larger
// than the max width or max height, so those values should be used.
'width' => $resource->getWidth() ?: $max_width,
'height' => $resource->getHeight() ?: $max_height,
'class' => ['media-oembed-content'],
'loading' => $this->getSetting('loading')['attribute'],
],
'#attached' => [
'library' => [
'media/oembed.formatter',
],
],
];
// An empty title attribute will disable title inheritance, so only
// add it if the resource has a title.
$title = $resource->getTitle();
if ($title) {
$element[$delta]['#attributes']['title'] = $title;
}
CacheableMetadata::createFromObject($resource)
->addCacheTags($this->config->getCacheTags())
->applyTo($element[$delta]);
}
}
return $element;
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$form = parent::settingsForm($form, $form_state) + [
'max_width' => [
'#type' => 'number',
'#title' => $this->t('Maximum width'),
'#default_value' => $this->getSetting('max_width'),
'#size' => 5,
'#maxlength' => 5,
'#field_suffix' => $this->t('pixels'),
'#min' => 0,
],
'max_height' => [
'#type' => 'number',
'#title' => $this->t('Maximum height'),
'#default_value' => $this->getSetting('max_height'),
'#size' => 5,
'#maxlength' => 5,
'#field_suffix' => $this->t('pixels'),
'#min' => 0,
],
'loading' => [
'#type' => 'details',
'#title' => $this->t('oEmbed loading'),
'#description' => $this->t('Lazy render oEmbed with native loading attribute (<em>loading="lazy"</em>). This improves performance by allowing browsers to lazily load assets.'),
'attribute' => [
'#title' => $this->t('oEmbed loading attribute'),
'#type' => 'radios',
'#default_value' => $this->getSetting('loading')['attribute'],
'#options' => [
'lazy' => $this->t('Lazy (<em>loading="lazy"</em>)'),
'eager' => $this->t('Eager (<em>loading="eager"</em>)'),
],
'#description' => $this->t('Select the loading attribute for oEmbed. <a href=":link">Learn more about the loading attribute for oEmbed.</a>', [
':link' => 'https://html.spec.whatwg.org/multipage/urls-and-fetching.html#lazy-loading-attributes',
]),
],
],
];
$form['loading']['attribute']['lazy']['#description'] = $this->t('Delays loading the resource until that section of the page is visible in the browser. When in doubt, lazy loading is recommended.');
$form['loading']['attribute']['eager']['#description'] = $this->t('Force browsers to download a resource as soon as possible. This is the browser default for legacy reasons. Only use this option when the resource is always expected to render.');
return $form;
}
/**
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = parent::settingsSummary();
if ($this->getSetting('max_width') && $this->getSetting('max_height')) {
$summary[] = $this->t('Maximum size: %max_width x %max_height pixels', [
'%max_width' => $this->getSetting('max_width'),
'%max_height' => $this->getSetting('max_height'),
]);
}
elseif ($this->getSetting('max_width')) {
$summary[] = $this->t('Maximum width: %max_width pixels', [
'%max_width' => $this->getSetting('max_width'),
]);
}
elseif ($this->getSetting('max_height')) {
$summary[] = $this->t('Maximum height: %max_height pixels', [
'%max_height' => $this->getSetting('max_height'),
]);
}
$summary[] = $this->t('Loading attribute: @attribute', [
'@attribute' => $this->getSetting('loading')['attribute'],
]);
return $summary;
}
/**
* {@inheritdoc}
*/
public static function isApplicable(FieldDefinitionInterface $field_definition) {
if ($field_definition->getTargetEntityTypeId() !== 'media') {
return FALSE;
}
if (parent::isApplicable($field_definition)) {
$media_type = $field_definition->getTargetBundle();
if ($media_type) {
$media_type = MediaType::load($media_type);
return $media_type && $media_type->getSource() instanceof OEmbedInterface;
}
}
return FALSE;
}
}

View File

@@ -0,0 +1,63 @@
<?php
namespace Drupal\media\Plugin\Field\FieldWidget;
use Drupal\Core\Field\Attribute\FieldWidget;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\Plugin\Field\FieldWidget\StringTextfieldWidget;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\media\Entity\MediaType;
use Drupal\media\Plugin\media\Source\OEmbedInterface;
/**
* Plugin implementation of the 'oembed_textfield' widget.
*
* @internal
* This is an internal part of the oEmbed system and should only be used by
* oEmbed-related code in Drupal core.
*/
#[FieldWidget(
id: 'oembed_textfield',
label: new TranslatableMarkup('oEmbed URL'),
field_types: ['string'],
)]
class OEmbedWidget extends StringTextfieldWidget {
/**
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$element = parent::formElement($items, $delta, $element, $form, $form_state);
/** @var \Drupal\media\Plugin\media\Source\OEmbedInterface $source */
$source = $items->getEntity()->getSource();
$message = $this->t('You can link to media from the following services: @providers', ['@providers' => implode(', ', $source->getProviders())]);
if (!empty($element['value']['#description'])) {
$element['value']['#description'] = [
'#theme' => 'item_list',
'#items' => [$element['value']['#description'], $message],
];
}
else {
$element['value']['#description'] = $message;
}
return $element;
}
/**
* {@inheritdoc}
*/
public static function isApplicable(FieldDefinitionInterface $field_definition) {
$target_bundle = $field_definition->getTargetBundle();
if (!parent::isApplicable($field_definition) || $field_definition->getTargetEntityTypeId() !== 'media' || !$target_bundle) {
return FALSE;
}
return MediaType::load($target_bundle)->getSource() instanceof OEmbedInterface;
}
}

View File

@@ -0,0 +1,536 @@
<?php
namespace Drupal\media\Plugin\Filter;
use Drupal\Component\Utility\Html;
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceEntityFormatter;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\Render\RenderContext;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Security\TrustedCallbackInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\filter\Attribute\Filter;
use Drupal\filter\FilterProcessResult;
use Drupal\filter\Plugin\FilterBase;
use Drupal\filter\Plugin\FilterInterface;
use Drupal\image\Plugin\Field\FieldType\ImageItem;
use Drupal\media\MediaInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a filter to embed media items using a custom tag.
*
* @internal
*/
#[Filter(
id: "media_embed",
title: new TranslatableMarkup("Embed media"),
description: new TranslatableMarkup("Embeds media items using a custom tag, <code>&lt;drupal-media&gt;</code>. If used in conjunction with the 'Align/Caption' filters, make sure this filter is configured to run after them."),
type: FilterInterface::TYPE_TRANSFORM_REVERSIBLE,
weight: 100,
settings: [
"default_view_mode" => "default",
"allowed_view_modes" => [],
"allowed_media_types" => [],
],
)]
class MediaEmbed extends FilterBase implements ContainerFactoryPluginInterface, TrustedCallbackInterface {
/**
* The entity repository.
*
* @var \Drupal\Core\Entity\EntityRepositoryInterface
*/
protected $entityRepository;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The entity display repository.
*
* @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface
*/
protected $entityDisplayRepository;
/**
* The entity type bundle info service.
*
* @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
*/
protected $entityTypeBundleInfo;
/**
* The renderer.
*
* @var \Drupal\Core\Render\RendererInterface
*/
protected $renderer;
/**
* The logger factory.
*
* @var \Drupal\Core\Logger\LoggerChannelFactoryInterface
*/
protected $loggerFactory;
/**
* An array of counters for the recursive rendering protection.
*
* Each counter takes into account all the relevant information about the
* field and the referenced entity that is being rendered.
*
* @var array
*
* @see \Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceEntityFormatter::$recursiveRenderDepth
*/
protected static $recursiveRenderDepth = [];
/**
* Constructs a MediaEmbed object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin ID for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
* The entity repository.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Core\Entity\EntityDisplayRepositoryInterface $entity_display_repository
* The entity display repository.
* @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $bundle_info
* The entity type bundle info service.
* @param \Drupal\Core\Render\RendererInterface $renderer
* The renderer.
* @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_factory
* The logger factory.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityRepositoryInterface $entity_repository, EntityTypeManagerInterface $entity_type_manager, EntityDisplayRepositoryInterface $entity_display_repository, EntityTypeBundleInfoInterface $bundle_info, RendererInterface $renderer, LoggerChannelFactoryInterface $logger_factory) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityRepository = $entity_repository;
$this->entityTypeManager = $entity_type_manager;
$this->entityDisplayRepository = $entity_display_repository;
$this->entityTypeBundleInfo = $bundle_info;
$this->renderer = $renderer;
$this->loggerFactory = $logger_factory;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('entity.repository'),
$container->get('entity_type.manager'),
$container->get('entity_display.repository'),
$container->get('entity_type.bundle.info'),
$container->get('renderer'),
$container->get('logger.factory')
);
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$view_mode_options = $this->entityDisplayRepository->getViewModeOptions('media');
$form['default_view_mode'] = [
'#type' => 'select',
'#options' => $view_mode_options,
'#title' => $this->t('Default view mode'),
'#default_value' => $this->settings['default_view_mode'],
'#description' => $this->t('The view mode that an embedded media item should be displayed in by default. This can be overridden using the <code>data-view-mode</code> attribute.'),
];
$bundles = $this->entityTypeBundleInfo->getBundleInfo('media');
$bundle_options = array_map(function ($item) {
return $item['label'];
}, $bundles);
$form['allowed_media_types'] = [
'#title' => $this->t('Media types selectable in the Media Library'),
'#type' => 'checkboxes',
'#options' => $bundle_options,
'#default_value' => $this->settings['allowed_media_types'],
'#description' => $this->t('If none are selected, all will be allowed.'),
'#element_validate' => [[static::class, 'validateOptions']],
];
$form['allowed_view_modes'] = [
'#title' => $this->t("View modes selectable in the 'Edit media' dialog"),
'#type' => 'checkboxes',
'#options' => $view_mode_options,
'#default_value' => $this->settings['allowed_view_modes'],
'#description' => $this->t("If two or more view modes are selected, users will be able to update the view mode that an embedded media item should be displayed in after it has been embedded. If less than two view modes are selected, media will be embedded using the default view mode and no view mode options will appear after a media item has been embedded."),
'#element_validate' => [[static::class, 'validateOptions']],
];
return $form;
}
/**
* Form element validation handler.
*
* @param array $element
* The allowed_view_modes form element.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state.
*/
public static function validateOptions(array &$element, FormStateInterface $form_state) {
// Filters the #value property so only selected values appear in the
// config.
$form_state->setValueForElement($element, array_filter($element['#value']));
}
/**
* Builds the render array for the given media entity in the given langcode.
*
* @param \Drupal\media\MediaInterface $media
* A media entity to render.
* @param string $view_mode
* The view mode to render it in.
* @param string $langcode
* Language code in which the media entity should be rendered.
*
* @return array
* A render array.
*/
protected function renderMedia(MediaInterface $media, $view_mode, $langcode) {
// Due to render caching and delayed calls, filtering happens later
// in the rendering process through a '#pre_render' callback, so we
// need to generate a counter for the media entity that is being embedded.
// @see \Drupal\filter\Element\ProcessedText::preRenderText()
$recursive_render_id = $media->uuid();
if (isset(static::$recursiveRenderDepth[$recursive_render_id])) {
static::$recursiveRenderDepth[$recursive_render_id]++;
}
else {
static::$recursiveRenderDepth[$recursive_render_id] = 1;
}
// Protect ourselves from recursive rendering: return an empty render array.
if (static::$recursiveRenderDepth[$recursive_render_id] > EntityReferenceEntityFormatter::RECURSIVE_RENDER_LIMIT) {
$this->loggerFactory->get('media')->error('During rendering of embedded media: recursive rendering detected for %entity_id. Aborting rendering.', [
'%entity_id' => $media->id(),
]);
return [];
}
$build = $this->entityTypeManager
->getViewBuilder('media')
->view($media, $view_mode, $langcode);
// Allows other modules to treat embedded media items differently.
$build['#embed'] = TRUE;
// There are a few concerns when rendering an embedded media entity:
// - entity access checking happens not during rendering but during routing,
// and therefore we have to do it explicitly here for the embedded entity.
$build['#access'] = $media->access('view', NULL, TRUE);
// - caching an embedded media entity separately is unnecessary; the host
// entity is already render cached.
unset($build['#cache']['keys']);
// - Contextual Links do not make sense for embedded entities; we only allow
// the host entity to be contextually managed.
$build['#pre_render'][] = static::class . '::disableContextualLinks';
// - default styling may break captioned media embeds; attach asset library
// to ensure captions behave as intended. Do not set this at the root
// level of the render array, otherwise it will be attached always,
// instead of only when #access allows this media to be viewed and hence
// only when media is actually rendered.
$build[':media_embed']['#attached']['library'][] = 'media/filter.caption';
return $build;
}
/**
* Builds the render array for the indicator when media cannot be loaded.
*
* @return array
* A render array.
*/
protected function renderMissingMediaIndicator() {
return [
'#theme' => 'media_embed_error',
'#message' => $this->t('The referenced media source is missing and needs to be re-embedded.'),
];
}
/**
* {@inheritdoc}
*/
public function process($text, $langcode) {
$result = new FilterProcessResult($text);
if (stristr($text, '<drupal-media') === FALSE) {
return $result;
}
$dom = Html::load($text);
$xpath = new \DOMXPath($dom);
foreach ($xpath->query('//drupal-media[@data-entity-type="media" and normalize-space(@data-entity-uuid)!=""]') as $node) {
/** @var \DOMElement $node */
$uuid = $node->getAttribute('data-entity-uuid');
$view_mode_id = $node->getAttribute('data-view-mode') ?: $this->settings['default_view_mode'];
// Delete the consumed attributes.
$node->removeAttribute('data-entity-type');
$node->removeAttribute('data-entity-uuid');
$node->removeAttribute('data-view-mode');
$media = $this->entityRepository->loadEntityByUuid('media', $uuid);
assert($media === NULL || $media instanceof MediaInterface);
if (!$media) {
$this->loggerFactory->get('media')->error('During rendering of embedded media: the media item with UUID "@uuid" does not exist.', ['@uuid' => $uuid]);
}
else {
$media = $this->entityRepository->getTranslationFromContext($media, $langcode);
$media = clone $media;
$this->applyPerEmbedMediaOverrides($node, $media);
}
$view_mode = NULL;
if ($view_mode_id !== EntityDisplayRepositoryInterface::DEFAULT_DISPLAY_MODE) {
$view_mode = $this->entityRepository->loadEntityByConfigTarget('entity_view_mode', "media.$view_mode_id");
if (!$view_mode) {
$this->loggerFactory->get('media')->error('During rendering of embedded media: the view mode "@view-mode-id" does not exist.', ['@view-mode-id' => $view_mode_id]);
}
}
$build = $media && ($view_mode || $view_mode_id === EntityDisplayRepositoryInterface::DEFAULT_DISPLAY_MODE)
? $this->renderMedia($media, $view_mode_id, $langcode)
: $this->renderMissingMediaIndicator();
if (empty($build['#attributes']['class'])) {
$build['#attributes']['class'] = [];
}
// Any attributes not consumed by the filter should be carried over to the
// rendered embedded entity. For example, `data-align` and `data-caption`
// should be carried over, so that even when embedded media goes missing,
// at least the caption and visual structure won't get lost.
foreach ($node->attributes as $attribute) {
if ($attribute->nodeName == 'class') {
// We don't want to overwrite the existing CSS class of the embedded
// media (or if the media entity can't be loaded, the missing media
// indicator). But, we need to merge in CSS classes added by other
// filters, such as filter_align, in order for those filters to work
// properly.
$build['#attributes']['class'] = array_unique(array_merge($build['#attributes']['class'], explode(' ', $attribute->nodeValue)));
}
else {
$build['#attributes'][$attribute->nodeName] = $attribute->nodeValue;
}
}
$this->renderIntoDomNode($build, $node, $result);
}
$result->setProcessedText(Html::serialize($dom));
return $result;
}
/**
* {@inheritdoc}
*/
public function tips($long = FALSE) {
if ($long) {
return $this->t('
<p>You can embed media items:</p>
<ul>
<li>Choose which media item to embed: <code>&lt;drupal-media data-entity-uuid="07bf3a2e-1941-4a44-9b02-2d1d7a41ec0e" /&gt;</code></li>
<li>Optionally also choose a view mode: <code>data-view-mode="tiny_embed"</code>, otherwise the default view mode is used.</li>
<li>The <code>data-entity-type="media"</code> attribute is required for consistency.</li>
</ul>');
}
else {
return $this->t('You can embed media items (using the <code>&lt;drupal-media&gt;</code> tag).');
}
}
/**
* Renders the given render array into the given DOM node.
*
* @param array $build
* The render array to render in isolation.
* @param \DOMNode $node
* The DOM node to render into.
* @param \Drupal\filter\FilterProcessResult $result
* The accumulated result of filter processing, updated with the metadata
* bubbled during rendering.
*/
protected function renderIntoDomNode(array $build, \DOMNode $node, FilterProcessResult &$result) {
// We need to render the embedded entity:
// - without replacing placeholders, so that the placeholders are
// only replaced at the last possible moment. Hence we cannot use
// either renderInIsolation() or renderRoot(), so we must use render().
// - without bubbling beyond this filter, because filters must
// ensure that the bubbleable metadata for the changes they make
// when filtering text makes it onto the FilterProcessResult
// object that they return ($result). To prevent that bubbling, we
// must wrap the call to render() in a render context.
$markup = $this->renderer->executeInRenderContext(new RenderContext(), function () use (&$build) {
return $this->renderer->render($build);
});
$result = $result->merge(BubbleableMetadata::createFromRenderArray($build));
static::replaceNodeContent($node, $markup);
}
/**
* Replaces the contents of a DOMNode.
*
* @param \DOMNode $node
* A DOMNode object.
* @param string $content
* The text or HTML that will replace the contents of $node.
*/
protected static function replaceNodeContent(\DOMNode &$node, $content) {
if (strlen($content)) {
// Load the content into a new DOMDocument and retrieve the DOM nodes.
$replacement_nodes = Html::load($content)->getElementsByTagName('body')
->item(0)
->childNodes;
}
else {
$replacement_nodes = [$node->ownerDocument->createTextNode('')];
}
foreach ($replacement_nodes as $replacement_node) {
// Import the replacement node from the new DOMDocument into the original
// one, importing also the child nodes of the replacement node.
$replacement_node = $node->ownerDocument->importNode($replacement_node, TRUE);
$node->parentNode->insertBefore($replacement_node, $node);
}
$node->parentNode->removeChild($node);
}
/**
* Disables Contextual Links for the embedded media by removing its property.
*
* @param array $build
* The render array for the embedded media.
*
* @return array
* The updated render array.
*
* @see \Drupal\Core\Entity\EntityViewBuilder::addContextualLinks()
*/
public static function disableContextualLinks(array $build) {
unset($build['#contextual_links']);
return $build;
}
/**
* Applies attribute-based per-media embed overrides of media information.
*
* Currently, this only supports overriding an image media source's `alt` and
* `title`. Support for more overrides may be added in the future.
*
* @param \DOMElement $node
* The HTML tag whose attributes may contain overrides, and if such
* attributes are applied, they will be considered consumed and will
* therefore be removed from the HTML.
* @param \Drupal\media\MediaInterface $media
* The media entity to apply attribute-based overrides to, if any.
*
* @see \Drupal\media\Plugin\media\Source\Image
*/
protected function applyPerEmbedMediaOverrides(\DOMElement $node, MediaInterface $media) {
if ($image_field = $this->getMediaImageSourceField($media)) {
$settings = $media->{$image_field}->getItemDefinition()->getSettings();
if (!empty($settings['alt_field']) && $node->hasAttribute('alt')) {
// Allow the display of the image without an alt tag in special cases.
// Since setting the value in the EditorMediaDialog to an empty string
// restores the default value, this allows special cases where the alt
// text should not be set to the default value, but should be
// explicitly empty instead so it can be ignored by assistive
// technologies, such as screen readers.
if ($node->getAttribute('alt') === '""') {
$node->setAttribute('alt', '');
}
$media->{$image_field}->alt = $node->getAttribute('alt');
// All media entities have a thumbnail. In the case of image media, it
// is conceivable that a particular view mode chooses to display the
// thumbnail instead of the image field itself since the thumbnail
// simply shows a smaller version of the actual media. So we must update
// its `alt` too. Because its `alt` already is inherited from the image
// field's `alt` at entity save time.
// @see \Drupal\media\Plugin\media\Source\Image::getMetadata()
$media->thumbnail->alt = $node->getAttribute('alt');
// Delete the consumed attribute.
$node->removeAttribute('alt');
}
if (!empty($settings['title_field']) && $node->hasAttribute('title')) {
// See above, the explanations for `alt` also apply to `title`.
$media->{$image_field}->title = $node->getAttribute('title');
$media->thumbnail->title = $node->getAttribute('title');
// Delete the consumed attribute.
$node->removeAttribute('title');
}
}
}
/**
* Get image field from source config.
*
* @param \Drupal\media\MediaInterface $media
* A media entity.
*
* @return string|null
* String of image field name.
*/
protected function getMediaImageSourceField(MediaInterface $media) {
$field_definition = $media->getSource()
->getSourceFieldDefinition($media->bundle->entity);
$item_class = $field_definition->getItemDefinition()->getClass();
if ($item_class == ImageItem::class || is_subclass_of($item_class, ImageItem::class)) {
return $field_definition->getName();
}
return NULL;
}
/**
* {@inheritdoc}
*/
public static function trustedCallbacks() {
return ['disableContextualLinks'];
}
/**
* {@inheritdoc}
*/
public function calculateDependencies() {
$dependencies = [];
// Combine the view modes from both config parameters.
$view_modes = $this->settings['allowed_view_modes'] + [$this->settings['default_view_mode']];
$view_modes = array_unique(array_values($view_modes));
$dependencies += ['config' => []];
$storage = $this->entityTypeManager->getStorage('entity_view_mode');
foreach ($view_modes as $view_mode) {
if ($entity_view_mode = $storage->load('media.' . $view_mode)) {
$dependencies[$entity_view_mode->getConfigDependencyKey()][] = $entity_view_mode->getConfigDependencyName();
}
}
return $dependencies;
}
}

View File

@@ -0,0 +1,69 @@
<?php
namespace Drupal\media\Plugin\QueueWorker;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Queue\Attribute\QueueWorker;
use Drupal\Core\Queue\QueueWorkerBase;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Process a queue of media items to fetch their thumbnails.
*/
#[QueueWorker(
id: 'media_entity_thumbnail',
title: new TranslatableMarkup('Thumbnail downloader'),
cron: ['time' => 60]
)]
class ThumbnailDownloader extends QueueWorkerBase implements ContainerFactoryPluginInterface {
/**
* The entity type manager service.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Constructs a new class instance.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* Entity type manager service.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('entity_type.manager')
);
}
/**
* {@inheritdoc}
*/
public function processItem($data) {
/** @var \Drupal\media\Entity\Media $media */
if ($media = $this->entityTypeManager->getStorage('media')->load($data['id'])) {
$media->updateQueuedThumbnail();
$media->save();
}
}
}

View File

@@ -0,0 +1,27 @@
<?php
namespace Drupal\media\Plugin\Validation\Constraint;
use Symfony\Component\Validator\Constraint;
/**
* Validates media mappings.
*
* @internal
*
* @Constraint(
* id = "MediaMappingsConstraint",
* label = @Translation("Media Mapping Constraint", context = "Validation"),
* type = {"string"}
* )
*/
class MediaMappingsConstraint extends Constraint {
/**
* The error message if source is used in media mapping.
*
* @var string
*/
public string $invalidMappingMessage = 'It is not possible to map the source field @source_field_name of a media type.';
}

View File

@@ -0,0 +1,47 @@
<?php
namespace Drupal\media\Plugin\Validation\Constraint;
use Drupal\Component\Plugin\Exception\PluginException;
use Drupal\media\MediaTypeInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
/**
* Validates media mappings.
*/
class MediaMappingsConstraintValidator extends ConstraintValidator {
/**
* {@inheritdoc}
*/
public function validate($value, Constraint $constraint): void {
if (!$constraint instanceof MediaMappingsConstraint) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__ . '\MediaMappingsConstraint');
}
if (!$value instanceof MediaTypeInterface) {
throw new UnexpectedTypeException($value, MediaTypeInterface::class);
}
// The source field cannot be the target of a field mapping because that
// would cause it to be overwritten, possibly with invalid data. This is
// also enforced in the UI.
if (is_array($value->getFieldMap())) {
try {
$source_field_name = $value->getSource()
->getSourceFieldDefinition($value)
?->getName();
if (in_array($source_field_name, $value->getFieldMap(), TRUE)) {
$this->context->addViolation($constraint->invalidMappingMessage, [
'@source_field_name' => $source_field_name,
]);
}
}
catch (PluginException) {
// The source references an invalid plugin.
}
}
}
}

View File

@@ -0,0 +1,51 @@
<?php
namespace Drupal\media\Plugin\Validation\Constraint;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Validation\Attribute\Constraint;
use Symfony\Component\Validator\Constraint as SymfonyConstraint;
/**
* Checks if a value represents a valid oEmbed resource URL.
*
* @internal
* This is an internal part of the oEmbed system and should only be used by
* oEmbed-related code in Drupal core.
*/
#[Constraint(
id: 'oembed_resource',
label: new TranslatableMarkup('oEmbed resource', [], ['context' => 'Validation']),
type: ['link', 'string', 'string_long']
)]
class OEmbedResourceConstraint extends SymfonyConstraint {
/**
* The error message if the URL does not match any known provider.
*
* @var string
*/
public $unknownProviderMessage = 'The given URL does not match any known oEmbed providers.';
/**
* The error message if the URL matches a disallowed provider.
*
* @var string
*/
public $disallowedProviderMessage = 'Sorry, the @name provider is not allowed.';
/**
* The error message if the URL is not a valid oEmbed resource.
*
* @var string
*/
public $invalidResourceMessage = 'The provided URL does not represent a valid oEmbed resource.';
/**
* The error message if an unexpected behavior occurs.
*
* @var string
*/
public $providerErrorMessage = 'An error occurred while trying to retrieve the oEmbed provider database.';
}

View File

@@ -0,0 +1,148 @@
<?php
namespace Drupal\media\Plugin\Validation\Constraint;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\media\OEmbed\ProviderException;
use Drupal\media\OEmbed\ResourceException;
use Drupal\media\OEmbed\ResourceFetcherInterface;
use Drupal\media\OEmbed\UrlResolverInterface;
use Drupal\media\Plugin\media\Source\OEmbedInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
/**
* Validates oEmbed resource URLs.
*
* @internal
* This is an internal part of the oEmbed system and should only be used by
* oEmbed-related code in Drupal core.
*/
class OEmbedResourceConstraintValidator extends ConstraintValidator implements ContainerInjectionInterface {
/**
* The oEmbed URL resolver service.
*
* @var \Drupal\media\OEmbed\UrlResolverInterface
*/
protected $urlResolver;
/**
* The resource fetcher service.
*
* @var \Drupal\media\OEmbed\ResourceFetcherInterface
*/
protected $resourceFetcher;
/**
* The logger service.
*
* @var \Drupal\Core\Logger\LoggerChannelInterface
*/
protected $logger;
/**
* Constructs a new OEmbedResourceConstraintValidator.
*
* @param \Drupal\media\OEmbed\UrlResolverInterface $url_resolver
* The oEmbed URL resolver service.
* @param \Drupal\media\OEmbed\ResourceFetcherInterface $resource_fetcher
* The resource fetcher service.
* @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_factory
* The logger service.
*/
public function __construct(UrlResolverInterface $url_resolver, ResourceFetcherInterface $resource_fetcher, LoggerChannelFactoryInterface $logger_factory) {
$this->urlResolver = $url_resolver;
$this->resourceFetcher = $resource_fetcher;
$this->logger = $logger_factory->get('media');
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('media.oembed.url_resolver'),
$container->get('media.oembed.resource_fetcher'),
$container->get('logger.factory')
);
}
/**
* {@inheritdoc}
*/
public function validate($value, Constraint $constraint) {
/** @var \Drupal\media\MediaInterface $media */
$media = $value->getEntity();
/** @var \Drupal\media\Plugin\media\Source\OEmbedInterface $source */
$source = $media->getSource();
if (!($source instanceof OEmbedInterface)) {
throw new \LogicException('Media source must implement ' . OEmbedInterface::class);
}
$url = $source->getSourceFieldValue($media);
// The URL may be NULL if the source field is empty, which is invalid input.
if (empty($url)) {
$this->context->addViolation($constraint->invalidResourceMessage);
return;
}
// Ensure that the URL matches a provider.
try {
$provider = $this->urlResolver->getProviderByUrl($url);
}
catch (ResourceException $e) {
$this->handleException($e, $constraint->unknownProviderMessage);
return;
}
catch (ProviderException $e) {
$this->handleException($e, $constraint->providerErrorMessage);
return;
}
// Ensure that the provider is allowed.
if (!in_array($provider->getName(), $source->getProviders(), TRUE)) {
$this->context->addViolation($constraint->disallowedProviderMessage, [
'@name' => $provider->getName(),
]);
return;
}
// Verify that resource fetching works, because some URLs might match
// the schemes but don't support oEmbed.
try {
$resource_url = $this->urlResolver->getResourceUrl($url);
$this->resourceFetcher->fetchResource($resource_url);
}
catch (ResourceException $e) {
$this->handleException($e, $constraint->invalidResourceMessage);
}
}
/**
* Handles exceptions that occur during validation.
*
* @param \Exception $e
* The caught exception.
* @param string $error_message
* (optional) The error message to set as a constraint violation.
*/
protected function handleException(\Exception $e, $error_message = NULL) {
if ($error_message) {
$this->context->addViolation($error_message);
}
// The oEmbed system makes heavy use of exception wrapping, so log the
// entire exception chain to help with troubleshooting.
do {
// @todo If $e is a ProviderException or ResourceException, log additional
// debugging information contained in those exceptions in
// https://www.drupal.org/project/drupal/issues/2972846.
$this->logger->error($e->getMessage());
$e = $e->getPrevious();
} while ($e);
}
}

View File

@@ -0,0 +1,41 @@
<?php
namespace Drupal\media\Plugin\media\Source;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\media\Attribute\MediaSource;
use Drupal\media\MediaTypeInterface;
/**
* Media source wrapping around an audio file.
*
* @see \Drupal\file\FileInterface
*/
#[MediaSource(
id: "audio_file",
label: new TranslatableMarkup("Audio file"),
description: new TranslatableMarkup("Use audio files for reusable media."),
allowed_field_types: ["file"],
default_thumbnail_filename: "audio.png"
)]
class AudioFile extends File {
/**
* {@inheritdoc}
*/
public function createSourceField(MediaTypeInterface $type) {
return parent::createSourceField($type)->set('settings', ['file_extensions' => 'mp3 wav aac']);
}
/**
* {@inheritdoc}
*/
public function prepareViewDisplay(MediaTypeInterface $type, EntityViewDisplayInterface $display) {
$display->setComponent($this->getSourceFieldDefinition($type)->getName(), [
'type' => 'file_audio',
'label' => 'visually_hidden',
]);
}
}

View File

@@ -0,0 +1,127 @@
<?php
namespace Drupal\media\Plugin\media\Source;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\file\FileInterface;
use Drupal\media\Attribute\MediaSource;
use Drupal\media\MediaInterface;
use Drupal\media\MediaTypeInterface;
use Drupal\media\MediaSourceBase;
/**
* File entity media source.
*
* @see \Drupal\file\FileInterface
*/
#[MediaSource(
id: "file",
label: new TranslatableMarkup("File"),
description: new TranslatableMarkup("Use local files for reusable media."),
allowed_field_types: ["file"],
)]
class File extends MediaSourceBase {
/**
* Key for "Name" metadata attribute.
*
* @var string
*/
const METADATA_ATTRIBUTE_NAME = 'name';
/**
* Key for "MIME type" metadata attribute.
*
* @var string
*/
const METADATA_ATTRIBUTE_MIME = 'mimetype';
/**
* Key for "File size" metadata attribute.
*
* @var string
*/
const METADATA_ATTRIBUTE_SIZE = 'filesize';
/**
* {@inheritdoc}
*/
public function getMetadataAttributes() {
return [
static::METADATA_ATTRIBUTE_NAME => $this->t('Name'),
static::METADATA_ATTRIBUTE_MIME => $this->t('MIME type'),
static::METADATA_ATTRIBUTE_SIZE => $this->t('File size'),
];
}
/**
* {@inheritdoc}
*/
public function getMetadata(MediaInterface $media, $attribute_name) {
/** @var \Drupal\file\FileInterface $file */
$file = $media->get($this->configuration['source_field'])->entity;
// If the source field is not required, it may be empty.
if (!$file) {
return parent::getMetadata($media, $attribute_name);
}
switch ($attribute_name) {
case static::METADATA_ATTRIBUTE_NAME:
case 'default_name':
return $file->getFilename();
case static::METADATA_ATTRIBUTE_MIME:
return $file->getMimeType();
case static::METADATA_ATTRIBUTE_SIZE:
return $file->getSize();
case 'thumbnail_uri':
return $this->getThumbnail($file) ?: parent::getMetadata($media, $attribute_name);
default:
return parent::getMetadata($media, $attribute_name);
}
}
/**
* Gets the thumbnail image URI based on a file entity.
*
* @param \Drupal\file\FileInterface $file
* A file entity.
*
* @return string
* File URI of the thumbnail image or NULL if there is no specific icon.
*/
protected function getThumbnail(FileInterface $file) {
$icon_base = $this->configFactory->get('media.settings')->get('icon_base_uri');
// We try to automatically use the most specific icon present in the
// $icon_base directory, based on the MIME type. For instance, if an
// icon file named "pdf.png" is present, it will be used if the file
// matches this MIME type.
$mimetype = $file->getMimeType();
$mimetype = explode('/', $mimetype);
$icon_names = [
$mimetype[0] . '--' . $mimetype[1],
$mimetype[1],
$mimetype[0],
];
foreach ($icon_names as $icon_name) {
$thumbnail = $icon_base . '/' . $icon_name . '.png';
if (is_file($thumbnail)) {
return $thumbnail;
}
}
return NULL;
}
/**
* {@inheritdoc}
*/
public function createSourceField(MediaTypeInterface $type) {
return parent::createSourceField($type)->set('settings', ['file_extensions' => 'txt doc docx pdf']);
}
}

View File

@@ -0,0 +1,187 @@
<?php
namespace Drupal\media\Plugin\media\Source;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\FieldTypePluginManagerInterface;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Image\ImageFactory;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\media\Attribute\MediaSource;
use Drupal\media\MediaInterface;
use Drupal\media\MediaTypeInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Image entity media source.
*
* @see \Drupal\Core\Image\ImageInterface
*/
#[MediaSource(
id: "image",
label: new TranslatableMarkup("Image"),
description: new TranslatableMarkup("Use local images for reusable media."),
allowed_field_types: ["image"],
default_thumbnail_filename: "no-thumbnail.png",
thumbnail_alt_metadata_attribute: "thumbnail_alt_value"
)]
class Image extends File {
/**
* Key for "image width" metadata attribute.
*
* @var string
*/
const METADATA_ATTRIBUTE_WIDTH = 'width';
/**
* Key for "image height" metadata attribute.
*
* @var string
*/
const METADATA_ATTRIBUTE_HEIGHT = 'height';
/**
* The image factory service.
*
* @var \Drupal\Core\Image\ImageFactory
*/
protected $imageFactory;
/**
* The file system service.
*
* @var \Drupal\Core\File\FileSystemInterface
*/
protected $fileSystem;
/**
* Constructs a new class instance.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* Entity type manager service.
* @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
* Entity field manager service.
* @param \Drupal\Core\Field\FieldTypePluginManagerInterface $field_type_manager
* The field type plugin manager service.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory service.
* @param \Drupal\Core\Image\ImageFactory $image_factory
* The image factory.
* @param \Drupal\Core\File\FileSystemInterface $file_system
* The file system service.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, FieldTypePluginManagerInterface $field_type_manager, ConfigFactoryInterface $config_factory, ImageFactory $image_factory, FileSystemInterface $file_system) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager, $entity_field_manager, $field_type_manager, $config_factory);
$this->imageFactory = $image_factory;
$this->fileSystem = $file_system;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('entity_type.manager'),
$container->get('entity_field.manager'),
$container->get('plugin.manager.field.field_type'),
$container->get('config.factory'),
$container->get('image.factory'),
$container->get('file_system')
);
}
/**
* {@inheritdoc}
*/
public function getMetadataAttributes() {
$attributes = parent::getMetadataAttributes();
$attributes += [
static::METADATA_ATTRIBUTE_WIDTH => $this->t('Width'),
static::METADATA_ATTRIBUTE_HEIGHT => $this->t('Height'),
];
return $attributes;
}
/**
* {@inheritdoc}
*/
public function getMetadata(MediaInterface $media, $name) {
// Get the file and image data.
/** @var \Drupal\file\FileInterface $file */
$file = $media->get($this->configuration['source_field'])->entity;
// If the source field is not required, it may be empty.
if (!$file) {
return parent::getMetadata($media, $name);
}
$uri = $file->getFileUri();
switch ($name) {
case static::METADATA_ATTRIBUTE_WIDTH:
$image = $this->imageFactory->get($uri);
return $image->getWidth() ?: NULL;
case static::METADATA_ATTRIBUTE_HEIGHT:
$image = $this->imageFactory->get($uri);
return $image->getHeight() ?: NULL;
case 'thumbnail_uri':
return $uri;
case 'thumbnail_alt_value':
return $media->get($this->configuration['source_field'])->alt ?: parent::getMetadata($media, $name);
}
return parent::getMetadata($media, $name);
}
/**
* {@inheritdoc}
*/
public function createSourceField(MediaTypeInterface $type) {
/** @var \Drupal\field\FieldConfigInterface $field */
$field = parent::createSourceField($type);
// Reset the field to its default settings so that we don't inherit the
// settings from the parent class' source field.
$settings = $this->fieldTypeManager->getDefaultFieldSettings($field->getType());
return $field->set('settings', $settings);
}
/**
* {@inheritdoc}
*/
public function prepareViewDisplay(MediaTypeInterface $type, EntityViewDisplayInterface $display) {
parent::prepareViewDisplay($type, $display);
// Use the `large` image style and do not link the image to anything.
// This will prevent the out-of-the-box configuration from outputting very
// large raw images. If the `large` image style has been deleted, do not
// set an image style.
$field_name = $this->getSourceFieldDefinition($type)->getName();
$component = $display->getComponent($field_name);
$component['settings']['image_link'] = '';
$component['settings']['image_style'] = '';
if ($this->entityTypeManager->getStorage('image_style')->load('large')) {
$component['settings']['image_style'] = 'large';
}
$display->setComponent($field_name, $component);
}
}

View File

@@ -0,0 +1,562 @@
<?php
namespace Drupal\media\Plugin\media\Source;
use Drupal\Component\Render\PlainTextOutput;
use Drupal\Component\Utility\Crypt;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\Display\EntityFormDisplayInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\FieldTypePluginManagerInterface;
use Drupal\Core\File\Exception\FileException;
use Drupal\Core\File\FileExists;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Url;
use Drupal\Core\Utility\Token;
use Drupal\media\Attribute\OEmbedMediaSource;
use Drupal\media\IFrameUrlHelper;
use Drupal\media\MediaInterface;
use Drupal\media\MediaSourceBase;
use Drupal\media\MediaTypeInterface;
use Drupal\media\OEmbed\Resource;
use Drupal\media\OEmbed\ResourceException;
use Drupal\media\OEmbed\ResourceFetcherInterface;
use Drupal\media\OEmbed\UrlResolverInterface;
use GuzzleHttp\ClientInterface;
use Psr\Http\Client\ClientExceptionInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Mime\MimeTypes;
/**
* Provides a media source plugin for oEmbed resources.
*
* For security reasons, the oEmbed source (and, therefore, anything that
* extends it) obeys a hard-coded list of allowed third-party oEmbed providers
* set in its plugin definition's providers array. This array is a set of
* provider names, exactly as they appear in the canonical oEmbed provider
* database at https://oembed.com/providers.json.
*
* You can implement support for additional providers by defining a new plugin
* that uses this class. This can be done in hook_media_source_info_alter().
* For example:
* @code
* <?php
*
* function example_media_source_info_alter(array &$sources) {
* $sources['artwork'] = [
* 'id' => 'artwork',
* 'label' => $this->t('Artwork'),
* 'description' => $this->t('Use artwork from Flickr and DeviantArt.'),
* 'allowed_field_types' => ['string'],
* 'default_thumbnail_filename' => 'no-thumbnail.png',
* 'providers' => ['Deviantart.com', 'Flickr'],
* 'class' => 'Drupal\media\Plugin\media\Source\OEmbed',
* ];
* }
* @endcode
* The "Deviantart.com" and "Flickr" provider names are specified in
* https://oembed.com/providers.json. The
* \Drupal\media\Plugin\media\Source\OEmbed class already knows how to handle
* standard interactions with third-party oEmbed APIs, so there is no need to
* define a new class which extends it. With the code above, you will able to
* create media types which use the "Artwork" source plugin, and use those media
* types to link to assets on Deviantart and Flickr.
*/
#[OEmbedMediaSource(
id: "oembed",
label: new TranslatableMarkup("oEmbed source"),
description: new TranslatableMarkup("Use oEmbed URL for reusable media."),
allowed_field_types: ["string"],
default_thumbnail_filename: "no-thumbnail.png",
deriver: OEmbedDeriver::class,
)]
class OEmbed extends MediaSourceBase implements OEmbedInterface {
/**
* The logger channel for media.
*
* @var \Psr\Log\LoggerInterface
*/
protected $logger;
/**
* The messenger service.
*
* @var \Drupal\Core\Messenger\MessengerInterface
*/
protected $messenger;
/**
* The HTTP client.
*
* @var \GuzzleHttp\Client
*/
protected $httpClient;
/**
* The oEmbed resource fetcher service.
*
* @var \Drupal\media\OEmbed\ResourceFetcherInterface
*/
protected $resourceFetcher;
/**
* The OEmbed manager service.
*
* @var \Drupal\media\OEmbed\UrlResolverInterface
*/
protected $urlResolver;
/**
* The iFrame URL helper service.
*
* @var \Drupal\media\IFrameUrlHelper
*/
protected $iFrameUrlHelper;
/**
* The file system.
*
* @var \Drupal\Core\File\FileSystemInterface
*/
protected $fileSystem;
/**
* The token replacement service.
*
* @var \Drupal\Core\Utility\Token
*/
protected $token;
/**
* Constructs a new OEmbed instance.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager service.
* @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
* The entity field manager service.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory service.
* @param \Drupal\Core\Field\FieldTypePluginManagerInterface $field_type_manager
* The field type plugin manager service.
* @param \Psr\Log\LoggerInterface $logger
* The logger channel for media.
* @param \Drupal\Core\Messenger\MessengerInterface $messenger
* The messenger service.
* @param \GuzzleHttp\ClientInterface $http_client
* The HTTP client.
* @param \Drupal\media\OEmbed\ResourceFetcherInterface $resource_fetcher
* The oEmbed resource fetcher service.
* @param \Drupal\media\OEmbed\UrlResolverInterface $url_resolver
* The oEmbed URL resolver service.
* @param \Drupal\media\IFrameUrlHelper $iframe_url_helper
* The iFrame URL helper service.
* @param \Drupal\Core\File\FileSystemInterface $file_system
* The file system.
* @param \Drupal\Core\Utility\Token $token
* The token replacement service.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, ConfigFactoryInterface $config_factory, FieldTypePluginManagerInterface $field_type_manager, LoggerInterface $logger, MessengerInterface $messenger, ClientInterface $http_client, ResourceFetcherInterface $resource_fetcher, UrlResolverInterface $url_resolver, IFrameUrlHelper $iframe_url_helper, FileSystemInterface $file_system, Token $token) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager, $entity_field_manager, $field_type_manager, $config_factory);
$this->logger = $logger;
$this->messenger = $messenger;
$this->httpClient = $http_client;
$this->resourceFetcher = $resource_fetcher;
$this->urlResolver = $url_resolver;
$this->iFrameUrlHelper = $iframe_url_helper;
$this->fileSystem = $file_system;
$this->token = $token;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('entity_type.manager'),
$container->get('entity_field.manager'),
$container->get('config.factory'),
$container->get('plugin.manager.field.field_type'),
$container->get('logger.factory')->get('media'),
$container->get('messenger'),
$container->get('http_client'),
$container->get('media.oembed.resource_fetcher'),
$container->get('media.oembed.url_resolver'),
$container->get('media.oembed.iframe_url_helper'),
$container->get('file_system'),
$container->get('token')
);
}
/**
* {@inheritdoc}
*/
public function getMetadataAttributes() {
return [
'type' => $this->t('Resource type'),
'title' => $this->t('Resource title'),
'author_name' => $this->t('Author/owner name'),
'author_url' => $this->t('Author/owner URL'),
'provider_name' => $this->t('Provider name'),
'provider_url' => $this->t('Provider URL'),
'cache_age' => $this->t('Suggested cache lifetime'),
'default_name' => $this->t('Media item default name'),
'thumbnail_uri' => $this->t('Thumbnail local URI'),
'thumbnail_width' => $this->t('Thumbnail width'),
'thumbnail_height' => $this->t('Thumbnail height'),
'url' => $this->t('Resource source URL'),
'width' => $this->t('Resource width'),
'height' => $this->t('Resource height'),
'html' => $this->t('Resource HTML representation'),
];
}
/**
* {@inheritdoc}
*/
public function getMetadata(MediaInterface $media, $name) {
$media_url = $this->getSourceFieldValue($media);
// The URL may be NULL if the source field is empty, in which case just
// return NULL.
if (empty($media_url)) {
return NULL;
}
try {
$resource_url = $this->urlResolver->getResourceUrl($media_url);
$resource = $this->resourceFetcher->fetchResource($resource_url);
}
catch (ResourceException $e) {
$this->messenger->addError($e->getMessage());
return NULL;
}
switch ($name) {
case 'default_name':
if ($title = $this->getMetadata($media, 'title')) {
return $title;
}
elseif ($url = $this->getMetadata($media, 'url')) {
return $url;
}
return parent::getMetadata($media, 'default_name');
case 'thumbnail_uri':
return $this->getLocalThumbnailUri($resource, $media) ?: parent::getMetadata($media, 'thumbnail_uri');
case 'type':
return $resource->getType();
case 'title':
return $resource->getTitle();
case 'author_name':
return $resource->getAuthorName();
case 'author_url':
return $resource->getAuthorUrl();
case 'provider_name':
$provider = $resource->getProvider();
return $provider ? $provider->getName() : '';
case 'provider_url':
$provider = $resource->getProvider();
return $provider ? $provider->getUrl() : NULL;
case 'cache_age':
return $resource->getCacheMaxAge();
case 'thumbnail_width':
return $resource->getThumbnailWidth();
case 'thumbnail_height':
return $resource->getThumbnailHeight();
case 'url':
$url = $resource->getUrl();
return $url ? $url->toString() : NULL;
case 'width':
return $resource->getWidth();
case 'height':
return $resource->getHeight();
case 'html':
return $resource->getHtml();
default:
break;
}
return NULL;
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$domain = $this->configFactory->get('media.settings')->get('iframe_domain');
if (!$this->iFrameUrlHelper->isSecure($domain)) {
array_unshift($form, [
'#markup' => '<p>' . $this->t('It is potentially insecure to display oEmbed content in a frame that is served from the same domain as your main Drupal site, as this may allow execution of third-party code. <a href=":url">You can specify a different domain for serving oEmbed content in the Media settings</a>.', [
':url' => Url::fromRoute('media.settings')->setAbsolute()->toString(),
]) . '</p>',
]);
}
$form['thumbnails_directory'] = [
'#type' => 'textfield',
'#title' => $this->t('Thumbnails location'),
'#default_value' => $this->configuration['thumbnails_directory'],
'#description' => $this->t('Thumbnails will be fetched from the provider for local usage. This is the URI of the directory where they will be placed.'),
'#required' => TRUE,
];
$configuration = $this->getConfiguration();
$plugin_definition = $this->getPluginDefinition();
$form['providers'] = [
'#type' => 'checkboxes',
'#title' => $this->t('Allowed providers'),
'#default_value' => $configuration['providers'],
'#options' => array_combine($plugin_definition['providers'], $plugin_definition['providers']),
'#description' => $this->t('Optionally select the allowed oEmbed providers for this media type. If left blank, all providers will be allowed.'),
];
return $form;
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::submitConfigurationForm($form, $form_state);
$configuration = $this->getConfiguration();
$configuration['providers'] = array_filter(array_values($configuration['providers']));
$this->setConfiguration($configuration);
}
/**
* {@inheritdoc}
*/
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
$thumbnails_directory = $form_state->getValue('thumbnails_directory');
/** @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $stream_wrapper_manager */
$stream_wrapper_manager = \Drupal::service('stream_wrapper_manager');
if (!$stream_wrapper_manager->isValidUri($thumbnails_directory)) {
$form_state->setErrorByName('thumbnails_directory', $this->t('@path is not a valid path.', [
'@path' => $thumbnails_directory,
]));
}
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return parent::defaultConfiguration() + [
'thumbnails_directory' => 'public://oembed_thumbnails/[date:custom:Y-m]',
'providers' => [],
];
}
/**
* Returns the local URI for a resource thumbnail.
*
* If the thumbnail is not already locally stored, this method will attempt
* to download it.
*
* @param \Drupal\media\OEmbed\Resource $resource
* The oEmbed resource.
* @param \Drupal\media\MediaInterface|null $media
* The media entity that contains the resource.
*
* @return string|null
* The local thumbnail URI, or NULL if it could not be downloaded, or if the
* resource has no thumbnail at all.
*
* @todo Determine whether or not oEmbed media thumbnails should be stored
* locally at all, and if so, whether that functionality should be
* toggle-able. See https://www.drupal.org/project/drupal/issues/2962751 for
* more information.
*/
protected function getLocalThumbnailUri(Resource $resource, ?MediaInterface $media = NULL) {
if (is_null($media)) {
@trigger_error('Calling ' . __METHOD__ . '() without the $media argument is deprecated in drupal:10.3.0 and it will be required in drupal:11.0.0. See https://www.drupal.org/node/3432920', E_USER_DEPRECATED);
$token_data = [];
}
else {
$token_data = ['date' => $media->getCreatedTime()];
}
// If there is no remote thumbnail, there's nothing for us to fetch here.
$remote_thumbnail_url = $resource->getThumbnailUrl();
if (!$remote_thumbnail_url) {
return NULL;
}
// Use the configured directory to store thumbnails. The directory can
// contain basic (i.e., global) tokens. If any of the replaced tokens
// contain HTML, the tags will be removed and XML entities will be decoded.
$configuration = $this->getConfiguration();
$directory = $configuration['thumbnails_directory'];
// The thumbnail directory might contain a date token, so we pass in the
// creation date of the media entity so that the token won't rely on the
// current request time, making the current request have a max-age of 0.
// @see system_tokens() for $type == 'date'.
$directory = $this->token->replace($directory, $token_data);
$directory = PlainTextOutput::renderFromHtml($directory);
// The local thumbnail doesn't exist yet, so try to download it. First,
// ensure that the destination directory is writable, and if it's not,
// log an error and bail out.
if (!$this->fileSystem->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS)) {
$this->logger->warning('Could not prepare thumbnail destination directory @dir for oEmbed media.', [
'@dir' => $directory,
]);
return NULL;
}
// The local filename of the thumbnail is always a hash of its remote URL.
// If a file with that name already exists in the thumbnails directory,
// regardless of its extension, return its URI.
$remote_thumbnail_url = $remote_thumbnail_url->toString();
$hash = Crypt::hashBase64($remote_thumbnail_url);
$files = $this->fileSystem->scanDirectory($directory, "/^$hash\..*/");
if (count($files) > 0) {
return reset($files)->uri;
}
// The local thumbnail doesn't exist yet, so we need to download it.
try {
$response = $this->httpClient->request('GET', $remote_thumbnail_url);
if ($response->getStatusCode() === 200) {
$local_thumbnail_uri = $directory . DIRECTORY_SEPARATOR . $hash . '.' . $this->getThumbnailFileExtensionFromUrl($remote_thumbnail_url, $response);
$this->fileSystem->saveData((string) $response->getBody(), $local_thumbnail_uri, FileExists::Replace);
return $local_thumbnail_uri;
}
}
catch (ClientExceptionInterface $e) {
$this->logger->warning('Failed to download remote thumbnail file due to "%error".', [
'%error' => $e->getMessage(),
]);
}
catch (FileException $e) {
$this->logger->warning('Could not download remote thumbnail from {url}.', [
'url' => $remote_thumbnail_url,
]);
}
return NULL;
}
/**
* Tries to determine the file extension of a thumbnail.
*
* @param string $thumbnail_url
* The remote URL of the thumbnail.
* @param \Psr\Http\Message\ResponseInterface $response
* The response for the downloaded thumbnail.
*
* @return string|null
* The file extension, or NULL if it could not be determined.
*/
protected function getThumbnailFileExtensionFromUrl(string $thumbnail_url, ResponseInterface $response): ?string {
// First, try to glean the extension from the URL path.
$path = parse_url($thumbnail_url, PHP_URL_PATH);
if ($path) {
$extension = strtolower(pathinfo($path, PATHINFO_EXTENSION));
if ($extension) {
return $extension;
}
}
// If the URL didn't give us any clues about the file extension, see if the
// response headers will give us a MIME type.
$content_type = $response->getHeader('Content-Type');
// If there was no Content-Type header, there's nothing else we can do.
if (empty($content_type)) {
return NULL;
}
$extensions = MimeTypes::getDefault()->getExtensions(reset($content_type));
if ($extensions) {
return reset($extensions);
}
// If no file extension could be determined from the Content-Type header,
// we're stumped.
return NULL;
}
/**
* {@inheritdoc}
*/
public function getSourceFieldConstraints() {
return [
'oembed_resource' => [],
];
}
/**
* {@inheritdoc}
*/
public function prepareViewDisplay(MediaTypeInterface $type, EntityViewDisplayInterface $display) {
$display->setComponent($this->getSourceFieldDefinition($type)->getName(), [
'type' => 'oembed',
'label' => 'visually_hidden',
]);
}
/**
* {@inheritdoc}
*/
public function prepareFormDisplay(MediaTypeInterface $type, EntityFormDisplayInterface $display) {
parent::prepareFormDisplay($type, $display);
$source_field = $this->getSourceFieldDefinition($type)->getName();
$display->setComponent($source_field, [
'type' => 'oembed_textfield',
'weight' => $display->getComponent($source_field)['weight'],
]);
$display->removeComponent('name');
}
/**
* {@inheritdoc}
*/
public function getProviders() {
$configuration = $this->getConfiguration();
return $configuration['providers'] ?: $this->getPluginDefinition()['providers'];
}
/**
* {@inheritdoc}
*/
public function createSourceField(MediaTypeInterface $type) {
$plugin_definition = $this->getPluginDefinition();
$label = (string) $this->t('@type URL', [
'@type' => $plugin_definition['label'],
]);
return parent::createSourceField($type)->set('label', $label);
}
}

View File

@@ -0,0 +1,35 @@
<?php
namespace Drupal\media\Plugin\media\Source;
use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Core\StringTranslation\StringTranslationTrait;
/**
* Derives media source plugin definitions for supported oEmbed providers.
*
* @internal
* This is an internal part of the oEmbed system and should only be used by
* oEmbed-related code in Drupal core.
*/
class OEmbedDeriver extends DeriverBase {
use StringTranslationTrait;
/**
* {@inheritdoc}
*/
public function getDerivativeDefinitions($base_plugin_definition) {
$this->derivatives = [
'video' => [
'id' => 'video',
'label' => $this->t('Remote video'),
'description' => $this->t('Use remote video URL for reusable media.'),
'providers' => ['YouTube', 'Vimeo'],
'default_thumbnail_filename' => 'video.png',
] + $base_plugin_definition,
];
return parent::getDerivativeDefinitions($base_plugin_definition);
}
}

View File

@@ -0,0 +1,23 @@
<?php
namespace Drupal\media\Plugin\media\Source;
use Drupal\media\MediaSourceFieldConstraintsInterface;
/**
* Defines additional functionality for source plugins that use oEmbed.
*/
interface OEmbedInterface extends MediaSourceFieldConstraintsInterface {
/**
* Returns the oEmbed provider names.
*
* The allowed providers can be configured by the user. If it is not
* configured, all providers supported by the plugin are returned.
*
* @return string[]
* A list of oEmbed provider names.
*/
public function getProviders();
}

View File

@@ -0,0 +1,41 @@
<?php
namespace Drupal\media\Plugin\media\Source;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\media\Attribute\MediaSource;
use Drupal\media\MediaTypeInterface;
/**
* Media source wrapping around a video file.
*
* @see \Drupal\file\FileInterface
*/
#[MediaSource(
id: "video_file",
label: new TranslatableMarkup("Video file"),
description: new TranslatableMarkup("Use video files for reusable media."),
allowed_field_types: ["file"],
default_thumbnail_filename: "video.png"
)]
class VideoFile extends File {
/**
* {@inheritdoc}
*/
public function createSourceField(MediaTypeInterface $type) {
return parent::createSourceField($type)->set('settings', ['file_extensions' => 'mp4']);
}
/**
* {@inheritdoc}
*/
public function prepareViewDisplay(MediaTypeInterface $type, EntityViewDisplayInterface $display) {
$display->setComponent($this->getSourceFieldDefinition($type)->getName(), [
'type' => 'file_video',
'label' => 'visually_hidden',
]);
}
}

View File

@@ -0,0 +1,55 @@
<?php
namespace Drupal\media\Plugin\views\filter;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Attribute\ViewsFilter;
use Drupal\views\Plugin\views\filter\FilterPluginBase;
/**
* Filter by published status.
*
* @ingroup views_filter_handlers
*/
#[ViewsFilter("media_status")]
class Status extends FilterPluginBase {
/**
* {@inheritdoc}
*/
public function adminSummary() {}
/**
* {@inheritdoc}
*/
protected function operatorForm(&$form, FormStateInterface $form_state) {}
/**
* {@inheritdoc}
*/
public function canExpose() {
return FALSE;
}
/**
* {@inheritdoc}
*/
public function query() {
$table = $this->ensureMyTable();
$snippet = "$table.status = 1 OR ($table.uid = ***CURRENT_USER*** AND ***CURRENT_USER*** <> 0 AND ***VIEW_OWN_UNPUBLISHED_MEDIA*** = 1) OR ***ADMINISTER_MEDIA*** = 1";
if ($this->moduleHandler->moduleExists('content_moderation')) {
$snippet .= ' OR ***VIEW_ANY_UNPUBLISHED_NODES*** = 1';
}
$this->query->addWhereExpression($this->options['group'], $snippet);
}
/**
* {@inheritdoc}
*/
public function getCacheContexts() {
$contexts = parent::getCacheContexts();
$contexts[] = 'user';
return $contexts;
}
}

View File

@@ -0,0 +1,72 @@
<?php
namespace Drupal\media\Plugin\views\wizard;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\views\Attribute\ViewsWizard;
use Drupal\views\Plugin\views\wizard\WizardPluginBase;
/**
* Provides Views creation wizard for Media.
*/
#[ViewsWizard(
id: 'media',
base_table: 'media_field_data',
title: new TranslatableMarkup('Media')
)]
class Media extends WizardPluginBase {
/**
* Set the created column.
*
* @var string
*/
protected $createdColumn = 'media_field_data-created';
/**
* {@inheritdoc}
*/
public function getAvailableSorts() {
return [
'media_field_data-name:DESC' => $this->t('Media name'),
];
}
/**
* {@inheritdoc}
*/
protected function defaultDisplayOptions() {
$display_options = parent::defaultDisplayOptions();
// Add permission-based access control.
$display_options['access']['type'] = 'perm';
$display_options['access']['options']['perm'] = 'view media';
// Remove the default fields, since we are customizing them here.
unset($display_options['fields']);
// Add the name field, so that the display has content if the user switches
// to a row style that uses fields.
$display_options['fields']['name']['id'] = 'name';
$display_options['fields']['name']['table'] = 'media_field_data';
$display_options['fields']['name']['field'] = 'name';
$display_options['fields']['name']['entity_type'] = 'media';
$display_options['fields']['name']['entity_field'] = 'media';
$display_options['fields']['name']['label'] = '';
$display_options['fields']['name']['alter']['alter_text'] = 0;
$display_options['fields']['name']['alter']['make_link'] = 0;
$display_options['fields']['name']['alter']['absolute'] = 0;
$display_options['fields']['name']['alter']['trim'] = 0;
$display_options['fields']['name']['alter']['word_boundary'] = 0;
$display_options['fields']['name']['alter']['ellipsis'] = 0;
$display_options['fields']['name']['alter']['strip_tags'] = 0;
$display_options['fields']['name']['alter']['html'] = 0;
$display_options['fields']['name']['hide_empty'] = 0;
$display_options['fields']['name']['empty_zero'] = 0;
$display_options['fields']['name']['settings']['link_to_entity'] = 1;
$display_options['fields']['name']['plugin_id'] = 'field';
return $display_options;
}
}

View File

@@ -0,0 +1,84 @@
<?php
namespace Drupal\media\Plugin\views\wizard;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\views\Attribute\ViewsWizard;
use Drupal\views\Plugin\views\wizard\WizardPluginBase;
/**
* Provides Views creation wizard for Media revisions.
*/
#[ViewsWizard(
id: 'media_revision',
title: new TranslatableMarkup('Media revisions'),
base_table: 'media_field_revision'
)]
class MediaRevision extends WizardPluginBase {
/**
* Set the created column.
*
* @var string
*/
protected $createdColumn = 'media_field_revision-created';
/**
* {@inheritdoc}
*/
protected function defaultDisplayOptions() {
$display_options = parent::defaultDisplayOptions();
// Add permission-based access control.
$display_options['access']['type'] = 'perm';
$display_options['access']['options']['perm'] = 'view all revisions';
// Remove the default fields, since we are customizing them here.
unset($display_options['fields']);
// Add the changed field.
$display_options['fields']['changed']['id'] = 'changed';
$display_options['fields']['changed']['table'] = 'media_field_revision';
$display_options['fields']['changed']['field'] = 'changed';
$display_options['fields']['changed']['entity_type'] = 'media';
$display_options['fields']['changed']['entity_field'] = 'changed';
$display_options['fields']['changed']['alter']['alter_text'] = FALSE;
$display_options['fields']['changed']['alter']['make_link'] = FALSE;
$display_options['fields']['changed']['alter']['absolute'] = FALSE;
$display_options['fields']['changed']['alter']['trim'] = FALSE;
$display_options['fields']['changed']['alter']['word_boundary'] = FALSE;
$display_options['fields']['changed']['alter']['ellipsis'] = FALSE;
$display_options['fields']['changed']['alter']['strip_tags'] = FALSE;
$display_options['fields']['changed']['alter']['html'] = FALSE;
$display_options['fields']['changed']['hide_empty'] = FALSE;
$display_options['fields']['changed']['empty_zero'] = FALSE;
$display_options['fields']['changed']['plugin_id'] = 'field';
$display_options['fields']['changed']['type'] = 'timestamp';
$display_options['fields']['changed']['settings']['date_format'] = 'medium';
$display_options['fields']['changed']['settings']['custom_date_format'] = '';
$display_options['fields']['changed']['settings']['timezone'] = '';
// Add the name field.
$display_options['fields']['name']['id'] = 'name';
$display_options['fields']['name']['table'] = 'media_field_revision';
$display_options['fields']['name']['field'] = 'name';
$display_options['fields']['name']['entity_type'] = 'media';
$display_options['fields']['name']['entity_field'] = 'name';
$display_options['fields']['name']['label'] = '';
$display_options['fields']['name']['alter']['alter_text'] = 0;
$display_options['fields']['name']['alter']['make_link'] = 0;
$display_options['fields']['name']['alter']['absolute'] = 0;
$display_options['fields']['name']['alter']['trim'] = 0;
$display_options['fields']['name']['alter']['word_boundary'] = 0;
$display_options['fields']['name']['alter']['ellipsis'] = 0;
$display_options['fields']['name']['alter']['strip_tags'] = 0;
$display_options['fields']['name']['alter']['html'] = 0;
$display_options['fields']['name']['hide_empty'] = 0;
$display_options['fields']['name']['empty_zero'] = 0;
$display_options['fields']['name']['settings']['link_to_entity'] = 0;
$display_options['fields']['name']['plugin_id'] = 'field';
return $display_options;
}
}

View File

@@ -0,0 +1,55 @@
<?php
namespace Drupal\media\Routing;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\Routing\AdminHtmlRouteProvider;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides HTML routes for media pages.
*/
class MediaRouteProvider extends AdminHtmlRouteProvider {
/**
* The media settings config.
*
* @var \Drupal\Core\Config\ImmutableConfig
*/
protected $config;
/**
* {@inheritdoc}
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, ConfigFactoryInterface $config_factory) {
parent::__construct($entity_type_manager, $entity_field_manager);
$this->config = $config_factory->get('media.settings');
}
/**
* {@inheritdoc}
*/
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static(
$container->get('entity_type.manager'),
$container->get('entity_field.manager'),
$container->get('config.factory')
);
}
/**
* {@inheritdoc}
*/
protected function getCanonicalRoute(EntityTypeInterface $entity_type) {
if ($this->config->get('standalone_url')) {
return parent::getCanonicalRoute($entity_type);
}
else {
return parent::getEditFormRoute($entity_type);
}
}
}