Upgrade Your Drupal Skills

We trained 1,000+ Drupal Developers over the last decade.

See Advanced Courses NAH, I know Enough

Detect entity type

Default entities (node, user, term etc) in Drupal 7 are instances of a stdClass class. It means you can't determine entity type directly. Let's say you have an object and you want to ensure that it's a node and only then do something node specific. Entity API doesn't provide such possibility out of the box.

When it can be helpful? Well, real life example: custom ctools context plugin which returns some value depends on an object loaded from current panel's url.
<?php

/**
 * API function for context determination.
 */
function module_get_context() {
  ...

  // Load object from current page (page manager).
  // Path could be node/%node, user/%user or term/%term.
  $current_menu_object = menu_get_object('pm_arg', 1);

  // Is it a node? If yes we need to do one operation.
  // If it's a user we need to do something else.
  $object = $current_menu_object->data;

  ...
}
To solve this problem you can use next functions:
<?php

/**
 * API function to detect if passed object is a node.
 *
 * @param object $object
 *   Object.
 *
 * @return bool
 *   TRUE if it's a node FALSE otherwise.
 */
function module_is_node($object) {
  return !empty($object->nid);
}

/**
 * API function to detect if passed object is a term.
 *
 * @param object $object
 *   Object.
 *
 * @return bool
 *   TRUE if it's a term FALSE otherwise.
 */
function module_is_term($object) {
  return !empty($object->tid);
}

/**
 * API function to detect if passed object is an user.
 *
 * @param object $object
 *   Object.
 *
 * @return bool
 *   TRUE if it's an user FALSE otherwise.
 */
function module_is_user($object) {
  return !empty($object->uid);
}
I usually put helper functions into separate module that I create for each project.

So having this functions now you can determine entity type and do some specific actions:
<?php

/**
 * API function for context determination.
 */
function module_get_context() {
  ...

  // Load object from current page (page manager).
  // Path could be node/%node, user/%user or term/%term.
  $current_menu_object = menu_get_object('pm_arg', 1);

  // Is it a node? If yes we need to do one operation.
  // If it's a user we need to do something else.
  $object = $current_menu_object->data;

  if (module_is_node($object)) {
    // Do node specific actions here.
  } elseif (module_is_term($object)) {
    // Do term specific actions here.
  }

  ...
}
Of course you can extend this function set for other entities like file, etc. Or even write one universal function for checking all entity types.

Key notes:

Author: 
Original Post: 

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web