language.api.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * @file
  4. * Hooks provided by the Language module.
  5. */
  6. /**
  7. * @addtogroup hooks
  8. * @{
  9. */
  10. /**
  11. * Define language types.
  12. *
  13. * @return array
  14. * An associative array of language type definitions. The keys are the
  15. * identifiers, which are also used as names for global variables representing
  16. * the types in the bootstrap phase. The values are associative arrays that
  17. * may contain the following elements:
  18. * - name: The human-readable language type identifier.
  19. * - description: A description of the language type.
  20. * - locked: A boolean indicating if the user can choose whether to configure
  21. * the language type or not using the UI.
  22. * - fixed: A fixed array of language negotiation method identifiers to use to
  23. * initialize this language. If locked is set to TRUE and fixed is set, it
  24. * will always use the specified methods in the given priority order. If not
  25. * present and locked is TRUE then language-interface will be
  26. * used.
  27. *
  28. * @todo Rename the 'fixed' key to something more meaningful, for instance
  29. * 'negotiation settings'. See https://www.drupal.org/node/2166879.
  30. *
  31. * @see hook_language_types_info_alter()
  32. * @ingroup language_negotiation
  33. */
  34. function hook_language_types_info() {
  35. return array(
  36. 'custom_language_type' => array(
  37. 'name' => t('Custom language'),
  38. 'description' => t('A custom language type.'),
  39. 'locked' => FALSE,
  40. ),
  41. 'fixed_custom_language_type' => array(
  42. 'locked' => TRUE,
  43. 'fixed' => array('custom_language_negotiation_method'),
  44. ),
  45. );
  46. }
  47. /**
  48. * Perform alterations on language types.
  49. *
  50. * @param array $language_types
  51. * Array of language type definitions.
  52. *
  53. * @see hook_language_types_info()
  54. * @ingroup language_negotiation
  55. */
  56. function hook_language_types_info_alter(array &$language_types) {
  57. if (isset($language_types['custom_language_type'])) {
  58. $language_types['custom_language_type_custom']['description'] = t('A far better description.');
  59. }
  60. }
  61. /**
  62. * Perform alterations on language negotiation methods.
  63. *
  64. * @param array $negotiation_info
  65. * Array of language negotiation method definitions.
  66. *
  67. * @ingroup language_negotiation
  68. */
  69. function hook_language_negotiation_info_alter(array &$negotiation_info) {
  70. if (isset($negotiation_info['custom_language_method'])) {
  71. $negotiation_info['custom_language_method']['config'] = 'admin/config/regional/language/detection/custom-language-method';
  72. }
  73. }
  74. /**
  75. * Allow modules to alter the language fallback candidates.
  76. *
  77. * @param array $candidates
  78. * An array of language codes whose order will determine the language fallback
  79. * order.
  80. * @param array $context
  81. * A language fallback context.
  82. *
  83. * @see \Drupal\Core\Language\LanguageManagerInterface::getFallbackCandidates()
  84. */
  85. function hook_language_fallback_candidates_alter(array &$candidates, array $context) {
  86. $candidates = array_reverse($candidates);
  87. }
  88. /**
  89. * Allow modules to alter the fallback candidates for specific operations.
  90. *
  91. * @param array $candidates
  92. * An array of language codes whose order will determine the language fallback
  93. * order.
  94. * @param array $context
  95. * A language fallback context.
  96. *
  97. * @see \Drupal\Core\Language\LanguageManagerInterface::getFallbackCandidates()
  98. */
  99. function hook_language_fallback_candidates_OPERATION_alter(array &$candidates, array $context) {
  100. // We know that the current OPERATION deals with entities so no need to check
  101. // here.
  102. if ($context['data']->getEntityTypeId() == 'node') {
  103. $candidates = array_reverse($candidates);
  104. }
  105. }
  106. /**
  107. * @} End of "addtogroup hooks".
  108. */