Allow to render different view modes per entity type in BuildFieldTrait::buildReferencedEntities
>>> [!note] Migrated issue <!-- Drupal.org comment --> <!-- Migrated from issue #3541124. --> Reported by: [dagmar](https://www.drupal.org/user/154086) Related to !39 >>> <h3 id="summary-problem-motivation">Problem/Motivation</h3> <p>If you have a entity reference field that references multiple entity bundles types, there is no way to render different view modes per entity bundle.</p> <h4 id="summary-steps-reproduce">Steps to reproduce</h4> <p>Add a field to reference paragraph of type text and paragraph of type image gallery, try to render the text paragraphs using the full mode for text, but image thumbnails for image.</p> <h3 id="summary-proposed-resolution">Proposed resolution</h3> <p>Chang the $view_mode param signature in:</p> <div class="codeblock"> <pre><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br>&nbsp; </span><span style="color: #007700">protected function </span><span style="color: #0000BB">buildReferencedEntities</span><span style="color: #007700">(?</span><span style="color: #0000BB">EntityReferenceFieldItemListInterface $reference_field </span><span style="color: #007700">= </span><span style="color: #0000BB">NULL</span><span style="color: #007700">, </span><span style="color: #0000BB">string $view_mode </span><span style="color: #007700">= </span><span style="color: #DD0000">'full'</span><span style="color: #007700">, ?</span><span style="color: #0000BB">string $langcode </span><span style="color: #007700">= </span><span style="color: #0000BB">NULL</span><span style="color: #007700">): array {<br></span><span style="color: #0000BB">?&gt;</span></span></pre></div> <p>and in</p> <div class="codeblock"> <pre><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br>&nbsp; </span><span style="color: #007700">protected function </span><span style="color: #0000BB">buildEntities</span><span style="color: #007700">(array </span><span style="color: #0000BB">$entities </span><span style="color: #007700">= [], </span><span style="color: #0000BB">string $view_mode </span><span style="color: #007700">= </span><span style="color: #DD0000">'full'</span><span style="color: #007700">, ?</span><span style="color: #0000BB">string $langcode </span><span style="color: #007700">= </span><span style="color: #0000BB">NULL</span><span style="color: #007700">): array {<br></span><span style="color: #0000BB">?&gt;</span></span></pre></div> <p>To make <code>$view_mode</code> to be a string or an array. When the array is passed, we can set a list of view modes per entity type.</p> <p>Alternatively it could be a closure, to have even more control, but not sure this complexity is even necessary.</p> <h3 id="summary-remaining-tasks">Remaining tasks</h3> <h3 id="summary-ui-changes">User interface changes</h3> <h3 id="summary-api-changes">API changes</h3> <h3 id="summary-data-model-changes">Data model changes</h3>
issue