1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Plugin to provide an relationship handler for all terms from node.
|
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('Multiple terms from node'),
|
14
|
'keyword' => 'terms',
|
15
|
'description' => t('Adds a taxonomy terms from a node context; if multiple terms are selected, they wil be concatenated.'),
|
16
|
'required context' => new ctools_context_required(t('Node'), 'node'),
|
17
|
'context' => 'ctools_terms_from_node_context',
|
18
|
'edit form' => 'ctools_terms_from_node_settings_form',
|
19
|
'defaults' => array('vocabulary' => array(), 'concatenator' => ','),
|
20
|
);
|
21
|
|
22
|
/**
|
23
|
* Return a new context based on an existing context.
|
24
|
*/
|
25
|
function ctools_terms_from_node_context($context, $conf) {
|
26
|
// If unset it wants a generic, unfilled context, which is just NULL.
|
27
|
if (empty($context->data)) {
|
28
|
return ctools_context_create_empty('terms', NULL);
|
29
|
}
|
30
|
|
31
|
// Collect all terms for the chosen vocabulary and concatenate them.
|
32
|
$node = $context->data;
|
33
|
$terms = array();
|
34
|
|
35
|
$fields = field_info_instances('node', $node->type);
|
36
|
foreach ($fields as $name => $info) {
|
37
|
$field_info = field_info_field($name);
|
38
|
if ($field_info['type'] == 'taxonomy_term_reference' && (empty($conf['vocabulary']) || !empty($conf['vocabulary'][$field_info['settings']['allowed_values'][0]['vocabulary']]))) {
|
39
|
$items = field_get_items('node', $node, $name);
|
40
|
if (is_array($items)) {
|
41
|
foreach ($items as $item) {
|
42
|
$terms[] = $item['tid'];
|
43
|
}
|
44
|
}
|
45
|
}
|
46
|
elseif ($field_info['type'] == 'entityreference' && $field_info['settings']['target_type'] == 'taxonomy_term') {
|
47
|
$items = field_get_items('node', $node, $name);
|
48
|
if (is_array($items)) {
|
49
|
$tids = array();
|
50
|
foreach ($items as $item) {
|
51
|
$tids[] = $item['target_id'];
|
52
|
}
|
53
|
|
54
|
$term_objects = taxonomy_term_load_multiple($tids);
|
55
|
foreach ($term_objects as $term) {
|
56
|
if (empty($conf['vocabulary']) || in_array($term->vocabulary_machine_name, $conf['vocabulary'])) {
|
57
|
$terms[] = $term->tid;
|
58
|
}
|
59
|
}
|
60
|
}
|
61
|
}
|
62
|
}
|
63
|
|
64
|
if (!empty($terms)) {
|
65
|
$all_terms = ctools_break_phrase(implode($conf['concatenator'], $terms));
|
66
|
return ctools_context_create('terms', $all_terms);
|
67
|
}
|
68
|
}
|
69
|
|
70
|
/**
|
71
|
* Settings form for the relationship.
|
72
|
*/
|
73
|
function ctools_terms_from_node_settings_form($form, &$form_state) {
|
74
|
$conf = $form_state['conf'];
|
75
|
|
76
|
$options = array();
|
77
|
foreach (taxonomy_vocabulary_get_names() as $name => $vocabulary) {
|
78
|
$options[$name] = $vocabulary->name;
|
79
|
}
|
80
|
$form['vocabulary'] = array(
|
81
|
'#title' => t('Vocabulary'),
|
82
|
'#type' => 'checkboxes',
|
83
|
'#options' => $options,
|
84
|
'#default_value' => $conf['vocabulary'],
|
85
|
'#prefix' => '<div class="clearfix">',
|
86
|
'#suffix' => '</div>',
|
87
|
);
|
88
|
$form['concatenator'] = array(
|
89
|
'#title' => t('Concatenator'),
|
90
|
'#type' => 'select',
|
91
|
'#options' => array(',' => ', (AND)', '+' => '+ (OR)'),
|
92
|
'#default_value' => $conf['concatenator'],
|
93
|
'#prefix' => '<div class="clearfix">',
|
94
|
'#suffix' => '</div>',
|
95
|
'#description' => t("When the value from this context is passed on to a view as argument, the terms can be concatenated in the form of 1+2+3 (for OR) or 1,2,3 (for AND)."),
|
96
|
);
|
97
|
|
98
|
return $form;
|
99
|
}
|