language.inc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /**
  3. * @file
  4. * Ctools context type plugin to hold the current language context.
  5. */
  6. /**
  7. * Plugins are described by creating a $plugin array which will be used
  8. * by the system that includes this file.
  9. */
  10. $plugin = array(
  11. 'title' => t('Language'),
  12. 'description' => t('Language object.'),
  13. 'context' => 'ctools_context_language_create',
  14. 'context name' => 'language',
  15. 'keyword' => 'language',
  16. // Provides a list of items which are exposed as keywords.
  17. 'convert list' => 'ctools_language_context_convert_list',
  18. // Convert keywords into data.
  19. 'convert' => 'ctools_language_context_convert',
  20. 'placeholder form' => array(
  21. '#type' => 'textfield',
  22. '#description' => t('Enter a valid langcode.'),
  23. '#value' => $GLOBALS['language']->language,
  24. ),
  25. // Provide settings for the context.
  26. 'edit form' => 'ctools_context_language_settings_form',
  27. 'settings' => ctools_context_language_conf_defaults(),
  28. );
  29. /**
  30. * Ensures a full populated settings array with sane defaults.
  31. *
  32. * @param mixed $conf
  33. * Array with the user defined settings, or a string identifying a language.
  34. *
  35. * @return array
  36. * Array with all available settings.
  37. */
  38. function ctools_context_language_conf_defaults($conf = array()) {
  39. if (!is_array($conf)) {
  40. $conf = array(
  41. 'preset_langcode' => (string) $conf,
  42. );
  43. }
  44. return $conf + array(
  45. 'enable_cache_argument' => TRUE,
  46. 'language_type' => 'language',
  47. 'preset_langcode' => $GLOBALS['language']->language,
  48. );
  49. }
  50. /**
  51. * Create a context, either from manual configuration or the current language.
  52. */
  53. function ctools_context_language_create($empty, $data = NULL, $conf = FALSE) {
  54. $context = new ctools_context('language');
  55. $context->plugin = 'language';
  56. if ($empty) {
  57. return $context;
  58. }
  59. $context->title = t('Language');
  60. $settings = ctools_context_language_conf_defaults($data);
  61. if ($settings['language_type'] != 'preset') {
  62. $language_object = $GLOBALS[$settings['language_type']];
  63. }
  64. else {
  65. // Fetch the enabled language objects.
  66. $languages = language_list('enabled');
  67. $languages = $languages[1];
  68. // Set the custom language, but fallback to the interface language.
  69. $language_object = $GLOBALS['language'];
  70. if (isset($languages[$settings['preset_langcode']])) {
  71. $language_object = $languages[$settings['preset_langcode']];
  72. }
  73. }
  74. // If enabled set the argument ot use in the cid.
  75. if ($settings['enable_cache_argument']) {
  76. $context->argument = $language_object->language;
  77. }
  78. $context->data = $language_object;
  79. return $context;
  80. }
  81. /**
  82. * Provide a list of sub-keywords.
  83. *
  84. * This is used to provide keywords from the context for use in a content type,
  85. * pane, etc.
  86. */
  87. function ctools_language_context_convert_list() {
  88. $context = new stdClass();
  89. $context->data = $GLOBALS['language'];
  90. return array(
  91. 'language' => t('Langcode. E.g. !example', array('!example' => ctools_language_context_convert($context, 'language'))),
  92. 'name' => t('Name. E.g. !example', array('!example' => ctools_language_context_convert($context, 'name'))),
  93. 'native' => t('Native name of the language. E.g. !example', array('!example' => ctools_language_context_convert($context, 'native'))),
  94. 'direction' => t('Text direction 0=LRT, 1=RTL. E.g. !example', array('!example' => ctools_language_context_convert($context, 'direction'))),
  95. 'enabled' => t('Status. E.g. !example', array('!example' => ctools_language_context_convert($context, 'enabled'))),
  96. 'plurals' => t('Number of plural forms. E.g. !example', array('!example' => ctools_language_context_convert($context, 'plurals'))),
  97. 'formula' => t('Plural formula. E.g. !example', array('!example' => ctools_language_context_convert($context, 'formula'))),
  98. 'domain' => t('Domain prefix. E.g. !example', array('!example' => ctools_language_context_convert($context, 'domain'))),
  99. 'prefix' => t('Url prefix . E.g. !example', array('!example' => ctools_language_context_convert($context, 'prefix'))),
  100. 'weight' => t('The weight. E.g. !example', array('!example' => ctools_language_context_convert($context, 'weight'))),
  101. 'javascript' => t('Key of the javascript file with the translations. E.g. !example', array('!example' => ctools_language_context_convert($context, 'javascript'))),
  102. 'provider' => t('Negotiation method that defined the language. E.g. !example', array('!example' => ctools_language_context_convert($context, 'provider'))),
  103. );
  104. }
  105. /**
  106. * Convert a context property into a string to be used as a keyword.
  107. */
  108. function ctools_language_context_convert($context, $type) {
  109. if (isset($context->data->$type)) {
  110. return $context->data->$type;
  111. }
  112. }
  113. /**
  114. * Settings form.
  115. */
  116. function ctools_context_language_settings_form($form, &$form_state) {
  117. $conf = ctools_context_language_conf_defaults($form_state['conf']);
  118. $form['enable_cache_argument'] = array(
  119. '#title' => t('Add language to cache id'),
  120. '#description' => t('If enabled the langcode will be part of context aware caches.'),
  121. '#type' => 'checkbox',
  122. '#default_value' => $conf['enable_cache_argument'],
  123. );
  124. // Prepare language type options.
  125. $language_type_options = drupal_map_assoc(language_types());
  126. $language_type_options['preset'] = t('Custom');
  127. $form['language_type'] = array(
  128. '#title' => t('The language type to use'),
  129. '#type' => 'radios',
  130. '#required' => TRUE,
  131. '#options' => $language_type_options,
  132. '#default_value' => $conf['language_type'],
  133. );
  134. ctools_include('language');
  135. $language_options = ctools_language_list();
  136. $form['preset_langcode'] = array(
  137. '#title' => t('Language'),
  138. '#type' => 'select',
  139. '#options' => $language_options,
  140. '#default_value' => $conf['preset_langcode'],
  141. '#states' => array(
  142. 'visible' => array(
  143. ':input[name="language_type"]' => array('value' => 'preset'),
  144. ),
  145. ),
  146. );
  147. if (!empty($conf['preset_langcode']) && !isset($language_options[$conf['preset_langcode']])) {
  148. drupal_set_message(t('The currently selected language %langcode is no longer available.', array('%langcode' => $conf['preset_langcode'])), 'error', FALSE);
  149. }
  150. return $form;
  151. }