1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Plugin to provide an relationship handler for term parent.
|
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('Term parent'),
|
14
|
'keyword' => 'parent_term',
|
15
|
'description' => t('Adds a taxonomy term parent from a term context.'),
|
16
|
'required context' => new ctools_context_required(t('Term'), 'entity:taxonomy_term'),
|
17
|
'context' => 'ctools_term_parent_context',
|
18
|
'edit form' => 'ctools_term_parent_settings_form',
|
19
|
'defaults' => array('type' => 'top'),
|
20
|
);
|
21
|
|
22
|
/**
|
23
|
* Return a new context based on an existing context.
|
24
|
*/
|
25
|
function ctools_term_parent_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('entity:taxonomy_term');
|
29
|
}
|
30
|
|
31
|
if (isset($context->data)) {
|
32
|
$result = db_query('SELECT t1.* FROM {taxonomy_term_hierarchy} t1 INNER JOIN {taxonomy_term_hierarchy} t2 ON t1.tid = t2.parent WHERE t2.tid = :tid', array(':tid' => $context->data->tid))->fetchAssoc();
|
33
|
|
34
|
// If top level term, keep looking up until we see a top level.
|
35
|
if ($conf['type'] == 'top') {
|
36
|
// If looking for top level, and there are no parents at all, make sure
|
37
|
// the current term is the 'top level'.
|
38
|
if (empty($result)) {
|
39
|
$result['tid'] = $context->data->tid;
|
40
|
}
|
41
|
while (!empty($result['parent'])) {
|
42
|
$result = db_query('SELECT * FROM {taxonomy_term_hierarchy} WHERE tid = :tid', array(':tid' => $result['parent']))->fetchAssoc();
|
43
|
}
|
44
|
}
|
45
|
|
46
|
// Load the term.
|
47
|
if ($result) {
|
48
|
$term = taxonomy_term_load($result['tid']);
|
49
|
return ctools_context_create('entity:taxonomy_term', $term);
|
50
|
}
|
51
|
}
|
52
|
}
|
53
|
|
54
|
/**
|
55
|
* Settings form for the relationship.
|
56
|
*/
|
57
|
function ctools_term_parent_settings_form($form, &$form_state) {
|
58
|
$conf = $form_state['conf'];
|
59
|
|
60
|
$form['type'] = array(
|
61
|
'#type' => 'select',
|
62
|
'#title' => t('Relationship type'),
|
63
|
'#options' => array('parent' => t('Immediate parent'), 'top' => t('Top level term')),
|
64
|
'#default_value' => $conf['type'],
|
65
|
);
|
66
|
|
67
|
return $form;
|
68
|
}
|