og_entity_create_access() passes NULL bundle to getFieldDefinitions() causing TypeError in hook_entity_bundle_field_info implementations
>>> [!note] Migrated issue <!-- Drupal.org comment --> <!-- Migrated from issue #3577326. --> Reported by: [herved](https://www.drupal.org/user/2197136) Related to !70 >>> <h3 id="summary-problem-motivation">Problem/Motivation</h3> <p>Coming from:<br> - <span class="drupalorg-gitlab-issue-link project-issue-status-info project-issue-status-7"><a href="https://www.drupal.org/project/rdf_sync/issues/3529979" title="Status: Closed (fixed)">#3529979: Incorrect type hint in rdf_sync_entity_bundle_field_info() hook implementation</a></span><br> - <span class="drupalorg-gitlab-issue-link project-issue-status-info project-issue-status-7"><a href="https://www.drupal.org/project/meta_entity/issues/3529976" title="Status: Closed (fixed)">#3529976: Incorrect type hint in meta_entity_entity_bundle_field_info() hook implementation</a></span></p> <p>It seems those issues originate from the fact that <code>og_entity_create_access</code> ends up calling <code>\Drupal\Core\Entity\EntityFieldManager::getFieldDefinitions</code> with <code>$bundle</code> param being <code>NULL</code>. Both under \Drupal\og\Og::isGroupContent and in the hook itself.</p> <p>This never happens in directly with drupal core alone: if the entity is not bundle-able or has only 1 bundle then getFieldDefinitions is always called with $bundle = $entity_id, e.g.</p> <pre>\Drupal::service('entity_field.manager')-&gt;getFieldDefinitions('user', 'user');<br>\Drupal::service('entity_field.manager')-&gt;getFieldDefinitions('comment', 'comment');<br>...</pre><h4 id="summary-steps-reproduce">Steps to reproduce</h4> <p>- Install drupal core<br> - Add a hook_entity_bundle_field_info implementation using <code>string $bundle</code> param, or change the one in field module: \Drupal\field\Hook\FieldHooks::entityBundleFieldInfo<br> - Call createAccess() without a bundle on e.g. user:<br> <code>\Drupal::entityTypeManager()-&gt;getAccessControlHandler('user')-&gt;createAccess();</code><br> &gt; No error</p> <p>- Now enable og and repeat previous step.</p> <blockquote><p>TypeError: Drupal\field\Hook\FieldHooks::entityBundleFieldInfo(): Argument #2 ($bundle) must be of type string, null given, called in /var/www/html/web/core/lib/Drupal/Core/Entity/EntityFieldManager.php on line 432 in /var/www/html/web/core/modules/field/src/Hook/FieldHooks.php on line 210 #0 /var/www/html/web/core/lib/Drupal/Core/Entity/EntityFieldManager.php(432): Drupal\field\Hook\FieldHooks-&gt;entityBundleFieldInfo()</p></blockquote> <h3 id="summary-proposed-resolution">Proposed resolution</h3> <p>In og_entity_create_access(), ensure we set a bundle before checking group content status:<br> <code>$bundle = $bundle ?: $entity_type_id;</code></p> <p>The documentation in core for hook_entity_create_access is incorrect and should take into account that $entity_bundle can be NULL, but that is out of scope here.</p> <h3 id="summary-remaining-tasks">Remaining tasks</h3> <p>?</p> <h3 id="summary-ui-changes">User interface changes</h3> <p>N/A</p> <h3 id="summary-api-changes">API changes</h3> <h3 id="summary-data-model-changes">Data model changes</h3>
issue