Skip to main content

Drupal 8/9/10: Programmatically listing all available entity types

A quick way to find out which configuration or content entities are available in your Drupal site.

Person writing down a list of things in a notebook.

Whether you're new to Drupal or you're working on a complex site you didn't build yourself, it's not always easy to keep an overview of the site's available entity types.

You can run the following snippet as a standalone php script with drush, or integrate it somewhere in your codebase.

Solution

$entity_types = Drupal::entityTypeManager()->getDefinitions();

$result = [];
foreach ($entity_types as $name => $entity_type) {
  $result[$name] = [
    'group' => $entity_type->getGroupLabel()->render(),
    'class' => $entity_type->getClass(),
    'provided by' => $entity_type->getProvider(),
  ];
}

// Sort alphabetically by key and print result.
ksort($result);
print_r($result);

To run this script with drush:

drush scr path/to/myscript.php

Example output

[action] => Array
    (
        [group] => Configuration
        [class] => Drupal\system\Entity\Action
        [provided by] => system
    )

[base_field_override] => Array
    (
        [group] => Configuration
        [class] => Drupal\Core\Field\Entity\BaseFieldOverride
        [provided by] => core
    )

[block] => Array
    (
        [group] => Configuration
        [class] => Drupal\block\Entity\Block
        [provided by] => block
    )

[block_content] => Array
    (
        [group] => Content
        [class] => Drupal\block_content\Entity\BlockContent
        [provided by] => block_content
    )

[block_content_type] => Array
    (
        [group] => Configuration
        [class] => Drupal\block_content\Entity\BlockContentType
        [provided by] => block_content
    )

[comment] => Array
    (
        [group] => Content
        [class] => Drupal\comment\Entity\Comment
        [provided by] => comment
    )

[comment_type] => Array
    (
        [group] => Configuration
        [class] => Drupal\comment\Entity\CommentType
        [provided by] => comment
    )

[...]

Further reading