From d8b4e40b05c13312e84e82c102e6e3fffb29a05b Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Mon, 18 Oct 2021 14:56:08 +0100
Subject: [PATCH] Issue #3205866 by danflanagan8, phenaproxima:
 media_requirements() should report missing source fields

---
 core/modules/media/media.install              | 33 ++++++++++++++-----
 .../src/Functional/MediaRequirementsTest.php  |  3 ++
 2 files changed, 27 insertions(+), 9 deletions(-)

diff --git a/core/modules/media/media.install b/core/modules/media/media.install
index a2e4751f2cad..1be5ae6db62a 100644
--- a/core/modules/media/media.install
+++ b/core/modules/media/media.install
@@ -120,22 +120,37 @@ function media_requirements($phase) {
       }
     }
 
-    // When a new media type with an image source is created we're configuring
-    // the default entity view display using the 'large' image style.
-    // Unfortunately, if a site builder has deleted the 'large' image style,
-    // we need some other image style to use, but at this point, we can't
-    // really know the site builder's intentions. So rather than do something
-    // surprising, we're leaving the embedded media without an image style and
-    // adding a warning that the site builder might want to add an image style.
-    // @see Drupal\media\Plugin\media\Source\Image::prepareViewDisplay
     $module_handler = \Drupal::service('module_handler');
     foreach (MediaType::loadMultiple() as $type) {
       // Load the default display.
       $display = \Drupal::service('entity_display.repository')
         ->getViewDisplay('media', $type->id());
 
+      // Check for missing source field definition.
       $source_field_definition = $type->getSource()->getSourceFieldDefinition($type);
-      if (empty($source_field_definition) || !is_a($source_field_definition->getItemDefinition()->getClass(), ImageItem::class, TRUE)) {
+      if (empty($source_field_definition)) {
+        $requirements['media_missing_source_field_' . $type->id()] = [
+          'title' => t('Media'),
+          'description' => t('The source field definition for the %type media type is missing.',
+            [
+              '%type' => $type->label(),
+            ]
+          ),
+          'severity' => REQUIREMENT_ERROR,
+        ];
+        continue;
+      }
+
+      // When a new media type with an image source is created we're
+      // configuring the default entity view display using the 'large' image
+      // style. Unfortunately, if a site builder has deleted the 'large' image
+      // style, we need some other image style to use, but at this point, we
+      // can't really know the site builder's intentions. So rather than do
+      // something surprising, we're leaving the embedded media without an
+      // image style and adding a warning that the site builder might want to
+      // add an image style.
+      // @see Drupal\media\Plugin\media\Source\Image::prepareViewDisplay
+      if (!is_a($source_field_definition->getItemDefinition()->getClass(), ImageItem::class, TRUE)) {
         continue;
       }
 
diff --git a/core/modules/media/tests/src/Functional/MediaRequirementsTest.php b/core/modules/media/tests/src/Functional/MediaRequirementsTest.php
index e8ff550cbcb2..db12de615073 100644
--- a/core/modules/media/tests/src/Functional/MediaRequirementsTest.php
+++ b/core/modules/media/tests/src/Functional/MediaRequirementsTest.php
@@ -26,10 +26,13 @@ public function testMissingSourceFieldDefinition() {
     $field_storage_definition = $field_definition->getFieldStorageDefinition();
     $field_definition->delete();
     $field_storage_definition->delete();
+    $valid_media_type = $this->createMediaType('test');
 
     $this->drupalLogin($this->rootUser);
     $this->drupalGet('/admin/reports/status');
     $this->assertSession()->statusCodeEquals(200);
+    $this->assertSession()->pageTextContains("The source field definition for the {$media_type->label()} media type is missing.");
+    $this->assertSession()->pageTextNotContains("The source field definition for the {$valid_media_type->label()} media type is missing.");
   }
 
 }
-- 
GitLab