.../Core/Plugin/PluginWithHandlersInterface.php | 42 +++++++++++++++++ .../Drupal/Core/Plugin/PluginWithHandlersTrait.php | 53 ++++++++++++++++++++++ core/modules/media/src/MediaStorageProxyBase.php | 2 +- .../MediaStorageProxyMetadataHandlerInterface.php | 2 +- core/modules/media/src/MediaTypeForm.php | 4 +- .../src/Plugin/MediaStorageProxy/Test.php | 30 ++++++------ .../MediaStorageProxy/TestMetadataHandler.php | 40 ++++++++++++++++ 7 files changed, 152 insertions(+), 21 deletions(-) diff --git a/core/lib/Drupal/Core/Plugin/PluginWithHandlersInterface.php b/core/lib/Drupal/Core/Plugin/PluginWithHandlersInterface.php new file mode 100644 index 0000000..3564713 --- /dev/null +++ b/core/lib/Drupal/Core/Plugin/PluginWithHandlersInterface.php @@ -0,0 +1,42 @@ + 'classname', +// ]; +// } + + /** + * {@inheritdoc} + */ + public function getHandlerClass($handler_type, $nested = FALSE) { + $handlers_annotation = $this->getPluginDefinition()['handlers']; + if (isset($handlers_annotation[$handler_type]) && (!$nested || isset($handlers_annotation[$handler_type][$nested]))) { + return $nested ? $handlers_annotation[$handler_type][$nested] : $handlers_annotation[$handler_type]; + } + // Allow plugins to opt in to be their own handler of this type. + $interface = $this->getInterfacesForHandlerTypes()[$handler_type]; + if ($this instanceof $interface) { + return static::class; + } + return FALSE; + } + + /** + * {@inheritdoc} + */ + public function hasHandlerClass($handler_type, $nested = FALSE) { + return !empty($this->getHandlerClass($handler_type, $nested)); + } + + /** + * {@inheritdoc} + */ + public function getHandler($handler_type, $nested = FALSE) { + $class = $this->getHandlerClass($handler_type, $nested); + // @todo always inject context (at minimum source field configuration), see EntityHandlerInterface + // @todo allow handlers to use dependency injection from the container, see ContainerInjectionInterface + return new $class(); + } + +} diff --git a/core/modules/media/src/MediaStorageProxyBase.php b/core/modules/media/src/MediaStorageProxyBase.php index 69e90a3..46bb095 100644 --- a/core/modules/media/src/MediaStorageProxyBase.php +++ b/core/modules/media/src/MediaStorageProxyBase.php @@ -15,7 +15,7 @@ /** * Base implementation of media storage proxy plugin. */ -abstract class MediaStorageProxyBase extends PluginBase implements MediaStorageProxyInterface, MediaStorageProxyMetadataHandlerInterface, MediaStorageProxyThumbnailHandlerInterface, ContainerFactoryPluginInterface { +abstract class MediaStorageProxyBase extends PluginBase implements MediaStorageProxyInterface, MediaStorageProxyThumbnailHandlerInterface, ContainerFactoryPluginInterface { /** * Plugin label. diff --git a/core/modules/media/src/MediaStorageProxyMetadataHandlerInterface.php b/core/modules/media/src/MediaStorageProxyMetadataHandlerInterface.php index ba31414..d84c8c8 100644 --- a/core/modules/media/src/MediaStorageProxyMetadataHandlerInterface.php +++ b/core/modules/media/src/MediaStorageProxyMetadataHandlerInterface.php @@ -30,7 +30,7 @@ * * @see \Drupal\media\MediaStorageProxyInterface */ -interface MediaStorageProxyMetadataHandlerInterface extends MediaStorageProxyInterface { +interface MediaStorageProxyMetadataHandlerInterface { /** * Provide a default name for the media item. diff --git a/core/modules/media/src/MediaTypeForm.php b/core/modules/media/src/MediaTypeForm.php index 2b774a6..158391a 100644 --- a/core/modules/media/src/MediaTypeForm.php +++ b/core/modules/media/src/MediaTypeForm.php @@ -151,7 +151,7 @@ public function form(array $form, FormStateInterface $form_state) { ], ]; - if (empty($handler) || empty($handler->getMetadataLabels())) { + if (empty($handler) || empty($handler->getHandler('metadata')->getMetadataLabels())) { $form['handler_dependent']['field_map']['#access'] = FALSE; } else { @@ -163,7 +163,7 @@ public function form(array $form, FormStateInterface $form_state) { } $field_map = $this->entity->getFieldMap(); - foreach ($handler->getMetadataLabels() as $metadata_attribute_name => $metadata_attribute_label) { + foreach ($handler->getHandler('metadata')->getMetadataLabels() as $metadata_attribute_name => $metadata_attribute_label) { $form['handler_dependent']['field_map'][$metadata_attribute_name] = [ '#type' => 'select', '#title' => $metadata_attribute_label, diff --git a/core/modules/media/tests/modules/media_test_handler/src/Plugin/MediaStorageProxy/Test.php b/core/modules/media/tests/modules/media_test_handler/src/Plugin/MediaStorageProxy/Test.php index d06bf95..20ce372 100644 --- a/core/modules/media/tests/modules/media_test_handler/src/Plugin/MediaStorageProxy/Test.php +++ b/core/modules/media/tests/modules/media_test_handler/src/Plugin/MediaStorageProxy/Test.php @@ -3,8 +3,10 @@ namespace Drupal\media_test_handler\Plugin\MediaStorageProxy; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Plugin\PluginWithHandlersTrait; use Drupal\media\MediaInterface; use Drupal\media\MediaStorageProxyBase; +use Drupal\media\MediaStorageProxyMetadataHandlerInterface; use Drupal\media\MediaTypeInterface; /** @@ -15,10 +17,21 @@ * label = @Translation("Test media storage proxy"), * description = @Translation("Media storage proxy for testing purposes."), * allowed_field_types = {"string"}, + * handlers = { + * "metadata" = "Drupal\media_test_handler\Plugin\MediaStorageProxy\TestMetadataHandler", + * } * ) */ class Test extends MediaStorageProxyBase { + use PluginWithHandlersTrait; + + protected function getInterfacesForHandlerTypes() { + return [ + 'metadata' => MediaStorageProxyMetadataHandlerInterface::class, + ]; + } + /** * {@inheritdoc} */ @@ -36,23 +49,6 @@ public function getThumbnail(MediaInterface $media) { /** * {@inheritdoc} */ - public function getMetadataLabels() { - return [ - 'attribute_1' => $this->t('Attribute 1'), - 'attribute_2' => $this->t('Attribute 2'), - ]; - } - - /** - * {@inheritdoc} - */ - public function getMetadata(MediaInterface $media, $attribute_name) { - return FALSE; - } - - /** - * {@inheritdoc} - */ public function defaultConfiguration() { return parent::defaultConfiguration() + [ 'test_config_value' => 'This is default value.', diff --git a/core/modules/media/tests/modules/media_test_handler/src/Plugin/MediaStorageProxy/TestMetadataHandler.php b/core/modules/media/tests/modules/media_test_handler/src/Plugin/MediaStorageProxy/TestMetadataHandler.php new file mode 100644 index 0000000..32837bc --- /dev/null +++ b/core/modules/media/tests/modules/media_test_handler/src/Plugin/MediaStorageProxy/TestMetadataHandler.php @@ -0,0 +1,40 @@ + $this->t('Attribute 1'), + 'attribute_2' => $this->t('Attribute 2'), + ]; + } + + /** + * {@inheritdoc} + */ + public function getMetadata(MediaInterface $media, $attribute_name) { + return FALSE; + } + +}