Drupal 8/9: Listing all available entity types
TrainingCloud.io
Posted on May 19, 2021
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.
Here are two quick ways to help you gain insight into your Drupal entity types:
Option 1. Using Drupal Console
If you have Drupal Console installed, you can run the following anywhere inside your Drupal project:
drupal debug:entity
or its shorter alias de
:
drupal de
Sample output
--------------------------- ---------------
Entity name Entity type
--------------------------- ---------------
comment Content
contact_message Content
node Content
block_content Content
menu_link_content Content
file Content
group Content
group_content Content
shortcut Content
taxonomy_term Content
path_alias Content
user Content
action Configuration
base_field_override Configuration
block Configuration
comment_type Configuration
contact_form Configuration
language_content_settings Configuration
node_type Configuration
block_content_type Configuration
[...]
Option 2: Using a code snippet
$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);
You could save this as a standalone php script (let's name it entities.php
, and run it with drush or Drupal Console:
drush scr path/to/entities.php
or
drupal snippet --file=/full/path/to/entities.php
Sample 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
-
Drupal::entityTypeManager()
on api.drupal.org.
If you liked this article, you'll love our courses on Drupal 8/9 Site Building, Module Development, and Theme Development.
Credits
- Article image: Unsplash / Glenn Carstens-Peters
💖 💪 🙅 🚩
TrainingCloud.io
Posted on May 19, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.