1 |
31a5a6d6
|
Assos Assos
|
<?php
|
2 |
|
|
|
3 |
|
|
/**
|
4 |
|
|
* @file
|
5 |
|
|
* Plugin to provide a relationship handler for a field collection field.
|
6 |
|
|
*/
|
7 |
|
|
|
8 |
|
|
// Plugin definition.
|
9 |
|
|
$plugin = array(
|
10 |
|
|
'title' => t('Field collection item'),
|
11 |
|
|
'description' => t('Creates an entity context from a field collection field on a field.'),
|
12 |
|
|
'context' => 'field_collection_field_collection_from_field_context',
|
13 |
|
|
'edit form' => 'field_collection_field_collection_from_field_edit_form',
|
14 |
|
|
'get child' => 'field_collection_field_collection_from_field_get_child',
|
15 |
|
|
'get children' => 'field_collection_field_collection_from_field_get_children',
|
16 |
|
|
'defaults' => array('delta' => 0),
|
17 |
|
|
);
|
18 |
|
|
|
19 |
|
|
/**
|
20 |
|
|
* Get child callback.
|
21 |
|
|
*/
|
22 |
|
|
function field_collection_field_collection_from_field_get_child($plugin, $parent, $child) {
|
23 |
|
|
$plugins = field_collection_field_collection_from_field_get_children($plugin, $parent);
|
24 |
|
|
return $plugins[$parent . ':' . $child];
|
25 |
|
|
}
|
26 |
|
|
|
27 |
|
|
/**
|
28 |
|
|
* Get children callback.
|
29 |
|
|
*/
|
30 |
|
|
function field_collection_field_collection_from_field_get_children($plugin, $parent) {
|
31 |
|
|
|
32 |
|
|
$plugins = array();
|
33 |
|
|
$instances_info = field_info_instances();
|
34 |
|
|
if (isset($instances_info['field_collection_item'])) {
|
35 |
|
|
$field_collection_items = $instances_info['field_collection_item'];
|
36 |
|
|
|
37 |
|
|
foreach (field_read_instances() as $instance) {
|
38 |
|
|
if (isset($field_collection_items[$instance['field_name']])) {
|
39 |
|
|
$child_plugin_id = $parent . ':' . $instance['entity_type'] . ':' . $instance['bundle'] . ':' . $instance['field_name'];
|
40 |
|
|
|
41 |
|
|
$child_plugin = $plugin;
|
42 |
|
|
$child_plugin['context name'] = $instance['entity_type'] . ':' . $instance['bundle'] . ':' . $instance['field_name'];
|
43 |
|
|
$child_plugin['title'] = t(
|
44 |
|
|
'!label field collection (!field_name) from !entity_type (!bundle)',
|
45 |
|
|
array(
|
46 |
950416da
|
Assos Assos
|
'!label' => $instance['label'],
|
47 |
|
|
'!field_name' => $instance['field_name'],
|
48 |
|
|
'!entity_type' => $instance['entity_type'],
|
49 |
|
|
'!bundle' => $instance['bundle'],
|
50 |
31a5a6d6
|
Assos Assos
|
)
|
51 |
|
|
);
|
52 |
|
|
$restrictions = array('type' => array($instance['bundle']));
|
53 |
|
|
$child_plugin['required context'] = new ctools_context_required(ucfirst($instance['entity_type']), $instance['entity_type'], $restrictions);
|
54 |
|
|
$child_plugin['parent'] = $parent;
|
55 |
|
|
$child_plugin['keyword'] = 'Field collection';
|
56 |
|
|
$child_plugin['entity_type'] = $instance['entity_type'];
|
57 |
|
|
$child_plugin['field_name'] = $instance['field_name'];
|
58 |
|
|
|
59 |
|
|
$child_plugin['name'] = $child_plugin_id;
|
60 |
|
|
$plugins[$child_plugin_id] = $child_plugin;
|
61 |
|
|
|
62 |
|
|
}
|
63 |
|
|
}
|
64 |
|
|
}
|
65 |
|
|
|
66 |
|
|
return $plugins;
|
67 |
|
|
}
|
68 |
|
|
|
69 |
|
|
/**
|
70 |
|
|
* Return a new field collection context based on an existing context.
|
71 |
|
|
*/
|
72 |
|
|
function field_collection_field_collection_from_field_context($context, $conf) {
|
73 |
|
|
|
74 |
|
|
$plugin_info = ctools_get_relationship($conf['name']);
|
75 |
|
|
$delta = (int) $conf['delta'];
|
76 |
|
|
|
77 |
|
|
$entity = $context->data;
|
78 |
|
|
if (isset($entity->{$plugin_info['field_name']})) {
|
79 |
|
|
|
80 |
|
|
$items = field_get_items($plugin_info['entity_type'], $entity, $plugin_info['field_name']);
|
81 |
a1cafe7e
|
Assos Assos
|
|
82 |
|
|
// Use negative delta to get item from the end.
|
83 |
|
|
if ($delta < 0) {
|
84 |
|
|
// Add negative pseudo-delta to total amount of items to get the real
|
85 |
|
|
// delta. Example (field_collection with 5 elements): count() == 5:
|
86 |
|
|
// 5 + -1 = 4, which would be the last element in this example.
|
87 |
|
|
$delta = count($items) + $delta;
|
88 |
|
|
}
|
89 |
|
|
|
90 |
950416da
|
Assos Assos
|
if (!empty($items) && isset($items[$delta]['value'])) {
|
91 |
5e632cae
|
Assos Assos
|
return ctools_context_create('entity:field_collection_item', $items[$delta]['value']);
|
92 |
31a5a6d6
|
Assos Assos
|
}
|
93 |
|
|
}
|
94 |
|
|
|
95 |
|
|
return ctools_context_create_empty('entity:field_collection_item', NULL);
|
96 |
|
|
}
|
97 |
|
|
|
98 |
|
|
/**
|
99 |
|
|
* Settings form.
|
100 |
|
|
*/
|
101 |
|
|
function field_collection_field_collection_from_field_edit_form($form, &$form_state) {
|
102 |
|
|
$conf = $form_state['conf'];
|
103 |
|
|
|
104 |
|
|
$form['delta'] = array(
|
105 |
|
|
'#type' => 'textfield',
|
106 |
|
|
'#title' => t('Delta'),
|
107 |
|
|
'#size' => 3,
|
108 |
a1cafe7e
|
Assos Assos
|
'#description' => t('The relationship can only create one context, but multiple items can be related. Please type in the number you want. The first one will be 0. Use negative delta to get items from the end. The last item will be -1'),
|
109 |
31a5a6d6
|
Assos Assos
|
'#default_value' => empty($conf['delta']) ? 0 : $conf['delta'],
|
110 |
|
|
);
|
111 |
|
|
|
112 |
|
|
return $form;
|
113 |
|
|
} |