Deleting media references causes InvalidComponentException and AssertionError
>>> [!note] Migrated issue <!-- Drupal.org comment --> <!-- Migrated from issue #3590029. --> Reported by: [danepowell](https://www.drupal.org/user/339326) >>> <h3 id="overview">Overview</h3> <p>If you reference a media entity from a region (or other config component) and then delete the referenced entity, it causes at least two types of errors.</p> <p>On the page itself, you'll get an InvalidComponentException, which is ugly but recoverable. If you try to run canvas_post_update_0016_page_region_component_inputs_must_be_arrays, you'll get a nastier error that crashes updates:</p> <pre>[error]&nbsp; AssertionError: assert(Inspector::assertAllObjects($referenced_content_entities, FieldableEntityInterface::class)) in assert() (line 210 of /var/www/html/docroot/modules/contrib/canvas/src/PropExpressions/StructuredData/ReferenceFieldTypePropExpression.php) #0 /var/www/html/docroot/modules/contrib/canvas/src/PropExpressions/StructuredData/ReferenceFieldTypePropExpression.php(210): assert()</pre><p>Steps to reproduce:</p> <ol> <li>Install Canvas 1.3.3</li> <li>Edit a page region, such as the header, and add an image component with an actual image reference</li> <li>Go to the media library and delete the referenced image</li> <li>Revisit the page, see an InvalidComponentException in the header region</li> <li>Install Canvas 1.4.0 and run drush updb</li> <li>Observe an AssertionError</li> </ol> <p>For reference, here's what the region with the media reference looks like <em>after</em> the entity is deleted:</p> <pre>ddev drush cget canvas.page_region.astral.header&nbsp;&nbsp;&nbsp;&nbsp; <br><br>uuid: f1e2bf9c-6ca1-4102-8bf7-573cf957bb45<br>langcode: en<br>status: true<br>dependencies:<br>&nbsp; config:<br>&nbsp;&nbsp;&nbsp; - canvas.component.js.header<br>&nbsp;&nbsp;&nbsp; - canvas.component.js.image<br>&nbsp;&nbsp;&nbsp; - canvas.component.js.logo<br>&nbsp;&nbsp;&nbsp; - canvas.component.js.main_navigation<br>&nbsp;&nbsp;&nbsp; - field.field.media.image.media_image<br>&nbsp;&nbsp;&nbsp; - image.style.canvas_parametrized_width<br>&nbsp;&nbsp;&nbsp; - media.type.acquia_dam_image_asset<br>&nbsp;&nbsp;&nbsp; - media.type.image<br>&nbsp; content:<br>&nbsp;&nbsp;&nbsp; - 'file:file:9dbfd988-3286-495e-87c0-90383984e3d3'<br>&nbsp;&nbsp;&nbsp; - 'media:image:93809c00-387a-446d-b8ca-d4adde378084'<br>&nbsp; module:<br>&nbsp;&nbsp;&nbsp; - acquia_dam<br>&nbsp;&nbsp;&nbsp; - file<br>&nbsp;&nbsp;&nbsp; - media<br>&nbsp; theme:<br>&nbsp;&nbsp;&nbsp; - astral<br>_core:<br>&nbsp; default_config_hash: ibBobeG3N57rxkjWfMklXLGPTkqeW-1BSPEwfEqB2fE<br>id: astral.header<br>region: header<br>theme: astral<br>component_tree:<br>&nbsp; 0:<br>&nbsp;&nbsp;&nbsp; uuid: 68fdccf3-114d-446d-bd10-8a8b6005a612<br>&nbsp;&nbsp;&nbsp; component_id: js.header<br>&nbsp;&nbsp;&nbsp; component_version: 043cb73114a3bf3f<br>&nbsp;&nbsp;&nbsp; inputs: '{"displaySearchForm":true}'<br>&nbsp; '0:logo:0':<br>&nbsp;&nbsp;&nbsp; parent_uuid: 68fdccf3-114d-446d-bd10-8a8b6005a612<br>&nbsp;&nbsp;&nbsp; slot: logo<br>&nbsp;&nbsp;&nbsp; uuid: 9bc3c4e7-81e1-41ef-ba40-061561d358ef<br>&nbsp;&nbsp;&nbsp; component_id: js.logo<br>&nbsp;&nbsp;&nbsp; component_version: 8fe3be948e0194e1<br>&nbsp;&nbsp;&nbsp; inputs: '[]'<br>&nbsp; '0:menu:0':<br>&nbsp;&nbsp;&nbsp; parent_uuid: 68fdccf3-114d-446d-bd10-8a8b6005a612<br>&nbsp;&nbsp;&nbsp; slot: menu<br>&nbsp;&nbsp;&nbsp; uuid: 7d543abc-9be9-4353-b940-38dfa578dd6a<br>&nbsp;&nbsp;&nbsp; component_id: js.main_navigation<br>&nbsp;&nbsp;&nbsp; component_version: 8fe3be948e0194e1<br>&nbsp;&nbsp;&nbsp; inputs: '[]'<br>&nbsp; 1:<br>&nbsp;&nbsp;&nbsp; uuid: ffdc886f-9dd1-4df1-bdf1-057e324a6cfa<br>&nbsp;&nbsp;&nbsp; component_id: js.image<br>&nbsp;&nbsp;&nbsp; component_version: f312cd1f9fe0bcf0<br>&nbsp;&nbsp;&nbsp; inputs: '{"image":"1"}'</pre><h3 id="proposed-resolution">Proposed resolution</h3> <p>The attached patch should fix it but I have no idea if it's the best solution.</p> <h3 id="ui-changes">User interface changes</h3>
issue