-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtaxonomy_override.module
147 lines (139 loc) · 5.01 KB
/
taxonomy_override.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
module_load_include('inc', 'taxonomy_override', 'taxonomy_override.queries');
/*
* Overrides taxonomy default callback.
*/
function taxonomy_override_menu_alter(&$items) {
$items['taxonomy/term/%taxonomy_term']['page callback'] = '_taxonomy_override_taxonomy_override_pages';
// Optional
$items['taxonomy/term/%taxonomy_term']['title callback'] = '_taxonomy_override_taxonomy_override_title';
$items['taxonomy/term/%taxonomy_term']['file'] = 'taxonomy_override.module';
$items['taxonomy/term/%taxonomy_term']['module'] = 'taxonomy_override';
$items['taxonomy/term/%taxonomy_term']['access arguments'] = array('access content');
}
/**
*
* Taxonomy callback page to modify display's behavior,
* on a taxonomy term page.
*
* @param $term Term concerned by page display.
*/
function _taxonomy_override_taxonomy_override_pages($tid = '') {
if (is_object($tid)) {
$tid = $tid->tid;
}
$term = taxonomy_term_load($tid);
$vocabulary = taxonomy_vocabulary_load($term->vid);
/*
* Retrieve eventually associated callback with machine name.
*/
$association = _taxonomy_override_get_association_by_machine_name($vocabulary->machine_name);
if (!empty($association) && $association['callback_function']) {
$callback = $association['callback_function'];
if (function_exists($callback)) {
$out = $callback($tid);
if ($out) {
if (module_exists('context')) {
// Fire Context behavior if Context is enabled.
$plugin = context_get_plugin('condition', 'taxonomy_term');
$plugin->execute($term, 'view');
}
return $out;
}
else {
drupal_set_message(t('Callback function doesn\'t return any content. Please check this page\'s callback function and return either HTML or renderable array.'), 'warning');
}
}
else {
drupal_set_message(t('Callback associated with this page doesn\'t seem to exist. Default taxonomy term page has been displayed by default.'), 'warning');
}
}
/*
* No specific behavior : we load classic taxonomy page view.
*/
module_load_include('inc','taxonomy','taxonomy.pages');
return taxonomy_term_page($term);
}
/*
* Optional title overriding.
*/
function _taxonomy_override_taxonomy_override_title($term) {
return function_exists('i18n_taxonomy_term_name') ?
i18n_taxonomy_term_name($term) : taxonomy_term_title($term);
}
/**
* Gets all available callbacks
* @return array
*/
function taxonomy_override_get_available_callbacks() {
$callbacks = &drupal_static(__FUNCTION__);
if (!isset($callbacks)) {
$callbacks = module_invoke_all('taxonomy_override_define_callback');
}
return $callbacks;
}
function taxonomy_override_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'taxonomy_form_vocabulary') {
// do not alter on deletion
if (isset($form_state['confirm_delete']) && isset($form_state['values']['vid'])) {
return;
}
$form['taxonomy_override'] = array(
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#title' => t('Taxonomy Override'),
'#weight' => 15
);
$availableCallbacks = taxonomy_override_get_available_callbacks();
$availableCallbacks = array_combine($availableCallbacks, $availableCallbacks);
$currentMachineName = $form['machine_name']['#default_value'];
$currentAssociation = _taxonomy_override_get_association_by_machine_name($currentMachineName);
array_unshift($availableCallbacks, t('-Default-'));
$form['taxonomy_override'] = array(
'#type' => 'select',
'#options' => $availableCallbacks,
'#title' => t('Callback display to use')
);
if (!empty($currentAssociation) && $currentAssociation['callback_function']) {
$form['taxonomy_override']['#default_value'] = $currentAssociation['callback_function'];
}
$form['#submit'][] = '_taxonomy_override_vocabulary_form_submit';
}
}
/**
* Vocabulary form's submit handler in order to manage callback
* modification.
*/
function _taxonomy_override_vocabulary_form_submit($form, &$form_state) {
$callback = $form_state['values']['taxonomy_override'];
$machine_name = $form_state['values']['machine_name'];
$oldAssoc = _taxonomy_override_get_association_by_machine_name($machine_name);
if ((!$callback && empty($oldAssoc)) || $oldAssoc['callback_function'] == $callback) {
return;
}
/*
* We add associated callback with machine name of taxonomy.
* If a callback already exists, we update it.
*/
if ($callback) {
if (empty($oldAssoc)) {
_taxonomy_override_add_association($machine_name, $callback);
}
else {
_taxonomy_override_update_association($machine_name, $callback);
}
}
else {
/*
* Callback 0 => Default value => We delete the association.
*/
_taxonomy_overide_delete_association_by_machine_name($machine_name);
}
}
/**
* Implements hook_taxonomy_vocabulary_delete().
*/
function taxonomy_override_taxonomy_vocabulary_delete($vocabulary) {
_taxonomy_overide_delete_association_by_machine_name($vocabulary->machine_name);
}