Drupal 8/9: Listing all available entity types
Tags: drupal, php, programming, snippet
Published: 2021-05-19 10:41:01

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](https://drupalconsole.com/) installed, you can run the following anywhere inside your Drupal project:

```bash
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

```php
$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](https://drush.org/) 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()`](https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21EntityTypeManager.php/class/EntityTypeManager/9.1.x) on api.drupal.org.

---

If you liked this article, you'll love [our courses](https://trainingcloud.io/courses) on Drupal 8/9 Site Building, Module Development, and Theme Development. 

![TrainingCloud: Drupal and PHP training for (aspiring) developers](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2wav7v6hn30d0q9vfy3c.png)

---

## Credits
* Article image: [Unsplash / Glenn Carstens-Peters](https://unsplash.com/photos/RLw-UC03Gwc/)