0

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?

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

4 Answers 4

6

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";
}

Edit:

The method has moved to the service EntityFieldManager:

$base_fields = \Drupal::service('entity_field.manager')
  ->getBaseFieldDefinitions($entity_type_id);
9
  • EntityManager is deprecated
    – Eyal
    Commented Apr 12, 2016 at 10:12
  • EntityManager is used 2500 times in core. For me it seems ok to use it.
    – 4uk4
    Commented Apr 12, 2016 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
    Commented Apr 12, 2016 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.
    – 4uk4
    Commented Apr 12, 2016 at 11:45
  • The methods in entity manager are all synthetic sugar to the new implantations. They exist only for backwards compatibility.
    – Eyal
    Commented Apr 12, 2016 at 11:47
0

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.
}
4
  • You can get the entity type manager directly from \Drupal by using the entityTypeManager method. \Drupal->entityTypeManager()
    – Eyal
    Commented Apr 12, 2016 at 6:21
  • Yes, but usually you'd dependency inject either the container or the entityTypeManager service into your service. Commented Apr 12, 2016 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
    Commented Apr 13, 2016 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. Commented Apr 13, 2016 at 22:36
0

If you know the class of the entity type then:

$fields = Foo::baseFieldDefinitions(\Drupal::entityTypeManager()->getDefinition('foo'));
1
  • This was specifically how to get it from a string id. Thanks though. Commented Apr 12, 2016 at 22:21
0

Here's an example for drush, sample $entity_type_id is user but could be node etc..

drush ev 'var_export(array_keys(\Drupal::service("entity_field.manager")->getBaseFieldDefinitions("user")))'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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