You are here

function ctools_context_create_entity in Chaos Tool Suite (ctools) 7

It's important to remember that $conf is optional here, because contexts are not always created from the UI.

1 string reference to 'ctools_context_create_entity'
entity.inc in plugins/contexts/entity.inc
Plugin to provide a node context. A node context is a node wrapped in a context object that can be utilized by anything that accepts contexts.

File

plugins/contexts/entity.inc, line 58
Plugin to provide a node context. A node context is a node wrapped in a context object that can be utilized by anything that accepts contexts.

Code

function ctools_context_create_entity($empty, $data = NULL, $conf = FALSE, $plugin) {
  $entity_type = $plugin['keyword'];
  $entity = entity_get_info($entity_type);
  $context = new ctools_context(array(
    'entity:' . $entity_type,
    'entity',
    $entity_type,
  ));
  $context->plugin = $plugin['name'];
  $context->keyword = $entity_type;
  if ($empty) {
    return $context;
  }

  // Attempt to retain compatibility with broken id:
  if (is_array($data) && !isset($data['entity_id']) && isset($data['id'])) {
    $id = $data['id'];
  }
  elseif (is_array($data) && isset($data['entity_id'])) {
    $id = $data['entity_id'];
  }
  elseif (is_object($data)) {
    $ids = entity_extract_ids($entity_type, $data);
    $id = $ids[0];
  }
  elseif (is_numeric($data)) {
    $id = $data;
    $data = entity_load($entity_type, array(
      $id,
    ));
    $data = !empty($data[$id]) ? $data[$id] : FALSE;
  }
  if (is_array($data)) {
    $data = entity_load($entity_type, array(
      $id,
    ));
    $data = !empty($data[$id]) ? $data[$id] : FALSE;
  }
  if (!empty($data)) {
    $context->data = $data;
    if (!empty($entity['entity keys']['label'])) {
      $context->title = $data->{$entity['entity keys']['label']};
    }
    $context->argument = $id;
    if ($entity['entity keys']['bundle']) {
      $context->restrictions['type'] = array(
        $data->{$entity['entity keys']['bundle']},
      );
    }
    return $context;
  }
}