Skip to content

Instantly share code, notes, and snippets.

@MatthieuScarset
Created October 18, 2021 05:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MatthieuScarset/cd9669e5c869992311c8c7876b6a752a to your computer and use it in GitHub Desktop.
Save MatthieuScarset/cd9669e5c869992311c8c7876b6a752a to your computer and use it in GitHub Desktop.
Drupal - Check if a config entity type is a content entity type (or fieldable entity type...etc)
<?php
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
$entity_types = \Drupal::service('entity_type.manager')->getDefinitions();
/** @var \Drupal\Core\Config\Entity\ConfigEntityInterface $entity_type */
foreach ($entity_types as $entity_type) {
// Stop now if entity type is not fieldable.
if (!$entity_type->entityClassImplements(FieldableEntityInterface::class)) {
continue;
}
// Stop now if entity type is not a cnntent entity type.
if (!$entity_type->entityClassImplements(ContentEntityInterface::class)) {
continue;
}
// Do your things now...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment