metatag.api.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. <?php
  2. /**
  3. * @file
  4. * API documentation for the Metatag module.
  5. */
  6. /**
  7. * Provides a default configuration for Metatag intances.
  8. *
  9. * This hook allows modules to provide their own Metatag instances which can
  10. * either be used as-is or as a "starter" for users to build from.
  11. *
  12. * This hook should be placed in MODULENAME.metatag.inc and it will be auto-
  13. * loaded. MODULENAME.metatag.inc *must* be in the same directory as the
  14. * .module file which *must* also contain an implementation of
  15. * hook_ctools_plugin_api, preferably with the same code as found in
  16. * metatag_ctools_plugin_api().
  17. *
  18. * The $config->disabled boolean attribute indicates whether the Metatag
  19. * instance should be enabled (FALSE) or disabled (TRUE) by default.
  20. *
  21. * @return array
  22. * An associative array containing the structures of Metatag instances, as
  23. * generated from the Export tab, keyed by the Metatag config name.
  24. *
  25. * @see metatag_metatag_config_default()
  26. * @see metatag_ctools_plugin_api()
  27. */
  28. function hook_metatag_config_default() {
  29. $configs = array();
  30. $config = new stdClass();
  31. $config->instance = 'config1';
  32. $config->api_version = 1;
  33. $config->disabled = FALSE;
  34. $config->config = array(
  35. 'title' => array('value' => '[current-page:title] | [site:name]'),
  36. 'generator' => array('value' => 'Drupal 7 (https://www.drupal.org)'),
  37. 'canonical' => array('value' => '[current-page:url:absolute]'),
  38. 'shortlink' => array('value' => '[current-page:url:unaliased]'),
  39. );
  40. $configs[$config->instance] = $config;
  41. $config = new stdClass();
  42. $config->instance = 'config2';
  43. $config->api_version = 1;
  44. $config->disabled = FALSE;
  45. $config->config = array(
  46. 'title' => array('value' => '[user:name] | [site:name]'),
  47. );
  48. $configs[$config->instance] = $config;
  49. return $configs;
  50. }
  51. /**
  52. * Allow the exported configurations to be changed prior to being cached.
  53. */
  54. function hook_metatag_config_default_alter(&$config) {
  55. }
  56. /**
  57. * Internal hook for adding further configuration values in bundled submodules.
  58. *
  59. * The defaults provided by the main Metatag module need to be extended by the
  60. * bundled submodules before they are presented to other modules for altering
  61. * via hook_metatag_config_default_alter(), in case differences in module
  62. * weights and loading priorities cause the submodules' settings to run after
  63. * those of any custom modules.
  64. *
  65. * @see hook_metatag_config_default()
  66. * @see hook_metatag_config_default_alter()
  67. */
  68. function hook_metatag_bundled_config_alter(&$config) {
  69. }
  70. /**
  71. * Allow modules to act upon the record insertion.
  72. */
  73. function hook_metatag_config_instance_info() {
  74. return array();
  75. }
  76. /**
  77. * Alter record insertion provided by modules with the previous hook.
  78. *
  79. * @see hook_metatag_config_instance_info()
  80. */
  81. function hook_metatag_config_instance_info_alter(&$info) {
  82. }
  83. /**
  84. * Allow the Metatag config instance name to be changed.
  85. *
  86. * By default, Metatag module define config instance per entity bundle, making
  87. * it impossible to store set of meta tags based on other criteria. Combining
  88. * this hook with hook_metatag_config_instance_info_alter() it is possible to
  89. * alter the logic of how instances are named based on any custom criteria.
  90. *
  91. * @param string $instance
  92. * The name of the instance generated by default.
  93. * @param object $entity
  94. * The entity object to generate the Metatag instance name for.
  95. * @param string $entity_type
  96. * The entity type of the entity.
  97. * @param string $bundle
  98. * The bundle of the entity.
  99. */
  100. function hook_metatag_get_entity_metatags_instance_alter(&$instance, $entity, $entity_type, $bundle) {
  101. if ($entity_type == 'user') {
  102. // Split config instances based on user roles.
  103. foreach (array_reverse(user_roles(), TRUE) as $rid => $role) {
  104. if ($rid == DRUPAL_AUTHENTICATED_RID) {
  105. continue;
  106. }
  107. if (isset($entity->roles[$rid])) {
  108. $instance = 'user:' . $role;
  109. break;
  110. }
  111. }
  112. }
  113. }
  114. /**
  115. * Never triggered?
  116. *
  117. * @todo Confirm whether this still exists.
  118. */
  119. function hook_metatag_config_load() {
  120. }
  121. /**
  122. * Never triggered?
  123. *
  124. * @todo Confirm whether this still exists.
  125. */
  126. function hook_metatag_config_load_presave() {
  127. }
  128. /**
  129. * Allow a Metatag configuration to be modified prior to being saved.
  130. *
  131. * @param object $config
  132. * The configuration object that is about to be saved.
  133. */
  134. function hook_metatag_config_presave($config) {
  135. }
  136. /**
  137. * Triggered when a Metatag configuration is created.
  138. *
  139. * @param object $config
  140. * The configuration object that was created.
  141. */
  142. function hook_metatag_config_insert($config) {
  143. }
  144. /**
  145. * Triggered when a Metatag configuration is updated.
  146. *
  147. * @param object $config
  148. * The configuration object that was modified.
  149. */
  150. function hook_metatag_config_update($config) {
  151. }
  152. /**
  153. * Triggered when a Metatag configuration is removed.
  154. *
  155. * @param object $config
  156. * The name of the configuration object that was removed.
  157. */
  158. function hook_metatag_config_delete($config) {
  159. }
  160. /**
  161. * Definition of the meta tags and groups.
  162. *
  163. * @return array
  164. * A nested array of 'tags' and 'groups', each keyed off the machine name for
  165. * their respective structure type, with the following values:
  166. * Tags:
  167. * 'label' - The name for this meta tag.
  168. * 'description' - An explanation of what this meta tag is used for and what
  169. * values are permissible.
  170. * 'class' - The class name that controls this meta tag.
  171. * 'weight' - Used to sort the meta tags during output.
  172. * 'group' - The machine name of a group this meta tag will be contained
  173. * within.
  174. * 'context' - Optionally control the type of configuration the meta tag
  175. * will be available from. Possible values are:
  176. * [empty] - All meta tags apply to all possible objects, by default.
  177. * 'global' - This will make it only show in the global meta tag
  178. * configuration form.
  179. * [entity type] - Makes the meta tag only show for specific entity types.
  180. * 'header' - Optionally output the meta tag as an HTTP header value.
  181. * 'element' - Optional attributes for rendering the meta tag. Should
  182. * contain the following:
  183. * '#theme' - The theming function used to render the meta tag.
  184. * 'replaces' - An optional array of meta tags that this meta tag replaces.
  185. * Used to indicate that these deprecated meta tags will be replaced by
  186. * this newer one, their values will be used, upon the next object save
  187. * the deprecated tag will be entirely replaced by the new meta tag. While
  188. * one meta tag can replace several others, only one of the possible
  189. * values will be used, the others will be silently purged upon the next
  190. * configuration/object save.
  191. * 'multiple' - If set to TRUE the output will be comma-separated and output
  192. * as multiple tags.
  193. * 'image' - If set to TRUE some additional effort will be added to attempt
  194. * extracting image URLs from the value. Currently limited to matching
  195. * the default output of core image theming, i.e. the following string:
  196. * src="[URL]" width=
  197. * 'url' - If set to TRUE, relative paths will be converted to absolute.
  198. * 'is_language' - If set to TRUE, will not allow the Drupal default
  199. * language value "und" to be output.
  200. * 'maxlength' - If set to a numeric value, meta tag values will be trimmed
  201. * to this limit, which may be overridden via the settings page. Note: any
  202. * meta tags with this value assigned can have its maxlength controlled,
  203. * so setting the attribute to an empty string will allow site builders to
  204. * adjust the meta tag lengths as needed.
  205. * 'select_or_other' - If set to TRUE, form[#type] is set to 'select' and
  206. * the "select_or_other" module is available, that module will be used to
  207. * provide a text field to manually insert another option.
  208. * 'form' - Optional items to be passed directly to the form; uses standard
  209. * Form API values.
  210. * 'devel_generate' - Optional values to be passed to the Devel Generate
  211. * submodule. Should be an array containing one of the following values:
  212. * 'type' - One of the following:
  213. * 'canonical' - The token for the absolute URL for the current page.
  214. * 'email' - An email address randomly generated at the site's hostname.
  215. * 'float' - A random floating point number between 0.0 and 999.999.
  216. * 'image' - A randomly generated image.
  217. * 'integer' - A random integer between 0 and 999.
  218. * 'phone' - A phone number in the format 999-999-9999.
  219. * 'select' - A value randomly selected from the available form options.
  220. * 'text' - Random text string.
  221. * 'twitter' - A Twitter username.
  222. * 'url' - A randomly generated URL on this site.
  223. * 'maxlength' - The maximum length / number of iterations of this value,
  224. * defaults to 10.
  225. * 'dependencies' - Optional nested array of values to indicate other meta
  226. * tags that must be present in order for this meta tag to be visible. See
  227. * The Open Graph and Twitter Cards submodules for example usage. Each
  228. * dependency must contain the following elements:
  229. * 'dependency' - The name of the meta tag that is required.
  230. * 'attribute' - The name of the other meta tag that is to be checked,
  231. * most meta tags use "value" as the attribute name.
  232. * 'condition' - The state condition to be checked against, e.g. "filled"
  233. * to check text values, "checked" for a checkbox, "value" to compare
  234. * the raw selection; see https://api.drupal.org/drupal_process_states
  235. * for more details.
  236. * 'value' - The field value to check the 'condition' against. see
  237. * https://api.drupal.org/drupal_process_states for further details.
  238. * Groups:
  239. * 'label' - The name for this group.
  240. * 'description' - A detailed explanation of these meta tags.
  241. * 'form' - Additional items to be passed directly to the form.
  242. * Note: 'label', 'description', and any text strings passed in 'form', should
  243. * be translated.
  244. *
  245. * @see metatag_metatag_info()
  246. */
  247. function hook_metatag_info() {
  248. return array();
  249. }
  250. /**
  251. * Alter record insertion provided by modules with the previous hook.
  252. *
  253. * @see hook_metatag_info()
  254. */
  255. function hook_metatag_info_alter(&$info) {
  256. }
  257. /**
  258. * Alter metatags before being cached.
  259. *
  260. * This hook is invoked prior to the meta tags for a given page are cached.
  261. *
  262. * @param array $output
  263. * All of the meta tags to be output for this page in their raw format. This
  264. * is a heavily nested array.
  265. * @param string $instance
  266. * An identifier for the current page's page type, typically a combination
  267. * of the entity name and bundle name, e.g. "node:story".
  268. * @param array $options
  269. * All of the options used to generate the meta tags.
  270. */
  271. function hook_metatag_metatags_view_alter(&$output, $instance, $options) {
  272. if (isset($output['description']['#attached']['drupal_add_html_head'][0][0]['#value'])) {
  273. $output['description']['#attached']['drupal_add_html_head'][0][0]['#value'] = 'O rly?';
  274. }
  275. }
  276. /**
  277. * Allow other modules to customize the data to generate the cache ID.
  278. */
  279. function hook_metatag_page_cache_cid_parts_alter(&$cid_parts) {
  280. }
  281. /**
  282. * Allow other modules to alter the meta tags prior to saving.
  283. */
  284. function hook_metatag_presave(&$metatags, $entity_type, $entity_id, $revision_id, $langcode) {
  285. }
  286. /**
  287. * Allows modules to alter the defined list of tokens available
  288. * for metatag patterns replacements.
  289. *
  290. * By default only context (for example: global, node, etc...)
  291. * related tokens are made available to metatag patterns replacements.
  292. * This hook allows other modules to extend the default declared tokens.
  293. *
  294. * @param array $options
  295. * (optional) An array of options including the following keys and values:
  296. * - token types: An array of token types to be passed to theme_token_tree().
  297. * - context: An identifier for the configuration instance type, typically
  298. * an entity name or object name, e.g. node, views, taxonomy_term.
  299. *
  300. * @see metatag_config_edit_form()
  301. * @see metatag_field_attach_form()
  302. */
  303. function hook_metatag_token_types_alter(&$options) {
  304. // Watchout: $options['token types'] might be empty.
  305. if (!isset($options['token types'])) {
  306. $options['token types'] = array();
  307. }
  308. if ($options['context'] == 'config1') {
  309. $options['token types'] += array('token_type1', 'token_type2');
  310. }
  311. elseif ($options['context'] == 'config2') {
  312. $options['token types'] += array('token_type3', 'token_type4');
  313. }
  314. }
  315. /**
  316. * Allows modules to alter defined token patterns and values before replacement.
  317. *
  318. * The metatag module defines default token patterns replacements depending on
  319. * the different configuration instances (contexts, such as global, node, ...).
  320. * This hook provides an opportunity for other modules to alter the patterns or
  321. * the values for replacements, before tokens are replaced (token_replace).
  322. *
  323. * See facetapi and facetapi_bonus modules for an example of implementation.
  324. *
  325. * @param string $pattern
  326. * A string potentially containing replaceable tokens. The pattern could also
  327. * be altered by reference, allowing modules to implement further logic, such
  328. * as tokens lists or masks/filters.
  329. * @param array $types
  330. * Corresponds to the 'token data' property of the $options object.
  331. * (optional) An array of keyed objects. For simple replacement scenarios
  332. * 'node', 'user', and others are common keys, with an accompanying node or
  333. * user object being the value. Some token types, like 'site', do not require
  334. * any explicit information from $data and can be replaced even if it is
  335. * empty.
  336. * @param string $tag_name
  337. * The name of the meta tag being altered.
  338. *
  339. * @see DrupalTextMetaTag::getValue()
  340. */
  341. function hook_metatag_pattern_alter(&$pattern, &$types, $tag_name) {
  342. if (strpos($pattern, 'token_type1') !== FALSE) {
  343. $types['token_type1'] = "data to be used in hook_tokens for replacement";
  344. }
  345. if (strpos($pattern, 'token_type2') !== FALSE) {
  346. // Load something or do some operations.
  347. $types['token_type2'] = array("Then fill in the array with the right data");
  348. // $pattern could also be altered, for example, strip off [token_type3].
  349. $pattern = str_replace('[token_type3]', '', $pattern);
  350. }
  351. }
  352. /**
  353. * Allow modules to override whether entity types are enabled for use.
  354. *
  355. * By default the system only support entities that are not configuration
  356. * entities, have multiple view modes (excluding those created by the ical,
  357. * diff and token modules), are fieldable, and are not one of the following:
  358. * - field_collection_item (from the Field Collection module)
  359. * - paragraphs_item (from the Paragraphs module)
  360. *
  361. * @param bool $suitable
  362. * Whether or not the entity type is enabled for use with Metatag.
  363. * @param string $entity_type
  364. * The machine name of the entity type.
  365. * @param array $entity_info
  366. * The full specifications for this entity type, as returned by
  367. * entity_get_info().
  368. */
  369. function hook_metatag_entity_type_is_supported_alter(&$suitable, $entity_type, $entity_info) {
  370. // Enable Metatag support for a custom entity that might otherwise be
  371. // ignored, e.g. it doesn't allow fields.
  372. if ($entity_type == 'my_entity') {
  373. $suitable = TRUE;
  374. }
  375. }
  376. /**
  377. * Identify the entity type provided by a specific view.
  378. *
  379. * When a view is used to display a page it can be difficult to identify what
  380. * entity type is being managed. Use this hook to inform Metatag what type of
  381. * entity is being displayed.
  382. *
  383. * @param object $view
  384. * The view object being displayed.
  385. *
  386. * @return string|NULL
  387. * Should return a string indicating an entity type that will be paired with
  388. * the views' first argument ($view->args[0]) to load that entity.
  389. */
  390. function hook_metatag_views_post_render_get_entity($view) {
  391. $display = $view->display[$view->current_display];
  392. if ($display->display_options['path'] == 'coolpage/%') {
  393. return 'my_entity';
  394. }
  395. }
  396. /**
  397. * Allow the context string being passed to i18n_string to be changed before it
  398. * is used.
  399. *
  400. * If the string is set to an empty value it will cause this meta tag to not
  401. * be translated.
  402. *
  403. * @param string $context
  404. * The context string being passed into i18n_string. Will usually be in the
  405. * format "[category]:[path-identifier]", e.g. "[node:123]", "[page:contact]",
  406. * etc.
  407. * @param string $tag_name
  408. * The name of the meta tag being translated.
  409. */
  410. function hook_metatag_i18n_context_alter(&$context, $tag_name) {
  411. // Don't bother translating the canonical URL.
  412. if ($tag_name == 'canonical') {
  413. $context = '';
  414. }
  415. }
  416. /**
  417. * Allow modules to overide the expiration of metatag caches.
  418. *
  419. * By default Metatag caches everything as CACHE_PERMANENT, this alter allows to
  420. * change that.
  421. *
  422. * @param $expire
  423. * The expire value to change.
  424. * @param $cid
  425. * The cid about to be cached.
  426. * @param $data
  427. * The data to be cached.
  428. */
  429. function hook_metatag_cache_set_expire_alter(&$expire, $cid, $data) {
  430. $expire = CACHE_TEMPORARY;
  431. }