Repository navigation
Expand file tree
/
Copy pathtaxonomy.api.php
More file actions
310 lines (289 loc) · 9.42 KB
/
Copy pathtaxonomy.api.php
File metadata and controls
310 lines (289 loc) · 9.42 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
<?php
use Drupal\Core\Entity\EntityInterface;
/**
* @file
* Hooks provided by the Taxonomy module.
*/
/**
* @addtogroup hooks
* @{
*/
/**
* Act on a newly created vocabulary.
*
* This hook runs after a new vocabulary object has just been instantiated. It
* can be used to set initial values, e.g. to provide defaults.
*
* @param \Drupal\taxonomy\Plugin\Core\Entity\Vocabulary $vocabulary
* The vocabulary object.
*/
function hook_taxonomy_vocabulary_create(\Drupal\taxonomy\Plugin\Core\Entity\Vocabulary $vocabulary) {
if (!isset($vocabulary->foo)) {
$vocabulary->foo = NULL;
}
}
/**
* Act on taxonomy vocabularies when loaded.
*
* Modules implementing this hook can act on the vocabulary objects before they
* are returned by entity_load_multiple().
*
* @param array $vocabularies
* An array of taxonomy vocabulary entities.
*/
function hook_taxonomy_vocabulary_load(array $vocabularies) {
$result = db_select('mytable', 'm')
->fields('m', array('vid', 'foo'))
->condition('m.vid', array_keys($vocabularies), 'IN')
->execute();
foreach ($result as $record) {
$vocabularies[$record->vid]->foo = $record->foo;
}
}
/**
* Act on taxonomy vocabularies before they are saved.
*
* Modules implementing this hook can act on the vocabulary object before it is
* inserted or updated.
*
* @param Drupal\taxonomy\Plugin\Core\Entity\Vocabulary $vocabulary
* A taxonomy vocabulary entity.
*/
function hook_taxonomy_vocabulary_presave(Drupal\taxonomy\Plugin\Core\Entity\Vocabulary $vocabulary) {
$vocabulary->foo = 'bar';
}
/**
* Act on taxonomy vocabularies when inserted.
*
* Modules implementing this hook can act on the vocabulary object when saved
* to the database.
*
* @param Drupal\taxonomy\Plugin\Core\Entity\Vocabulary $vocabulary
* A taxonomy vocabulary entity.
*/
function hook_taxonomy_vocabulary_insert(Drupal\taxonomy\Plugin\Core\Entity\Vocabulary $vocabulary) {
if ($vocabulary->id() == 'my_vocabulary') {
$vocabulary->weight = 100;
}
}
/**
* Act on taxonomy vocabularies when updated.
*
* Modules implementing this hook can act on the vocabulary object when updated.
*
* @param Drupal\taxonomy\Plugin\Core\Entity\Vocabulary $vocabulary
* A taxonomy vocabulary entity.
*/
function hook_taxonomy_vocabulary_update(Drupal\taxonomy\Plugin\Core\Entity\Vocabulary $vocabulary) {
db_update('mytable')
->fields(array('foo' => $vocabulary->foo))
->condition('vid', $vocabulary->id())
->execute();
}
/**
* Act before taxonomy vocabulary deletion.
*
* This hook is invoked before entity_bundle_delete() is called and before
* the vocabulary is actually removed.
*
* @param Drupal\taxonomy\Plugin\Core\Entity\Vocabulary $vocabulary
* The taxonomy vocabulary entity that is about to be deleted.
*
* @see hook_taxonomy_vocabulary_delete()
*/
function hook_taxonomy_vocabulary_predelete(Drupal\taxonomy\Plugin\Core\Entity\Vocabulary $vocabulary) {
db_delete('mytable_index')
->condition('vid', $vocabulary->id())
->execute();
}
/**
* Respond to taxonomy vocabulary deletion.
*
* This hook is invoked after entity_bundle_delete() has been called and after
* the vocabulary has been removed.
*
* @param Drupal\taxonomy\Plugin\Core\Entity\Vocabulary $vocabulary
* The taxonomy vocabulary entity that has been deleted.
*
* @see hook_taxonomy_vocabulary_predelete()
*/
function hook_taxonomy_vocabulary_delete(Drupal\taxonomy\Plugin\Core\Entity\Vocabulary $vocabulary) {
db_delete('mytable')
->condition('vid', $vocabulary->id())
->execute();
}
/**
* Act on a newly created term.
*
* This hook runs after a new term object has just been instantiated. It can be
* used to set initial values, e.g. to provide defaults.
*
* @param \Drupal\taxonomy\Plugin\Core\Entity\Term $term
* The term object.
*/
function hook_taxonomy_term_create(\Drupal\taxonomy\Plugin\Core\Entity\Term $term) {
if (!isset($term->foo)) {
$term->foo = 'some_initial_value';
}
}
/**
* Act on taxonomy terms when loaded.
*
* Modules implementing this hook can act on the term objects returned by
* entity_load_multiple().
*
* For performance reasons, information to be added to term objects should be
* loaded in a single query for all terms where possible.
*
* Since terms are stored and retrieved from cache during a page request, avoid
* altering properties provided by the {taxonomy_term_data} table, since this
* may affect the way results are loaded from cache in subsequent calls.
*
* @param array $terms
* An array of taxonomy term entities, indexed by tid.
*/
function hook_taxonomy_term_load(array $terms) {
$result = db_select('mytable', 'm')
->fields('m', array('tid', 'foo'))
->condition('m.tid', array_keys($terms), 'IN')
->execute();
foreach ($result as $record) {
$terms[$record->tid]->foo = $record->foo;
}
}
/**
* Act on taxonomy terms before they are saved.
*
* Modules implementing this hook can act on the term object before it is
* inserted or updated.
*
* @param Drupal\taxonomy\Term $term
* A taxonomy term entity.
*/
function hook_taxonomy_term_presave(Drupal\taxonomy\Term $term) {
$term->foo = 'bar';
}
/**
* Act on taxonomy terms when inserted.
*
* Modules implementing this hook can act on the term object when saved to
* the database.
*
* @param Drupal\taxonomy\Term $term
* A taxonomy term entity.
*/
function hook_taxonomy_term_insert(Drupal\taxonomy\Term $term) {
db_insert('mytable')
->fields(array(
'tid' => $term->id(),
'foo' => $term->foo,
))
->execute();
}
/**
* Act on taxonomy terms when updated.
*
* Modules implementing this hook can act on the term object when updated.
*
* @param Drupal\taxonomy\Term $term
* A taxonomy term entity.
*/
function hook_taxonomy_term_update(Drupal\taxonomy\Term $term) {
db_update('mytable')
->fields(array('foo' => $term->foo))
->condition('tid', $term->id())
->execute();
}
/**
* Act before taxonomy term deletion.
*
* This hook is invoked from taxonomy term deletion before
* field_attach_delete() is called and before the term is actually removed from
* the database.
*
* @param Drupal\taxonomy\Term $term
* The taxonomy term entity that is about to be deleted.
*/
function hook_taxonomy_term_predelete(Drupal\taxonomy\Term $term) {
db_delete('mytable_index')
->condition('tid', $term->id())
->execute();
}
/**
* Respond to taxonomy term deletion.
*
* This hook is invoked from taxonomy term deletion after field_attach_delete()
* has been called and after the term has been removed from the database.
*
* @param Drupal\taxonomy\Term $term
* The taxonomy term entity that has been deleted.
*/
function hook_taxonomy_term_delete(Drupal\taxonomy\Term $term) {
db_delete('mytable')
->condition('tid', $term->id())
->execute();
}
/**
* Act on a taxonomy term that is being assembled before rendering.
*
* The module may add elements to $term->content prior to rendering. The
* structure of $term->content is a renderable array as expected by
* drupal_render().
*
* @param \Drupal\taxonomy\Plugin\Core\Entity\Term $term
* The term that is being assembled for rendering.
* @param \Drupal\entity\Plugin\Core\Entity\EntityDisplay $display
* The entity_display object holding the display options configured for the
* term components.
* @param $view_mode
* The $view_mode parameter from taxonomy_term_view().
* @param $langcode
* The language code used for rendering.
*
* @see hook_entity_view()
*/
function hook_taxonomy_term_view(\Drupal\taxonomy\Plugin\Core\Entity\Term $term, \Drupal\entity\Plugin\Core\Entity\EntityDisplay $display, $view_mode, $langcode) {
// Only do the extra work if the component is configured to be displayed.
// This assumes a 'mymodule_addition' extra field has been defined for the
// vocabulary in hook_field_extra_fields().
if ($display->getComponent('mymodule_addition')) {
$term->content['mymodule_addition'] = array(
'#markup' => mymodule_addition($term),
'#theme' => 'mymodule_my_additional_field',
);
}
}
/**
* Alter the results of taxonomy_term_view().
*
* This hook is called after the content has been assembled in a structured
* array and may be used for doing processing which requires that the complete
* taxonomy term content structure has been built.
*
* If the module wishes to act on the rendered HTML of the term rather than the
* structured content array, it may use this hook to add a #post_render
* callback. Alternatively, it could also implement
* hook_preprocess_HOOK() for taxonomy-term.tpl.php. See drupal_render() and
* theme() documentation respectively for details.
*
* @param $build
* A renderable array representing the taxonomy term content.
* @param \Drupal\taxonomy\Plugin\Core\Entity\Term $term
* The taxonomy term being rendered.
* @param \Drupal\entity\Plugin\Core\Entity\EntityDisplay $display
* The entity_display object holding the display options configured for the
* term components.
*
* @see hook_entity_view_alter()
*/
function hook_taxonomy_term_view_alter(&$build, \Drupal\taxonomy\Plugin\Core\Entity\Term $term, \Drupal\entity\Plugin\Core\Entity\EntityDisplay $display) {
if ($build['#view_mode'] == 'full' && isset($build['an_additional_field'])) {
// Change its weight.
$build['an_additional_field']['#weight'] = -10;
}
// Add a #post_render callback to act on the rendered HTML of the term.
$build['#post_render'][] = 'my_module_taxonomy_term_post_render';
}
/**
* @} End of "addtogroup hooks".
*/