core/modules/media/media.module | 1 -
.../modules/media/src/Plugin/Filter/MediaEmbed.php | 38 +++++++++++++++++++---
.../MediaEmbedFilterConfigurationUiTest.php | 8 +++--
.../MediaEmbedFilterDisabledIntegrationsTest.php | 1 -
.../tests/src/Kernel/MediaEmbedFilterTest.php | 19 ++++++-----
.../src/Kernel/MediaEmbedFilterTranslationTest.php | 1 -
6 files changed, 50 insertions(+), 18 deletions(-)
diff --git a/core/modules/media/media.module b/core/modules/media/media.module
index 9610c541fa..8f67df3703 100644
--- a/core/modules/media/media.module
+++ b/core/modules/media/media.module
@@ -438,7 +438,6 @@ function media_filter_format_edit_form_validate($form, FormStateInterface $form_
$required_attributes = [
'data-entity-type',
'data-entity-uuid',
- 'data-view-mode',
];
// If there are no attributes, the allowed item is set to FALSE,
diff --git a/core/modules/media/src/Plugin/Filter/MediaEmbed.php b/core/modules/media/src/Plugin/Filter/MediaEmbed.php
index aa159f8cd8..1893371043 100644
--- a/core/modules/media/src/Plugin/Filter/MediaEmbed.php
+++ b/core/modules/media/src/Plugin/Filter/MediaEmbed.php
@@ -3,9 +3,11 @@
namespace Drupal\media\Plugin\Filter;
use Drupal\Component\Utility\Html;
+use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
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;
@@ -25,6 +27,9 @@
* title = @Translation("Embed media"),
* description = @Translation("Embeds media items using a custom HTML tag. If used in conjunction with the 'Align/Caption' filters, make sure this filter is configured to run after them."),
* type = Drupal\filter\Plugin\FilterInterface::TYPE_TRANSFORM_REVERSIBLE,
+ * settings = {
+ * "default_view_mode" = "full",
+ * },
* weight = 100,
* )
*
@@ -46,6 +51,13 @@ class MediaEmbed extends FilterBase implements ContainerFactoryPluginInterface {
*/
protected $entityTypeManager;
+ /**
+ * The entity display repository.
+ *
+ * @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface
+ */
+ protected $entityDisplayRepository;
+
/**
* The renderer.
*
@@ -85,15 +97,18 @@ class MediaEmbed extends FilterBase implements ContainerFactoryPluginInterface {
* 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\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, RendererInterface $renderer, LoggerChannelFactoryInterface $logger_factory) {
+ public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityRepositoryInterface $entity_repository, EntityTypeManagerInterface $entity_type_manager, EntityDisplayRepositoryInterface $entity_display_repository, 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->renderer = $renderer;
$this->loggerFactory = $logger_factory;
}
@@ -108,11 +123,26 @@ public static function create(ContainerInterface $container, array $configuratio
$plugin_definition,
$container->get('entity.repository'),
$container->get('entity_type.manager'),
+ $container->get('entity_display.repository'),
$container->get('renderer'),
$container->get('logger.factory')
);
}
+ /**
+ * {@inheritdoc}
+ */
+ public function settingsForm(array $form, FormStateInterface $form_state) {
+ $form['default_view_mode'] = [
+ '#type' => 'select',
+ '#options' => $this->entityDisplayRepository->getViewModeOptions('media'),
+ '#title' => $this->t('Default view mode'),
+ '#default_value' => $this->settings['default_view_mode'],
+ '#description' => $this->t('The view mode that embedded media should be displayed in by default. This can be overridden by using the data-view-mode attribute.'),
+ ];
+ return $form;
+ }
+
/**
* Builds the render array for the given media entity in the given langcode.
*
@@ -204,10 +234,10 @@ public function process($text, $langcode) {
$dom = Html::load($text);
$xpath = new \DOMXPath($dom);
- foreach ($xpath->query('//drupal-media[@data-entity-type="media" and normalize-space(@data-view-mode)!="" and normalize-space(@data-entity-uuid)!=""]') as $node) {
+ 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');
+ $view_mode_id = $node->getAttribute('data-view-mode') ?: $this->settings['default_view_mode'];
// Delete the consumed attributes.
$node->removeAttribute('data-entity-type');
@@ -259,7 +289,7 @@ public function tips($long = FALSE) {
You can embed media items:
<drupal-media data-entity-uuid="07bf3a2e-1941-4a44-9b02-2d1d7a41ec0e" />data-view-mode="teaser".data-view-mode="tiny_embed", otherwise the default view mode is used.data-entity-type="media" attribute is required for consistency. -
-
-
",
- 'expected_error_message' => 'The tag in the allowed HTML tags is missing the following attributes: data-entity-type, data-entity-uuid, data-view-mode.',
+ 'expected_error_message' => 'The tag in the allowed HTML tags is missing the following attributes: data-entity-type, data-entity-uuid.',
],
'Tests validation when drupal-media element lacks some required attributes.' => [
'filters[filter_html][status]' => TRUE,
@@ -178,8 +180,8 @@ public function providerTestValidations() {
'filters[filter_caption][status]' => FALSE,
'filters[filter_html_image_secure][status]' => FALSE,
'media_embed' => TRUE,
- 'allowed_html' => "