diff --git a/ctools/relationships/entity_property.inc b/ctools/relationships/entity_property.inc new file mode 100644 index 0000000..9207665 --- /dev/null +++ b/ctools/relationships/entity_property.inc @@ -0,0 +1,152 @@ + t('Entity property or field (via Entity Metadata Wrapper)'), + 'description' => t('Creates any kind of context for entity properties and fields.'), + 'context' => 'entity_entity_property_context', + 'required context' => new ctools_context_required(t('Entity'), 'entity'), + 'edit form' => 'entity_entity_property_edit_form', + 'edit form validate' => 'entity_entity_property_edit_form_validate', + 'defaults' => array( + 'selector' => '', + 'target_context' => 'entity', + 'concatenator' => ',' + ), +); + +/** + * Return a new context based on an existing context. + */ +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(); + } + + // 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); + } + + // 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(); + } + $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; + } +} + +function entity_entity_property_edit_form($form, &$form_state) { + $conf = $form_state['conf']; + + $form['selector'] = array( + '#type' => 'textfield', + '#title' => t('Data selector'), + '#description' => t('Any valid data selector, e.g. "title" to select a node\'s title, or "field_tags:1" to select the second tag.'), + '#default_value' => $conf['selector'], + '#required' => TRUE, + ); + $form['concatenator'] = array( + '#title' => t('Concatenator (if multiple)'), + '#type' => 'select', + '#options' => array(',' => ', (AND)', '+' => '+ (OR)'), + '#default_value' => $conf['concatenator'], + '#prefix' => '