From 2284a0a6f43b2f4ce9e7338045bfa5eced14398e Mon Sep 17 00:00:00 2001
From: prashantdsala <p1989chauhan@gmail.com>
Date: Fri, 25 Apr 2025 12:51:05 +0530
Subject: [PATCH 1/3] Added validation message, used contructor property
 promotion

---
 .../media/src/Plugin/Filter/MediaEmbed.php    | 94 +++++++------------
 1 file changed, 36 insertions(+), 58 deletions(-)

diff --git a/core/modules/media/src/Plugin/Filter/MediaEmbed.php b/core/modules/media/src/Plugin/Filter/MediaEmbed.php
index 118422bf2c28..fb6d6468e24a 100644
--- a/core/modules/media/src/Plugin/Filter/MediaEmbed.php
+++ b/core/modules/media/src/Plugin/Filter/MediaEmbed.php
@@ -4,9 +4,6 @@
 
 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;
@@ -16,6 +13,10 @@
 use Drupal\Core\Render\RendererInterface;
 use Drupal\Core\Security\TrustedCallbackInterface;
 use Drupal\Core\StringTranslation\TranslatableMarkup;
+use Drupal\Core\Url;
+use Drupal\Core\Entity\EntityRepositoryInterface;
+use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
+use Drupal\Core\Entity\EntityTypeManagerInterface;
 use Drupal\filter\Attribute\Filter;
 use Drupal\filter\FilterProcessResult;
 use Drupal\filter\Plugin\FilterBase;
@@ -43,48 +44,6 @@
 )]
 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.
    *
@@ -106,27 +65,31 @@ class MediaEmbed extends FilterBase implements ContainerFactoryPluginInterface,
    *   The plugin ID for the plugin instance.
    * @param mixed $plugin_definition
    *   The plugin implementation definition.
-   * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
+   * @param \Drupal\Core\Entity\EntityRepositoryInterface $entityRepository
    *   The entity repository.
-   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
+   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
    *   The entity type manager.
-   * @param \Drupal\Core\Entity\EntityDisplayRepositoryInterface $entity_display_repository
+   * @param \Drupal\Core\Entity\EntityDisplayRepositoryInterface $entityDisplayRepository
    *   The entity display repository.
-   * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $bundle_info
+   * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entityTypeBundleInfo
    *   The entity type bundle info service.
    * @param \Drupal\Core\Render\RendererInterface $renderer
    *   The renderer.
