-
Notifications
You must be signed in to change notification settings - Fork 15
/
metatag.api.php
413 lines (389 loc) · 15 KB
/
metatag.api.php
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
<?php
/**
* @file
* API documentation for the Metatag module.
*/
/**
* The function hook_metatag_config_default() is not necessary in Backdrop CMS.
*
* To provide default configurations for metatag configs, simply place a config
* JSON file into the 'config' directory within the module. The file will be
* copied over to the active directory when the module is enabled.
*/
/**
* Allows the default configuration to be changed prior to being cached.
*/
function hook_metatag_config_default_alter(&$config) {
if ($config['instance'] == 'test') {
$config['config']['title'] = '[current-page:title] | foo bar';
}
}
/**
* Internal hook for adding further configuration values in bundled submodules.
*
* The defaults provided by the main Metatag module need to be extended by the
* bundled submodules before they are presented to other modules for altering
* via hook_metatag_config_default_alter(), in case differences in module
* weights and loading priorities cause the submodules' settings to run after
* those of any custom modules.
*
* @see hook_metatag_config_default_alter()
*/
function hook_metatag_bundled_config_alter(&$config) {
if ($config['instance'] == 'test') {
$config['config']['title'] = '[current-page:title] | foo bar baz';
}
}
/**
* Allow modules to act upon the record insertion.
*/
function hook_metatag_config_instance_info() {
return array();
}
/**
* Alter record insertion provided by modules with the previous hook.
*
* @see hook_metatag_config_instance_info()
*/
function hook_metatag_config_instance_info_alter(&$info) {
}
/**
* Allow the Metatag config instance name to be changed.
*
* By default, Metatag module define config instance per entity bundle, making
* it impossible to store set of meta tags based on other criteria. Combining
* this hook with hook_metatag_config_instance_info_alter() it is possible to
* alter the logic of how instances are named based on any custom criteria.
*
* @param string $instance
* The name of the instance generated by default.
* @param object $entity
* The entity object to generate the Metatag instance name for.
* @param string $entity_type
* The entity type of the entity.
* @param string $bundle
* The bundle of the entity.
*/
function hook_metatag_get_entity_metatags_instance_alter(&$instance, $entity, $entity_type, $bundle) {
if ($entity_type == 'user') {
// Split config instances based on user roles.
foreach (array_reverse(user_roles(), TRUE) as $rid => $role) {
if ($rid == BACKDROP_AUTHENTICATED_ROLE) {
continue;
}
if (isset($entity->roles[$rid])) {
$instance = 'user:' . $role;
break;
}
}
}
}
/**
* Never triggered?
*
* @todo Confirm whether this still exists.
*/
function hook_metatag_config_load() {
}
/**
* Never triggered?
*
* @todo Confirm whether this still exists.
*/
function hook_metatag_config_load_presave() {
}
/**
* Allow a Metatag configuration to be modified prior to being saved.
*
* @param object $config
* The configuration object that is about to be saved.
*/
function hook_metatag_config_presave($config) {
}
/**
* Triggered when a Metatag configuration is created.
*
* @param array $data
* An array of configuration data that has been saved.
*/
function hook_metatag_config_insert(array $data) {
}
/**
* Triggered when a Metatag configuration is updated.
*
* @param array $data
* An array of configuration data that has been saved.
*/
function hook_metatag_config_update(array $data) {
}
/**
* Triggered when a Metatag configuration is removed.
*
* @param array $data
* An array of configuration data that was removed.
*/
function hook_metatag_config_delete(array $data) {
}
/**
* Definition of the meta tags and groups.
*
* @return array
* A nested array of 'tags' and 'groups', each keyed off the machine name for
* their respective structure type, with the following values:
* Tags:
* 'label' - The name for this meta tag.
* 'description' - An explanation of what this meta tag is used for and what
* values are permissible.
* 'class' - The class name that controls this meta tag.
* 'weight' - Used to sort the meta tags during output.
* 'group' - The machine name of a group this meta tag will be contained
* within.
* 'context' - Optionally control the type of configuration the meta tag
* will be available from. Possible values are:
* [empty] - All meta tags apply to all possible objects, by default.
* 'global' - This will make it only show in the global meta tag
* configuration form.
* [entity type] - Makes the meta tag only show for specific entity types.
* 'header' - Optionally output the meta tag as an HTTP header value.
* 'element' - Optional attributes for rendering the meta tag. Should
* contain the following:
* '#theme' - The theming function used to render the meta tag.
* 'replaces' - An optional array of meta tags that this meta tag replaces.
* Used to indicate that these deprecated meta tags will be replaced by
* this newer one, their values will be used, upon the next object save
* the deprecated tag will be entirely replaced by the new meta tag. While
* one meta tag can replace several others, only one of the possible
* values will be used, the others will be silently purged upon the next
* configuration/object save.
* 'multiple' - If set to TRUE the output will be comma-separated and output
* as multiple tags.
* 'image' - If set to TRUE some additional effort will be added to attempt
* extracting image URLs from the value. Currently limited to matching
* the default output of core image theming, i.e. the following string:
* src="[URL]" width=
* 'url' - If set to TRUE, relative paths will be converted to absolute.
* 'is_language' - If set to TRUE, will not allow the Backdrop default
* language value "und" to be output.
* 'select_or_other' - If set to TRUE, form[#type] is set to 'select' and
* the "select_or_other" module is available, that module will be used to
* provide a text field to manually insert another option.
* 'form' - Optional items to be passed directly to the form; uses standard
* Form API values.
* 'devel_generate' - Optional values to be passed to the Devel Generate
* submodule. Should be an array containing one of the following values:
* 'type' - One of the following:
* 'canonical' - The token for the absolute URL for the current page.
* 'email' - An email address randomly generated at the site's hostname.
* 'float' - A random floating point number between 0.0 and 999.999.
* 'image' - A randomly generated image.
* 'integer' - A random integer between 0 and 999.
* 'phone' - A phone number in the format 999-999-9999.
* 'select' - A value randomly selected from the available form options.
* 'text' - Random text string.
* 'twitter' - A Twitter username.
* 'url' - A randomly generated URL on this site.
* 'maxlength' - The maximum length / number of iterations of this value,
* defaults to 10.
* 'dependencies' - Optional nested array of values to indicate other meta
* tags that must be present in order for this meta tag to be visible. See
* The Open Graph and Twitter Cards submodules for example usage. Each
* dependency must contain the following elements:
* 'dependency' - The name of the meta tag that is required.
* 'attribute' - The name of the other meta tag that is to be checked,
* most meta tags use "value" as the attribute name.
* 'condition' - The state condition to be checked against, e.g. "filled"
* to check text values, "checked" for a checkbox, "value" to compare
* the raw selection; see https://api.drupal.org/backdrop_process_states
* for more details.
* 'value' - The field value to check the 'condition' against. see
* https://api.drupal.org/backdrop_process_states for further details.
* Groups:
* 'label' - The name for this group.
* 'description' - A detailed explanation of these meta tags.
* 'form' - Additional items to be passed directly to the form.
* Note: 'label', 'description', and any text strings passed in 'form', should
* be translated.
*
* @see metatag_metatag_info()
*/
function hook_metatag_info() {
return array();
}
/**
* Alter record insertion provided by modules with the previous hook.
*
* @see hook_metatag_info()
*/
function hook_metatag_info_alter(&$info) {
}
/**
* Alter metatags before being cached.
*
* This hook is invoked prior to the meta tags for a given page are cached.
*
* @param array $output
* All of the meta tags to be output for this page in their raw format. This
* is a heavily nested array.
* @param string $instance
* An identifier for the current page's page type, typically a combination
* of the entity name and bundle name, e.g. "node:story".
* @param array $options
* All of the options used to generate the meta tags.
*/
function hook_metatag_metatags_view_alter(&$output, $instance, $options) {
if (isset($output['description']['#attached']['backdrop_add_html_head'][0][0]['#value'])) {
$output['description']['#attached']['backdrop_add_html_head'][0][0]['#value'] = 'O rly?';
}
}
/**
* Allow other modules to customize the data to generate the cache ID.
*/
function hook_metatag_page_cache_cid_parts_alter(&$cid_parts) {
}
/**
* Allow other modules to alter the meta tags prior to saving.
*/
function hook_metatag_presave(&$metatags, $entity_type, $entity_id, $revision_id, $langcode) {
}
/**
* Allows modules to alter the defined list of tokens available
* for metatag patterns replacements.
*
* By default, only context (for example: global, node, etc...)
* related tokens are made available to metatag patterns replacements.
* This hook allows other modules to extend the default declared tokens.
*
* @param array $options
* (optional) An array of options including the following keys and values:
* - token types: An array of token types to be passed to theme_token_tree().
* - context: An identifier for the configuration instance type, typically
* an entity name or object name, e.g. node, views, taxonomy_term.
*
* @see metatag_config_edit_form()
* @see metatag_field_attach_form()
*/
function hook_metatag_token_types_alter(&$options) {
if ($options['context'] == 'config1') {
$options['token types'] += array('token_type1', 'token_type2');
}
elseif ($options['context'] == 'config2') {
$options['token types'] += array('token_type3', 'token_type4');
}
}
/**
* Allows modules to alter defined token patterns and values before replacement.
*
* The metatag module defines default token patterns replacements depending on
* the different configuration instances (contexts, such as global, node, ...).
* This hook provides an opportunity for other modules to alter the patterns or
* the values for replacements, before tokens are replaced (token_replace).
*
* See facetapi and facetapi_bonus modules for an example of implementation.
*
* @param string $pattern
* A string potentially containing replaceable tokens. The pattern could also
* be altered by reference, allowing modules to implement further logic, such
* as tokens lists or masks/filters.
* @param array $types
* Corresponds to the 'token data' property of the $options object.
* (optional) An array of keyed objects. For simple replacement scenarios
* 'node', 'user', and others are common keys, with an accompanying node or
* user object being the value. Some token types, like 'site', do not require
* any explicit information from $data and can be replaced even if it is
* empty.
* @param string $tag_name
* The name of the meta tag being altered.
*
* @see BackdropTextMetaTag::getValue()
*/
function hook_metatag_pattern_alter(&$pattern, &$types, $tag_name) {
if (strpos($pattern, 'token_type1') !== FALSE) {
$types['token_type1'] = "data to be used in hook_tokens for replacement";
}
if (strpos($pattern, 'token_type2') !== FALSE) {
// Load something or do some operations.
$types['token_type2'] = array("Then fill in the array with the right data");
// $pattern could also be altered, for example, strip off [token_type3].
$pattern = str_replace('[token_type3]', '', $pattern);
}
}
/**
* Allow modules to override whether entity types are enabled for use.
*
* By default the system only support entities that are not configuration
* entities, have multiple view modes (excluding those created by the ical,
* diff and token modules), are fieldable, and are not one of the following:
* - field_collection_item (from the Field Collection module)
* - paragraphs_item (from the Paragraphs module)
*
* @param bool $suitable
* Whether or not the entity type is enabled for use with Metatag.
* @param string $entity_type
* The machine name of the entity type.
* @param array $entity_info
* The full specifications for this entity type, as returned by
* entity_get_info().
*/
function hook_metatag_entity_type_is_supported_alter(&$suitable, $entity_type, $entity_info) {
// Enable Metatag support for a custom entity that might otherwise be
// ignored, e.g. it doesn't allow fields.
if ($entity_type == 'my_entity') {
$suitable = TRUE;
}
}
/**
* Identify the entity type provided by a specific view.
*
* When a view is used to display a page it can be difficult to identify what
* entity type is being managed. Use this hook to inform Metatag what type of
* entity is being displayed.
*
* @param object $view
* The view object being displayed.
*
* @return string|NULL
* Should return a string indicating an entity type that will be paired with
* the views' first argument ($view->args[0]) to load that entity.
*/
function hook_metatag_views_post_render_get_entity($view) {
$display = $view->display[$view->current_display];
if ($display->display_options['path'] == 'coolpage/%') {
return 'my_entity';
}
}
/**
* Allow the context string being passed to i18n_string to be changed before it
* is used.
*
* If the string is set to an empty value it will cause this meta tag to not
* be translated.
*
* @param string $context
* The context string being passed into i18n_string. Will usually be in the
* format "[category]:[path-identifier]", e.g. "[node:123]", "[page:contact]",
* etc.
* @param string $tag_name
* The name of the meta tag being translated.
*/
function hook_metatag_i18n_context_alter(&$context, $tag_name) {
// Don't bother translating the canonical URL.
if ($tag_name == 'canonical') {
$context = '';
}
}
/**
* Allow modules to overide the expiration of metatag caches.
*
* By default Metatag caches everything as CACHE_PERMANENT, this alter allows to
* change that.
*
* @param $expire
* The expire value to change.
* @param $cid
* The cid about to be cached.
* @param $data
* The data to be cached.
*/
function hook_metatag_cache_set_expire_alter(&$expire, $cid, $data) {
$expire = CACHE_TEMPORARY;
}