When using Canvas module, Media Library Widget Fails with BaseFieldDefinition
>>> [!note] Migrated issue <!-- Drupal.org comment --> <!-- Migrated from issue #3560775. --> Reported by: [neerajkumar007](https://www.drupal.org/user/3785577) >>> <h3 id="summary-problem-motivation">Problem/Motivation</h3> <p>In Drupal Version 11.2.8 or greater.</p> <p>When using Canvas module, The Media Library widget encounters a fatal error when rendering because it attempts to call a method that does not exist on base fields.</p> <p>Error message: Call to undefined method Drupal\Core\Field\BaseFieldDefinition::id()</p> <p>The issue is located in the <code>getNoMediaTypesAvailableMessage()</code> method within:<br> <code>core/modules/media_library/src/Plugin/Field/FieldWidget/MediaLibraryWidget.php</code>.</p> <p>The specific line causing the crash is:</p> <div class="codeblock"> <pre><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br>$route_parameters</span><span style="color: #007700">[</span><span style="color: #DD0000">'field_config'</span><span style="color: #007700">] = </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fieldDefinition</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">id</span><span style="color: #007700">();<br></span><span style="color: #0000BB">?&gt;</span></span></pre></div> <p>The code assumes <code>$this-&gt;fieldDefinition</code> is always an instance of <strong>FieldConfig</strong> (configurable fields). However, in some contexts, it is an instance of <strong>BaseFieldDefinition</strong> (base fields defined in code).</p> <ul> <li><strong>FieldConfig</strong> has the <code>id()</code> method.</li> <li><strong>BaseFieldDefinition</strong> does <em>not</em> implement the <code>id()</code> method.</li> </ul> <p>This mismatch causes a PHP Fatal Error when the Media Library attempts to build the "Edit field settings" route.</p> <h4 id="summary-steps-reproduce">Steps to reproduce</h4> <ol> <li>Install Drupal with Minimal profile</li> <li>Install Olivero as default theme, then installed Claro and made it the admin theme</li> <li>Install Block Content Breakpoint, CKEditor 5, Configuration Manager, Contextual Links, Datetime, Text Editor, Field UI, File, Help, Image, Layout Discovery, Link, Media, Media Library, Custom Menu Links, Menu UI, Options, Path, Responsive Image, Taxonomy, Toolbar, Views, Views UI</li> <li>Install Drupal Canvas module</li> <li>Visit Drupal Canvas link in the toolbar</li> <li>Follow instructions, click No page selected drop down, choose + New drop down, choose New page</li> <li>Unrecoverable error</li> </ol> <p>After entering the Drupal Canvas interface, create a new page.<br> At this point, the Media Library widget attempts to load and the error appears (as shown in the attached screenshot).</p> <h3 id="summary-proposed-resolution">Proposed resolution</h3> <p>TBD</p> <h3 id="summary-remaining-tasks">Remaining tasks</h3> <p>Here, I need a suggestion on whether this fix is valid or I am missing something.</p> <h4>Fixing the issue in Drupal core</h4> <p>Drupal field configuration IDs always follow the standardized pattern: <code>&lt;entity_type&gt;.&lt;bundle&gt;.&lt;field_name&gt;</code>.<br> The components required to build this string (Entity Type ID, Bundle, and Field Name) are consistently available via getter methods on both <code>FieldConfig</code> and <code>BaseFieldDefinition</code> objects. This approach removes the dependency on the missing <code>id()</code> method.</p> <p>This issue was discovered while working with <strong>Drupal Canvas</strong>. While the error surfaced there, the root cause lies in Drupal Core's assumption that the Media Library widget is only used on configurable fields, thereby neglecting Base Fields that use it.</p> <p>The fix involves manually constructing the field configuration identifier using methods available to both field types.</p> <div class="codeblock"> <pre><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br>$route_parameters</span><span style="color: #007700">[</span><span style="color: #DD0000">'field_config'</span><span style="color: #007700">] = </span><span style="color: #0000BB">$entity_type_id </span><span style="color: #007700">. </span><span style="color: #DD0000">'.'</span><span style="color: #007700">. </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fieldDefinition</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getTargetBundle</span><span style="color: #007700">() . </span><span style="color: #DD0000">'.'</span><span style="color: #007700">. </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fieldDefinition</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getName</span><span style="color: #007700">();<br></span><span style="color: #0000BB">?&gt;</span></span></pre></div> <h4>Workarounds</h4> <p>This might affect the proposed resolution.</p> <ul> <li>Creating a Media bundle prior to installing Drupal Canvas will work around the issue.</li> </ul> <h3 id="summary-ui-changes">User interface changes</h3> <p>TBD</p> <h3 id="summary-introduced-terminology">Introduced terminology</h3> <p>TBD</p> <h3 id="summary-api-changes">API changes</h3> <p>TBD</p> <h3 id="summary-data-model-changes">Data model changes</h3> <p>TBD</p> <h3 id="summary-release-notes">Release notes snippet</h3> <p>TBD</p> > Related issue: [Issue #3563430](https://www.drupal.org/node/3563430)
issue