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 |
|
|
'!label' => $instance['label'],
|
47 |
|
|
'!field_name' => $instance['field_name'],
|
48 |
|
|
'!entity_type' => $instance['entity_type'],
|
49 |
|
|
'!bundle' => $instance['bundle']
|
50 |
|
|
)
|
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 |
|
|
if (isset($items[$delta]['value'])) {
|
82 |
|
|
$field_collection_item = field_collection_item_load($items[$delta]['value']);
|
83 |
|
|
}
|
84 |
|
|
|
85 |
|
|
return ctools_context_create('entity:field_collection_item', $items[$delta]['value']);
|
86 |
|
|
}
|
87 |
|
|
|
88 |
|
|
return ctools_context_create_empty('entity:field_collection_item', NULL);
|
89 |
|
|
}
|
90 |
|
|
|
91 |
|
|
/**
|
92 |
|
|
* Settings form.
|
93 |
|
|
*/
|
94 |
|
|
function field_collection_field_collection_from_field_edit_form($form, &$form_state) {
|
95 |
|
|
$conf = $form_state['conf'];
|
96 |
|
|
|
97 |
|
|
$form['delta'] = array(
|
98 |
|
|
'#type' => 'textfield',
|
99 |
|
|
'#title' => t('Delta'),
|
100 |
|
|
'#size' => 3,
|
101 |
|
|
'#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.'),
|
102 |
|
|
'#default_value' => empty($conf['delta']) ? 0 : $conf['delta'],
|
103 |
|
|
);
|
104 |
|
|
|
105 |
|
|
return $form;
|
106 |
|
|
} |