1 |
59e46405
|
Assos Assos
|
<?php
|
2 |
|
|
|
3 |
|
|
/**
|
4 |
|
|
* @file
|
5 |
|
|
* Plugin to provide an relationship handler for any entity property.
|
6 |
|
|
*/
|
7 |
|
|
|
8 |
|
|
/**
|
9 |
|
|
* Plugins are described by creating a $plugin array which will be used
|
10 |
|
|
* by the system that includes this file.
|
11 |
|
|
*/
|
12 |
|
|
$plugin = array(
|
13 |
|
|
'title' => t('Entity property or field (via Entity Metadata Wrapper)'),
|
14 |
|
|
'description' => t('Creates any kind of context for entity properties and fields.'),
|
15 |
|
|
'context' => 'entity_entity_property_context',
|
16 |
|
|
'required context' => new ctools_context_required(t('Entity'), 'entity'),
|
17 |
|
|
'edit form' => 'entity_entity_property_edit_form',
|
18 |
|
|
'edit form validate' => 'entity_entity_property_edit_form_validate',
|
19 |
|
|
'defaults' => array(
|
20 |
|
|
'selector' => '',
|
21 |
|
|
'target_context' => 'entity',
|
22 |
|
|
'concatenator' => ','
|
23 |
|
|
),
|
24 |
|
|
);
|
25 |
|
|
|
26 |
|
|
/**
|
27 |
|
|
* Return a new context based on an existing context.
|
28 |
|
|
*/
|
29 |
|
|
function entity_entity_property_context($context, $conf) {
|
30 |
|
|
$plugin = $conf['name'];
|
31 |
|
|
|
32 |
|
|
// If unset it wants a generic, unfilled context, which is just NULL.
|
33 |
|
|
if (empty($context->data)) {
|
34 |
|
|
return ctools_context_create_empty(isset($conf['target_context']) ? $conf['target_context'] : 'entity', NULL);
|
35 |
|
|
}
|
36 |
|
|
|
37 |
|
|
list($part1, $entity_type) = explode(':', $context->plugin);
|
38 |
|
|
|
39 |
|
|
if (isset($context->data) && $entity = $context->data) {
|
40 |
|
|
// Apply the selector.
|
41 |
|
|
$wrapper = entity_metadata_wrapper($entity_type, $entity);
|
42 |
|
|
$parts = explode(':', $conf['selector']);
|
43 |
|
|
|
44 |
|
|
try {
|
45 |
|
|
foreach ($parts as $part) {
|
46 |
|
|
if (!($wrapper instanceof EntityStructureWrapper || $wrapper instanceof EntityListWrapper)) {
|
47 |
|
|
$wrapper = NULL;
|
48 |
|
|
$value = NULL;
|
49 |
|
|
continue;
|
50 |
|
|
}
|
51 |
|
|
$wrapper = $wrapper->get($part);
|
52 |
|
|
}
|
53 |
|
|
}
|
54 |
|
|
catch (EntityMetadataWrapperException $e) {
|
55 |
|
|
$wrapper = NULL;
|
56 |
|
|
$value = NULL;
|
57 |
|
|
}
|
58 |
|
|
|
59 |
|
|
if (isset($wrapper)) {
|
60 |
|
|
$value = $wrapper->value();
|
61 |
|
|
}
|
62 |
|
|
|
63 |
|
|
// Massage list values.
|
64 |
|
|
if ($wrapper instanceof EntityListWrapper) {
|
65 |
|
|
$value = $wrapper[0]->value();
|
66 |
|
|
$argument = implode($conf['concatenator'], $wrapper->value(array('identifier' => TRUE)));
|
67 |
|
|
}
|
68 |
|
|
elseif ($wrapper instanceof EntityDrupalWrapper) {
|
69 |
|
|
$argument = $wrapper->getIdentifier();
|
70 |
|
|
}
|
71 |
|
|
|
72 |
|
|
// Bail out if we were unable to retrieve the value.
|
73 |
|
|
if (!isset($value)) {
|
74 |
|
|
return ctools_context_create_empty(isset($conf['target_context']) ? $conf['target_context'] : 'entity', NULL);
|
75 |
|
|
}
|
76 |
|
|
|
77 |
|
|
$context = ctools_context_create($conf['target_context'], $value);
|
78 |
|
|
// Provide a suiting argument if context is used as argument.
|
79 |
|
|
if (isset($argument)) {
|
80 |
|
|
$context->argument = $argument;
|
81 |
|
|
}
|
82 |
|
|
return $context;
|
83 |
|
|
}
|
84 |
|
|
}
|
85 |
|
|
|
86 |
|
|
function entity_entity_property_edit_form($form, &$form_state) {
|
87 |
|
|
$conf = $form_state['conf'];
|
88 |
|
|
|
89 |
|
|
$form['selector'] = array(
|
90 |
|
|
'#type' => 'textfield',
|
91 |
|
|
'#title' => t('Data selector'),
|
92 |
|
|
'#description' => t('Any valid data selector, e.g. "title" to select a node\'s title, or "field_tags:1" to select the second tag.'),
|
93 |
|
|
'#default_value' => $conf['selector'],
|
94 |
|
|
'#required' => TRUE,
|
95 |
|
|
);
|
96 |
|
|
$form['concatenator'] = array(
|
97 |
|
|
'#title' => t('Concatenator (if multiple)'),
|
98 |
|
|
'#type' => 'select',
|
99 |
|
|
'#options' => array(',' => ', (AND)', '+' => '+ (OR)'),
|
100 |
|
|
'#default_value' => $conf['concatenator'],
|
101 |
|
|
'#prefix' => '<div class="clearfix">',
|
102 |
|
|
'#suffix' => '</div>',
|
103 |
|
|
'#description' => t("When the resulting value is multiple valued and the context is passed on to a view as argument, the value can be concatenated in the form of 1+2+3 (for OR) or 1,2,3 (for AND)."),
|
104 |
|
|
);
|
105 |
|
|
return $form;
|
106 |
|
|
}
|
107 |
|
|
|
108 |
|
|
function entity_entity_property_edit_form_validate($form, &$form_state) {
|
109 |
|
|
$context_key = $form_state['values']['context'];
|
110 |
|
|
$context = $form_state['contexts'][$context_key];
|
111 |
|
|
$entity_type = $context->type[2];
|
112 |
|
|
|
113 |
|
|
try {
|
114 |
|
|
$all_properties = entity_get_all_property_info($entity_type);
|
115 |
|
|
$wrapper = entity_metadata_wrapper($entity_type, NULL, array('property info' => $all_properties));
|
116 |
|
|
$parts = explode(':', $form_state['values']['selector']);
|
117 |
|
|
foreach ($parts as $part) {
|
118 |
|
|
if (!($wrapper instanceof EntityStructureWrapper || $wrapper instanceof EntityListWrapper)) {
|
119 |
|
|
form_set_error('selector', t('Unable to apply the data selector part %key.'. array('%key' => $part)));
|
120 |
|
|
continue;
|
121 |
|
|
}
|
122 |
|
|
$wrapper = $wrapper->get($part);
|
123 |
|
|
}
|
124 |
|
|
$type = entity_entity_property_map_data_type($wrapper->type());
|
125 |
|
|
$form_state['conf']['target_context'] = $type;
|
126 |
|
|
}
|
127 |
|
|
catch (EntityMetadataWrapperException $e) {
|
128 |
|
|
form_set_error('selector', t('Unable to apply the data selector on entity type %type. @reason', array('@reason' => $e->getMessage(), '%type' => $entity_type)));
|
129 |
|
|
}
|
130 |
|
|
|
131 |
|
|
// Auto-generate a sane identifier.
|
132 |
|
|
if ($form_state['values']['identifier'] == $form['identifier']['#default_value']) {
|
133 |
|
|
$form_state['values']['identifier'] = 'Entity property ' . $entity_type . ':' . check_plain($form_state['values']['selector']);
|
134 |
|
|
}
|
135 |
|
|
}
|
136 |
|
|
|
137 |
|
|
/**
|
138 |
|
|
* Maps an entity-property data type to ctools types.
|
139 |
|
|
*/
|
140 |
|
|
function entity_entity_property_map_data_type($type) {
|
141 |
|
|
// Remove list<> wrappers from types.
|
142 |
|
|
while ($item_type = entity_property_list_extract_type($type)) {
|
143 |
|
|
$type = $item_type;
|
144 |
|
|
}
|
145 |
|
|
// Massage data type of entites to c
|
146 |
|
|
if (entity_get_info($type)) {
|
147 |
|
|
$type = "entity:$type";
|
148 |
|
|
}
|
149 |
|
|
if ($type == 'text') {
|
150 |
|
|
$type = 'string';
|
151 |
|
|
}
|
152 |
|
|
return $type;
|
153 |
|
|
} |