-   * @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_factory
+   * @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $loggerFactory
    *   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) {
+  public function __construct(
+    array $configuration,
+    $plugin_id,
+    $plugin_definition,
+    protected EntityRepositoryInterface $entityRepository,
+    protected EntityTypeManagerInterface $entityTypeManager,
+    protected EntityDisplayRepositoryInterface $entityDisplayRepository,
+    protected EntityTypeBundleInfoInterface $entityTypeBundleInfo,
+    protected RendererInterface $renderer,
+    protected LoggerChannelFactoryInterface $loggerFactory,
+  ) {
     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;
   }
 
   /**
@@ -164,12 +127,17 @@ public function settingsForm(array $form, FormStateInterface $form_state) {
     $bundle_options = array_map(function ($item) {
       return $item['label'];
     }, $bundles);
+
+    $description = empty($bundle_options)
+      ? $this->t('No media types defined. <a href=":url">Add media types</a>.', [':url' => Url::fromRoute('entity.media_type.collection')->toString()])
+      : $this->t('If none are selected, all will be allowed.');
+
     $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.'),
+      '#description' => $description,
       '#element_validate' => [[static::class, 'validateOptions']],
     ];
 
@@ -194,6 +162,16 @@ public function settingsForm(array $form, FormStateInterface $form_state) {
    *   The form state.
    */
   public static function validateOptions(array &$element, FormStateInterface $form_state) {
+    // Check if bundles in media type exist.
+    $bundles = \Drupal::service('entity_type.bundle.info')->getBundleInfo('media');
+    if (empty($bundles)) {
+      $filter_title = \Drupal::service('plugin.manager.filter')
+        ->getDefinition('media_embed')['title']->render();
+      $form_state->setError($element, t('No media types are defined. <a href=":url">Add media types</a> before configuring the @filter filter.', [
+        ':url' => Url::fromRoute('entity.media_type.collection')->toString(),
+        '@filter' => $filter_title,
+      ]));
+    }
     // Filters the #value property so only selected values appear in the
     // config.
     $form_state->setValueForElement($element, array_filter($element['#value']));
-- 
GitLab


From 5363a9d8e45ac274e2e5ab007f1cea7019d816ee Mon Sep 17 00:00:00 2001
From: prashantdsala <p1989chauhan@gmail.com>
Date: Fri, 25 Apr 2025 15:53:03 +0530
Subject: [PATCH 2/3] Removed the status message assertion

---
 .../ckeditor5/tests/src/FunctionalJavascript/AdminUiTest.php     | 1 -
 1 file changed, 1 deletion(-)

diff --git a/core/modules/ckeditor5/tests/src/FunctionalJavascript/AdminUiTest.php b/core/modules/ckeditor5/tests/src/FunctionalJavascript/AdminUiTest.php
index d380114dfe7d..647a89f948fe 100644
--- a/core/modules/ckeditor5/tests/src/FunctionalJavascript/AdminUiTest.php
+++ b/core/modules/ckeditor5/tests/src/FunctionalJavascript/AdminUiTest.php
@@ -223,7 +223,6 @@ public function testImageUploadSettingsAreValidated(): void {
     // No more validation errors, let's save.
     $this->assertNoRealtimeValidationErrors();
     $page->pressButton('Save configuration');
-    $assert_session->pageTextContains('The text format ckeditor5 has been updated');
   }
 
   /**
-- 
GitLab


From 45fe0a01beb53cb1c40089022a58beafeb3eaa80 Mon Sep 17 00:00:00 2001
From: prashantdsala <p1989chauhan@gmail.com>
Date: Fri, 25 Apr 2025 16:33:37 +0530
Subject: [PATCH 3/3] Adding check to the validation, fixing the functional
 test

---
 .../src/FunctionalJavascript/AdminUiTest.php  |  4 +++-
 .../media/src/Plugin/Filter/MediaEmbed.php    | 22 +++++++++++--------
 2 files changed, 16 insertions(+), 10 deletions(-)

diff --git a/core/modules/ckeditor5/tests/src/FunctionalJavascript/AdminUiTest.php b/core/modules/ckeditor5/tests/src/FunctionalJavascript/AdminUiTest.php
index 647a89f948fe..d801f72381ab 100644
--- a/core/modules/ckeditor5/tests/src/FunctionalJavascript/AdminUiTest.php
+++ b/core/modules/ckeditor5/tests/src/FunctionalJavascript/AdminUiTest.php
@@ -222,7 +222,9 @@ public function testImageUploadSettingsAreValidated(): void {
 
     // No more validation errors, let's save.
     $this->assertNoRealtimeValidationErrors();
-    $page->pressButton('Save configuration');
+    $page->pressButton(locator: 'Save configuration');
+    // $this->assertSession()->waitForElementVisible('css', '[role="status"], .messages--status');
+    $assert_session->pageTextContains('The text format ckeditor5 has been updated.');
   }
 
   /**
diff --git a/core/modules/media/src/Plugin/Filter/MediaEmbed.php b/core/modules/media/src/Plugin/Filter/MediaEmbed.php
index fb6d6468e24a..b8e3b44b447a 100644
--- a/core/modules/media/src/Plugin/Filter/MediaEmbed.php
+++ b/core/modules/media/src/Plugin/Filter/MediaEmbed.php
@@ -162,15 +162,19 @@ public function settingsForm(array $form, FormStateInterface $form_state) {
    *   The form state.
    */
   public static function validateOptions(array &$element, FormStateInterface $form_state) {
-    // Check if bundles in media type exist.
-    $bundles = \Drupal::service('entity_type.bundle.info')->getBundleInfo('media');
-    if (empty($bundles)) {
-      $filter_title = \Drupal::service('plugin.manager.filter')
-        ->getDefinition('media_embed')['title']->render();
-      $form_state->setError($element, t('No media types are defined. <a href=":url">Add media types</a> before configuring the @filter filter.', [
-        ':url' => Url::fromRoute('entity.media_type.collection')->toString(),
-        '@filter' => $filter_title,
-      ]));
+    $filters = $form_state->getValue('filters');
+    $is_enabled = isset($filters['media_embed']) ? $filters['media_embed']['status'] : NULL;
+    if ($is_enabled) {
+      // Check if bundles in media type exist.
+      $bundles = \Drupal::service('entity_type.bundle.info')->getBundleInfo('media');
+      if (empty($bundles)) {
+        $filter_title = \Drupal::service('plugin.manager.filter')
+          ->getDefinition('media_embed')['title']->render();
+        $form_state->setError($element, t('No media types are defined. <a href=":url">Add media types</a> before configuring the @filter filter.', [
+          ':url' => Url::fromRoute('entity.media_type.collection')->toString(),
+          '@filter' => $filter_title,
+        ]));
+      }
     }
     // Filters the #value property so only selected values appear in the
     // config.
-- 
GitLab