1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Node count functionality for Simple hierarchical select.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Implements hook_field_widget_info_alter().
|
10
|
*/
|
11
|
function shs_node_count_field_widget_info_alter(&$info) {
|
12
|
if (empty($info['taxonomy_shs'])) {
|
13
|
return;
|
14
|
}
|
15
|
// Add "a"node_count" setting to a shs widgets.
|
16
|
$info['taxonomy_shs']['settings']['shs'] += array(
|
17
|
'node_count' => FALSE,
|
18
|
);
|
19
|
}
|
20
|
|
21
|
/**
|
22
|
* Implements hook_form_FORM_ID_alter().
|
23
|
*/
|
24
|
function shs_node_count_form_field_ui_field_edit_form_alter(&$form, &$form_state, $form_id) {
|
25
|
if (empty($form['instance']['widget']['settings']['shs'])) {
|
26
|
return;
|
27
|
}
|
28
|
$settings_form = &$form['instance']['widget']['settings']['shs'];
|
29
|
$settings = &$form['#instance']['widget']['settings']['shs'];
|
30
|
|
31
|
$settings_form['node_count'] = array(
|
32
|
'#type' => 'checkbox',
|
33
|
'#title' => t('Display number of nodes'),
|
34
|
'#description' => t('Display the number of nodes associated with the term.'),
|
35
|
'#default_value' => empty($settings['node_count']) ? FALSE : $settings['node_count'],
|
36
|
'#weight' => -1,
|
37
|
);
|
38
|
}
|
39
|
|
40
|
/**
|
41
|
* Implements hook_shs_term_get_children_alter().
|
42
|
*/
|
43
|
function shs_node_count_shs_term_get_children_alter(&$terms, &$alter_options) {
|
44
|
if (empty($alter_options['settings']['node_count']) || empty($alter_options['settings']['language']) || !isset($alter_options['parent'])) {
|
45
|
// Nothing to do here.
|
46
|
return;
|
47
|
}
|
48
|
$langcode = $alter_options['settings']['language']->language;
|
49
|
$term = (object) array(
|
50
|
'vid' => $alter_options['vid'],
|
51
|
'tid' => 0,
|
52
|
);
|
53
|
$format = variable_get('shs_node_count_format', '%s (%d)');
|
54
|
foreach ($terms as &$item) {
|
55
|
array_walk($item[$langcode][$alter_options['parent']], function (&$name, $key) use ($term, $format) {
|
56
|
$term->tid = $key;
|
57
|
$count = shs_node_count_term_get_node_count($term, TRUE);
|
58
|
$name = sprintf($format, $name, $count);
|
59
|
});
|
60
|
}
|
61
|
}
|
62
|
|
63
|
/**
|
64
|
* Helper function to count number of nodes associated to a term.
|
65
|
*
|
66
|
* @param object $term
|
67
|
* The term object.
|
68
|
* @param bool $count_children
|
69
|
* If set to TRUE, nodes in child terms are counted also.
|
70
|
*
|
71
|
* @return int
|
72
|
* Number of nodes within the term.
|
73
|
*/
|
74
|
function shs_node_count_term_get_node_count($term, $count_children = FALSE) {
|
75
|
$num_nodes = &drupal_static(__FUNCTION__, array());
|
76
|
|
77
|
// Maybe this needs some more caching and value-updates on node_save()/
|
78
|
// _update()/delete().
|
79
|
if (empty($num_nodes["{$term->tid}:{$count_children}"])) {
|
80
|
$index_table = 'taxonomy_index';
|
81
|
if (module_exists('taxonomy_entity_index')) {
|
82
|
$index_table = 'taxonomy_entity_index';
|
83
|
}
|
84
|
|
85
|
// Count nodes associated to this term.
|
86
|
$num_nodes["{$term->tid}:{$count_children}"] = db_select($index_table, 'ti')
|
87
|
->fields('ti')
|
88
|
->condition('tid', $term->tid)
|
89
|
->execute()
|
90
|
->rowCount();
|
91
|
|
92
|
if ($count_children) {
|
93
|
$tids = array();
|
94
|
$tree = taxonomy_get_tree($term->vid, $term->tid);
|
95
|
foreach ($tree as $child_term) {
|
96
|
$tids[] = $child_term->tid;
|
97
|
}
|
98
|
if (count($tids)) {
|
99
|
$num_nodes["{$term->tid}:{$count_children}"] += db_select($index_table, 'ti')
|
100
|
->fields('ti')
|
101
|
->condition('tid', $tids, 'IN')
|
102
|
->execute()
|
103
|
->rowCount();
|
104
|
}
|
105
|
}
|
106
|
}
|
107
|
|
108
|
return isset($num_nodes["{$term->tid}:{$count_children}"]) ? $num_nodes["{$term->tid}:{$count_children}"] : 0;
|
109
|
}
|