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:

'); } diff --git a/core/modules/media/tests/src/FunctionalJavascript/MediaEmbedFilterConfigurationUiTest.php b/core/modules/media/tests/src/FunctionalJavascript/MediaEmbedFilterConfigurationUiTest.php index 7621cfbc78..b5051cdd59 100644 --- a/core/modules/media/tests/src/FunctionalJavascript/MediaEmbedFilterConfigurationUiTest.php +++ b/core/modules/media/tests/src/FunctionalJavascript/MediaEmbedFilterConfigurationUiTest.php @@ -73,6 +73,7 @@ public function testValidationWhenAdding($filter_html_status, $filter_align_stat } } if (!empty($allowed_html)) { + $page->clickLink('Limit allowed HTML tags and correct faulty HTML'); $page->fillField('filters[filter_html][settings][allowed_html]', $allowed_html); } $page->pressButton('Save configuration'); @@ -117,6 +118,7 @@ public function testValidationWhenEditing($filter_html_status, $filter_align_sta } } if (!empty($allowed_html)) { + $page->clickLink('Limit allowed HTML tags and correct faulty HTML'); $page->fillField('filters[filter_html][settings][allowed_html]', $allowed_html); } $page->pressButton('Save configuration'); @@ -170,7 +172,7 @@ public function providerTestValidations() { 'filters[filter_html_image_secure][status]' => FALSE, 'media_embed' => TRUE, 'allowed_html' => "