diff --git a/metatag.admin.inc b/metatag.admin.inc index 0b4278a..42a43a9 100644 --- a/metatag.admin.inc +++ b/metatag.admin.inc @@ -281,6 +281,8 @@ function metatag_config_edit_form($form, &$form_state, $config) { else { $options['token types'] = array($contexts[0]); } + // Allow hook_metatag_token_types_alter() to modify the defined tokens. + drupal_alter('metatag_token_types', $options); } // Ensure that this configuration is properly compared to its parent 'default' diff --git a/metatag.api.php b/metatag.api.php index b2d97dd..9667092 100644 --- a/metatag.api.php +++ b/metatag.api.php @@ -217,3 +217,70 @@ function hook_metatag_page_cache_cid_parts_alter(&$cid_parts) { */ function hook_metatag_presave(&$metatags, $entity_type, $entity_id) { } + +/** + * 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) { + // Watchout: $options['token types'] might be empty + if (!isset($options['token types'])) { + $options['token types'] = array(); + } + + 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 $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 $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. + * + * @see DrupalTextMetaTag::getValue() + */ +function hook_metatag_pattern_alter(&$pattern, &$types) { + 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); + } +}