If I have an entity type id as a string, how I can tell which base fields are defined for that entity type?

For example, how do I programatically check that an entity type has the "changed" field?

  • Note that entity keys might have a different field name. For example the id key of nodes is named nid. – Eyal Apr 12 '16 at 6:24

How to get the base fields for an entity type id:

$base_fields = \Drupal::entityManager()->getBaseFieldDefinitions($entity_type_id);

You get an array. The keys are the field names. Example how to loop through this array:

foreach ( $base_fields as $field_name => $definition ) {
  echo "$field_name: ", $definition->getDescription(), "\n";


The method has moved to the service EntityFieldManager:

$base_fields = \Drupal::service('entity_field.manager')
| improve this answer | |
  • EntityManager is deprecated – Eyal Apr 12 '16 at 10:12
  • EntityManager is used 2500 times in core. For me it seems ok to use it. – 4k4 Apr 12 '16 at 11:28
  • When something is marked as deprecated it means it is going to be removed in future versions. If you want to future proof your code then avoid deprecated methods and classes. There is an alternative method in the description. – Eyal Apr 12 '16 at 11:37
  • But for now there are a lot of things you can only do with EntityManager. This will take some time until it will get obsolete. As long as core uses it, I will do the same. Which is the alternative method? Please provide the code. – 4k4 Apr 12 '16 at 11:45
  • The methods in entity manager are all synthetic sugar to the new implantations. They exist only for backwards compatibility. – Eyal Apr 12 '16 at 11:47

The easiest way is through the entity type manager service, by getting the entity type definition and class:

$entity_type = 'node';
$container = \Drupal::getContainer();
/** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */
$entity_type_manager = $container->get('entity_type.manager');
$entity_type_definition = $entity_type_manager->getDefinition($entity_type);
$entity_type_class = $entity_type_definition->getClass();
$fields = call_user_func([$entity_type_class, 'baseFieldDefinitions'], $entity_type_definition);
if (!empty($fields['changed'])) {
  // Do something.
| improve this answer | |
  • You can get the entity type manager directly from \Drupal by using the entityTypeManager method. \Drupal->entityTypeManager() – Eyal Apr 12 '16 at 6:21
  • Yes, but usually you'd dependency inject either the container or the entityTypeManager service into your service. – masterchief Apr 12 '16 at 22:21
  • Do you know what is the best practice in dependency injection? Container or multiple devices? (I have used the second option) – Eyal Apr 13 '16 at 4:56
  • @Eyal I inject each individual service as that makes more sense from a class depending on something. It also means you can swap those things out easily without changing implementation code and makes unit testing easier. – masterchief Apr 13 '16 at 22:36

If you know the class of the entity type then:

$fields = Foo::baseFieldDefinitions(\Drupal::entityTypeManager()->getDefinition('foo'));
| improve this answer | |
  • This was specifically how to get it from a string id. Thanks though. – masterchief Apr 12 '16 at 22:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.