You are here

function entity_entity_property_context in Entity API 7

Return a new context based on an existing context.

1 string reference to 'entity_entity_property_context'
entity_property.inc in ctools/relationships/entity_property.inc
Plugin to provide an relationship handler for any entity property.

File

ctools/relationships/entity_property.inc, line 29
Plugin to provide an relationship handler for any entity property.

Code

function entity_entity_property_context($context, $conf) {
  $plugin = $conf['name'];

  // If unset it wants a generic, unfilled context, which is just NULL.
  if (empty($context->data)) {
    return ctools_context_create_empty(isset($conf['target_context']) ? $conf['target_context'] : 'entity', NULL);
  }
  list($part1, $entity_type) = explode(':', $context->plugin);
  if (isset($context->data) && ($entity = $context->data)) {

    // Apply the selector.
    $wrapper = entity_metadata_wrapper($entity_type, $entity);
    $parts = explode(':', $conf['selector']);
    try {
      foreach ($parts as $part) {
        if (!($wrapper instanceof EntityStructureWrapper || $wrapper instanceof EntityListWrapper)) {
          $wrapper = NULL;
          $value = NULL;
          continue;
        }
        $wrapper = $wrapper
          ->get($part);
      }
    } catch (EntityMetadataWrapperException $e) {
      $wrapper = NULL;
      $value = NULL;
    }
    if (isset($wrapper)) {
      $value = $wrapper
        ->value();
    }

    // Massage list values.
    if ($wrapper instanceof EntityListWrapper) {
      $value = $wrapper[0]
        ->value();
      $argument = implode($conf['concatenator'], $wrapper
        ->value(array(
        'identifier' => TRUE,
      )));
    }
    elseif ($wrapper instanceof EntityDrupalWrapper) {
      $argument = $wrapper
        ->getIdentifier();
    }

    // Bail out if we were unable to retrieve the value.
    if (!isset($value)) {
      return ctools_context_create_empty(isset($conf['target_context']) ? $conf['target_context'] : 'entity', NULL);
    }
    $context = ctools_context_create($conf['target_context'], $value);

    // Provide a suiting argument if context is used as argument.
    if (isset($argument)) {
      $context->argument = $argument;
    }
    return $context;
  }
}