1

Is there a generic way of knowing what collection of properties on an entity identify it as a unique instance?

For example, a node has a node id, and a revision id. Some entities do not have the revision id, other entities might require further keys.

Usecase example: Storing various entities in an external system, one wants to be sure to store enough data to be able to uniquely identify the resources in that system.

3 Answers 3

1

I have always assumed that when you do

$entity_info = entity_get_info('foo');
$table = $entity_info['base table'];
$schema = drupal_get_schema($table);

and then examine $schema['primary key'] and $schema['unique keys'] you get a tuple to represent uniqueness.

I would also assume that you can do

$entity_info = entity_get_info('foo');
$keys = $entity_info['entity keys'];

and this would represent a tuple to uniquely identify an entity. However, this gets a little confusing. For example, if you have UUID installed, then there is a uuid and revision uuid in the results. These are not needed within one system, but will guarantee uniqueness between systems.

3
  • I examined $entity_info['entity keys']; but concluded that for nodes, title was listed in there, which appears to mean the keys may be what I need, but they definitely contain other data as well. primary + unique keys might be just it though. Thanks.
    – Letharion
    Dec 8, 2012 at 0:13
  • I spent the last portion of my day working with both entity_get_info() and the schema API, so all of this was fresh in my head. I was also confused why title was listed with the keys for nodes. I ran out of time before I could get to the bottom of it.
    – mpdonadio
    Dec 8, 2012 at 0:37
  • @Letharion The title is reported as label. The entity keys hook_entity_info() is supposed to return are: id, revision, bundle, label, and uuid (Drupal 8). Of those keys, only one univocally identify an entity instance knowing the entity type: id.
    – apaderno
    Dec 8, 2012 at 0:58
1

The documentation for hook_entity_info() reports the following description for the properties the hook needs to return:

  • entity keys: An array describing how the Field API can extract the information it needs from the objects of the type. Elements:

    • id: The name of the property that contains the primary id of the entity. Every entity object passed to the Field API must have this property and its value must be numeric.
    • revision: The name of the property that contains the revision id of the entity. The Field API assumes that all revision ids are unique across all entities of a type. This entry can be omitted if the entities of this type are not versionable.

The primary ID is what univocally identifies an instance of an entity. The revision ID identifies a revision, but it is not necessary for univocally identifying an entity instance. Clearly, the primary ID univocally identifies an instance for which you know the entity type.

Drupal 8 introduces another entity key (uuid), which is described as:

uuid (optional): The name of the property that contains the universally unique identifier of the entity, which is used to distinctly identify an entity across different systems.

I guess it is unique among different entities.
Between the entities that Drupal 7, and Drupal 8 have in common, only the taxonomy vocabulary doesn't define a uuid field; node, user, and taxonomy term define it.

0

Drupal 7

If you have Entity module installed, then try:

entity_get_all_property_info('node');

Drush commands (one of these):

drush eval "var_dump(entity_get_all_property_info('node'));"
drush eval "print var_export(array_keys(entity_get_all_property_info('node')));"

Read more: Listing entity fields

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.