Verified Commit 7f69b30b authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3420997 by sorlov, quietone, DanielVeza, smustgrave, alexpott,...

Issue #3420997 by sorlov, quietone, DanielVeza, smustgrave, alexpott, mstrelan: Convert MediaSource plugin discovery to attributes

(cherry picked from commit 7201d4bf)
parent 26a5b7e6
Loading
Loading
Loading
Loading
Loading
+87 −0
Original line number Diff line number Diff line
<?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
  ) {}

}
+86 −0
Original line number Diff line number Diff line
<?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
  ) {}

}
+2 −2
Original line number Diff line number Diff line
@@ -5,7 +5,7 @@
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\media\Annotation\MediaSource;
use Drupal\media\Attribute\MediaSource;

/**
 * Manages media source plugins.
@@ -24,7 +24,7 @@ class MediaSourceManager extends DefaultPluginManager {
   *   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);
    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');
+9 −8
Original line number Diff line number Diff line
@@ -3,21 +3,22 @@
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 = @Translation("Audio file"),
 *   description = @Translation("Use audio files for reusable media."),
 *   allowed_field_types = {"file"},
 *   default_thumbnail_filename = "audio.png"
 * )
 */
#[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 {

  /**
+8 −8
Original line number Diff line number Diff line
@@ -2,7 +2,9 @@

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;
@@ -11,15 +13,13 @@
 * File entity media source.
 *
 * @see \Drupal\file\FileInterface
 *
 * @MediaSource(
 *   id = "file",
 *   label = @Translation("File"),
 *   description = @Translation("Use local files for reusable media."),
 *   allowed_field_types = {"file"},
 *   default_thumbnail_filename = "generic.png"
 * )
 */
#[MediaSource(
  id: "file",
  label: new TranslatableMarkup("File"),
  description: new TranslatableMarkup("Use local files for reusable media."),
  allowed_field_types: ["file"],
)]
class File extends MediaSourceBase {

  /**
Loading