You are here

function ctools_argument_entity_id_context in Chaos Tool Suite (ctools) 7

Discover if this argument gives us the entity we crave.

1 string reference to 'ctools_argument_entity_id_context'
entity_id.inc in plugins/arguments/entity_id.inc
Plugin to provide an argument handler for all entity ids.

File

plugins/arguments/entity_id.inc, line 50
Plugin to provide an argument handler for all entity ids.

Code

function ctools_argument_entity_id_context($arg = NULL, $conf = NULL, $empty = FALSE) {
  $entity_type = explode(':', $conf['name']);
  $entity_type = $entity_type[1];

  // If unset it wants a generic, unfilled context.
  if ($empty) {
    return ctools_context_create_empty('entity:' . $entity_type);
  }

  // We can accept either an entity object or a pure id.
  if (is_object($arg)) {
    return ctools_context_create('entity:' . $entity_type, $arg);
  }

  // Trim spaces and other garbage.
  $arg = trim($arg);
  if (!is_numeric($arg)) {
    $preg_matches = array();
    $match = preg_match('/\\[id: (\\d+)\\]/', $arg, $preg_matches);
    if (!$match) {
      $match = preg_match('/^id: (\\d+)/', $arg, $preg_matches);
    }
    if ($match) {
      $id = $preg_matches[1];
    }
    if (isset($id) && is_numeric($id)) {
      return ctools_context_create('entity:' . $entity_type, $id);
    }
    return FALSE;
  }
  $entities = entity_load($entity_type, array(
    $arg,
  ));
  if (empty($entities)) {
    return FALSE;
  }
  return ctools_context_create('entity:' . $entity_type, reset($entities));
}