Problem/Motivation

Coming from:
- #3529979: Incorrect type hint in rdf_sync_entity_bundle_field_info() hook implementation
- #3529976: Incorrect type hint in meta_entity_entity_bundle_field_info() hook implementation

It seems those issues originate from the fact that og_entity_create_access ends up calling \Drupal\Core\Entity\EntityFieldManager::getFieldDefinitions with $bundle param being NULL. Both under \Drupal\og\Og::isGroupContent and in the hook itself.

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.

\Drupal::service('entity_field.manager')->getFieldDefinitions('user', 'user');
\Drupal::service('entity_field.manager')->getFieldDefinitions('comment', 'comment');
...

Steps to reproduce

- Install drupal core
- Add a hook_entity_bundle_field_info implementation using string $bundle param, or change the one in field module: \Drupal\field\Hook\FieldHooks::entityBundleFieldInfo
- Call createAccess() without a bundle on e.g. user:
\Drupal::entityTypeManager()->getAccessControlHandler('user')->createAccess();
> No error

- Now enable og and repeat previous step.

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->entityBundleFieldInfo()

Proposed resolution

In og_entity_create_access(), ensure we set a bundle before checking group content status:
$bundle = $bundle ?: $entity_type_id;

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.

Remaining tasks

?

User interface changes

N/A

API changes

Data model changes

Issue fork og-3577326

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

herved created an issue. See original summary.

herved’s picture

Issue summary: View changes

herved’s picture

I created a core issue there #3577486: Incorrect documentation for EntityAccessControlHandlerInterface::createAccess and entity_create_access hooks
Maybe we need to postpone this og issue until a decision is made there in core.

claudiu.cristea’s picture

I see it legit. Bundle should always be a string even for entity types w/o a bundle

claudiu.cristea’s picture

Status: Active » Reviewed & tested by the community
amitaibu’s picture

Thanks, merging

herved’s picture

I see phpstan issues were fixed in #3552180: Untranslatable strings in OgEventSubscriber.php since then, so I only rebased the MR