This is very similar to this question: How do I get entity_type and entity_id from any entity page?
But I don't know what that asker means by "Entity Page," and in my case, using debug_backtrace (seriously?) doesn't cut it as I'd like this function to actually work from more than 1 scope.

Anyway... I am writing a module that uses numerous Entity Reference fields, and works the same code over several types of entities. The trouble is that in my function scope I don't know the type of entity.

If I pass an Entity object into a function, I can access its type parameter:

function foo ($entity) {
  // bundle is:

But how do I get the "Entity Type" from this object?

For example, I have a dateset entity type with several bundles: class and retreat. When I pass an instance of the class bundle into my function, I can identify that it's a class, and use identifier() to get its ID. But I want my function foo() to also handle node entity types with bundles content_type_a and content_type_b

Given any instance of one of those bundles, foo() can identify the bundle:

function foo ($entity) {
  $entity->type; // class, retreat, content_type_a or content_type_b
  $entity->indentifier(); // numeric ID

But what about getting node vs. dateset? Doesn't $entity know what type of entity it is?

UPDATE: Even though I accepted Yury's answer because it provided a realistic workaround, I think the true answer is that you cannot do this in Drupal. You have to pass around $entity_type anywhere you pass $entity (which really kind of makes the idea of having Entities as objects pretty much worthless... but I think any Drupal developer knows that object oriented concepts in Drupal are just hacked in and not ever truly related to real world object oriented design). See this Drupal issue: http://drupal.org/node/1042822

1 Answer 1


There are various (convoluted) methods for getting the entity type and bundle, but none that I'd consider "acceptable" on a large-scale. What I've done is implemented hook_entity_load() and added the entity_id and entity_type properties on all entities at load-time.

The following code does this, and is part of the Devtools module I maintain:

function hook_entity_load($entities, $type) {
  // add the entity type & id to all entity objects for easy data access
  // from other hooks & modules
  foreach ($entities as $entity) {
    // we don't want to add these entity fields when the entity is being exported into
    // a feature, otherwise the feature will always show as overridden and will not 
    // revert
    if (  preg_match('#admin/structure/features#', $_GET['q'])  // features page
          (                                                     // drush fu command
            isset($_SERVER['SCRIPT_FILENAME']) && 
            preg_match('/drush\.php$/', $_SERVER['SCRIPT_FILENAME']) &&
            isset($_SERVER['argv'][2]) &&
            $_SERVER['argv'][2] == 'fu'
       ) {

    // set entity_type
      $entity_type_field = (!isset($entity->entity_type)) ? 'entity_type' : 'devtools_entity_type';
      $entity->{$entity_type_field} = $type;

    // set entity_id
      $entity_id_field = (!isset($entity->entity_id)) ? 'entity_id' : 'devtools_entity_id';
      list($entity->{$entity_id_field}, $vid, $bundle) = entity_extract_ids($type, $entity);
  • Sidenote: just reviewed the above code and it would make much more sense to have that if() statement outside the loop, and returning the function call. Mar 5, 2013 at 7:25

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.