i18n_select.module

Tracking 7.x-1.x branch
  1. drupal
    1. 7 contributions/i18n/i18n_select/i18n_select.module

Multilingual content selection module.

Alters content queries to add language conditions.

Queries tagged with 'i18n_select' or that already have a language condition will not be altered.

Functions & methods

NameDescription
i18n_select_block_list_alterImplements hook_block_list_alter().
i18n_select_check_conditionsCheck all query conditions have a table alias.
i18n_select_check_queryCheck whether we should apply language conditions here:
i18n_select_check_tableCheck table exists in query and get alias for it.
i18n_select_initImplements hook_init().
i18n_select_langcodesGet language codes for content selection to use in query conditions
i18n_select_languageGet main language for content selection
i18n_select_menuImplements hook_menu().
i18n_select_modeGet current mode for i18n selection
i18n_select_pageCheck current path to enable selection
i18n_select_query_node_access_alterImplementation of hook_query_node_access_alter().
i18n_select_query_term_access_alterImplementation of hook_query_term_access_alter().
_i18n_select_table_aliasGet table alias

Constants

NameDescription
I18N_SELECT_MISSING
I18N_SELECT_NONE
I18N_SELECT_NORMAL
I18N_SELECT_PAGE_LISTEDEnable on only the listed pages.
I18N_SELECT_PAGE_NOTLISTEDEnable on every page except the listed pages.
I18N_SELECT_PAGE_PHPEnable if the associated PHP code returns TRUE.

File

View source
  1. <?php
  2. /**
  3. * @file
  4. * Multilingual content selection module.
  5. *
  6. * Alters content queries to add language conditions.
  7. *
  8. * Queries tagged with 'i18n_select' or that already have a language condition will not be altered.
  9. */
  10. // No language selection
  11. define('I18N_SELECT_NONE', 0);
  12. // Content with current language and undefined language
  13. define('I18N_SELECT_NORMAL', 1);
  14. // Select default language when current language is missing
  15. define('I18N_SELECT_MISSING', 2);
  16. /**
  17. * Enable on every page except the listed pages.
  18. */
  19. define('I18N_SELECT_PAGE_NOTLISTED', 0);
  20. /**
  21. * Enable on only the listed pages.
  22. */
  23. define('I18N_SELECT_PAGE_LISTED', 1);
  24. /**
  25. * Enable if the associated PHP code returns TRUE.
  26. */
  27. define('I18N_SELECT_PAGE_PHP', 2);
  28. /**
  29. * Implements hook_init().
  30. */
  31. function i18n_select_init() {
  32. // Determine selection mode for this page
  33. i18n_select(i18n_select_page());
  34. }
  35. /**
  36. * Implements hook_block_list_alter().
  37. *
  38. * Dirty trick to enable selection for blocks though it may be disabled for the current page.
  39. */
  40. function i18n_select_block_list_alter(&$blocks) {
  41. // Still, skip for form submission. There are pages like the ones produced
  42. // by overlay that render the blocks before the page.
  43. // See overlay_init(), overlay_overlay_child_initialize()
  44. if (empty($_POST) && !isset($_GET['token']) && variable_get('i18n_select_page_block', TRUE)) {
  45. i18n_select(TRUE);
  46. }
  47. }
  48. /**
  49. * Implements hook_menu().
  50. */
  51. function i18n_select_menu() {
  52. $items['admin/config/regional/i18n/select'] = array(
  53. 'title' => 'Selection',
  54. 'description' => 'Configure extended options for multilingual content and translations.',
  55. 'page callback' => 'drupal_get_form',
  56. 'page arguments' => array('i18n_select_admin_settings'),
  57. 'access arguments' => array('administer site configuration'),
  58. 'file' => 'i18n_select.admin.inc',
  59. 'type' => MENU_LOCAL_TASK,
  60. );
  61. return $items;
  62. }
  63. /**
  64. * Get current mode for i18n selection
  65. *
  66. * @param $type
  67. * Selection type: 'nodes', 'taxonomy', etc..
  68. */
  69. function i18n_select_mode($type = NULL) {
  70. if (i18n_select() && (!$type || variable_get('i18n_select_' . $type, TRUE))) {
  71. return I18N_SELECT_NORMAL;
  72. }
  73. else {
  74. return I18N_SELECT_NONE;
  75. }
  76. }
  77. /**
  78. * Check current path to enable selection
  79. *
  80. * This works pretty much like block visibility
  81. *
  82. * @return boolean
  83. * TRUE if content selection should be enabled for this page.
  84. */
  85. function i18n_select_page() {
  86. static $mode;
  87. if (!isset($mode)) {
  88. $mode = &drupal_static(__FUNCTION__);
  89. $visibility = variable_get('i18n_select_page_mode', I18N_SELECT_PAGE_NOTLISTED);
  90. if ($pages = variable_get('i18n_select_page_list', 'admin/*')) {
  91. // Convert path to lowercase. This allows comparison of the same path
  92. // with different case. Ex: /Page, /page, /PAGE.
  93. $pages = drupal_strtolower($pages);
  94. if ($visibility < I18N_SELECT_PAGE_PHP) {
  95. // Convert the Drupal path to lowercase
  96. $path = drupal_strtolower(drupal_get_path_alias($_GET['q']));
  97. // Compare the lowercase internal and lowercase path alias (if any).
  98. $page_match = drupal_match_path($path, $pages);
  99. if ($path != $_GET['q']) {
  100. $page_match = $page_match || drupal_match_path($_GET['q'], $pages);
  101. }
  102. // When $visibility has a value of 0 (I18N_SELECT_PAGE_NOTLISTED),
  103. // the block is displayed on all pages except those listed in $pages.
  104. // When set to 1 (I18N_SELECT_PAGE_LISTED), it is displayed only on those
  105. // pages listed in $pages.
  106. $mode = !($visibility xor $page_match);
  107. }
  108. elseif (module_exists('php')) {
  109. $mode = php_eval($pages);
  110. }
  111. else {
  112. $mode = FALSE;
  113. }
  114. }
  115. else {
  116. // No pages defined, still respect the setting (unlike blocks)
  117. $mode = $visibility == I18N_SELECT_PAGE_NOTLISTED;
  118. }
  119. }
  120. return $mode;
  121. }
  122. /**
  123. * Implementation of hook_query_node_access_alter().
  124. *
  125. * Rewrite node queries so language selection options are enforced.
  126. */
  127. function i18n_select_query_node_access_alter(QueryAlterableInterface $query) {
  128. if (i18n_select_mode('nodes') && i18n_select_check_query($query, 'nid') &&
  129. ($table_alias = i18n_select_check_table($query, 'node', 'nid'))) {
  130. $query->condition($table_alias . '.language', i18n_select_langcodes());
  131. // Mark query as altered
  132. $query->addTag('i18n_select');
  133. }
  134. }
  135. /**
  136. * Implementation of hook_query_term_access_alter().
  137. *
  138. * Rewrite taxonomy term queries so language selection options are enforced.
  139. */
  140. function i18n_select_query_term_access_alter(QueryAlterableInterface $query) {
  141. if (module_exists('i18n_taxonomy') && i18n_select_mode('taxonomy') && i18n_select_check_query($query, 'tid') &&
  142. ($table_alias = i18n_select_check_table($query, 'taxonomy_term_data', 'tid'))) {
  143. $query->condition($table_alias . '.language', i18n_select_langcodes());
  144. // Mark query as altered
  145. $query->addTag('i18n_select');
  146. }
  147. }
  148. /**
  149. * Check table exists in query and get alias for it.
  150. *
  151. * @param $query
  152. * Query object
  153. * @param $table_name
  154. * Table name to find.
  155. * @param $field_name
  156. * field to join the table if needed.
  157. *
  158. * @return
  159. * Table alias if found, none if not.
  160. */
  161. function i18n_select_check_table($query, $table_name, $field_name) {
  162. $tables = $query->getTables();
  163. foreach ($tables as $table) {
  164. if (!($table instanceof SelectQueryInterface) && $table['table'] == $table_name) {
  165. return _i18n_select_table_alias($table);
  166. }
  167. }
  168. // Join the table if we can find the key field on any of the tables
  169. // And all the conditions have a table alias (or there's a unique table).
  170. if (count($tables) == 1) {
  171. $table = reset($tables);
  172. $table_alias = _i18n_select_table_alias($table);
  173. if (i18n_select_check_conditions($query, $table_alias)) {
  174. $join_table = $table_alias;
  175. }
  176. }
  177. elseif (i18n_select_check_conditions($query)) {
  178. // Try to find the right field in the table schema.
  179. foreach ($tables as $table) {
  180. $schema = drupal_get_schema($table['table']);
  181. if ($schema && !empty($schema['fields'][$field_name])) {
  182. $join_table = _i18n_select_table_alias($table);
  183. break;
  184. }
  185. }
  186. }
  187. if (!empty($join_table)) {
  188. return $query->join($table_name, $table_name, $join_table . '.' . $field_name . ' = %alias.' . $field_name);
  189. }
  190. else {
  191. return FALSE;
  192. }
  193. }
  194. /**
  195. * Get table alias
  196. */
  197. function _i18n_select_table_alias($table) {
  198. return !empty($table['alias']) ? $table['alias'] : $table['table'];
  199. }
  200. /**
  201. * Check all query conditions have a table alias.
  202. *
  203. * @param $table_alias
  204. * Optional table alias for fields without table.
  205. *
  206. * @return boolean
  207. * TRUE if table conditions are ok, FALSE otherwise.
  208. */
  209. function i18n_select_check_conditions($query, $table_alias = NULL) {
  210. $conditions =& $query->conditions();
  211. foreach ($conditions as $index => $condition) {
  212. if (is_array($condition) && isset($condition['field'])) {
  213. if (strpos($condition['field'], '.') === FALSE) {
  214. if ($table_alias) {
  215. // Change the condition to include a table alias.
  216. $conditions[$index]['field'] = $table_alias . '.' . $condition['field'];
  217. }
  218. else {
  219. // We won't risk joining anything here.
  220. return FALSE;
  221. }
  222. }
  223. }
  224. }
  225. return TRUE;
  226. }
  227. /**
  228. * Check whether we should apply language conditions here:
  229. * - The query has not been tagged with 'i18n_select'
  230. * - The query doesn't have already a language condition
  231. * - All the conditions have a table alias or there's only one table.
  232. * - We are not loading specific objects (no condition for index field).
  233. *
  234. * @param $query
  235. * Query object.
  236. * @param $index_field
  237. * Object index field to check we don't have 'IN' conditions for it.
  238. */
  239. function i18n_select_check_query($query, $index_field = NULL) {
  240. static $tags;
  241. // Skip queries with certain tags
  242. if (!isset($tags)) {
  243. $tags = ($skip = variable_get('i18n_select_skip_tags', 'views')) ? array_map('trim', explode(',', $skip)) : array();
  244. $tags[] = 'i18n_select';
  245. }
  246. foreach ($tags as $tag) {
  247. if ($query->hasTag($tag)) {
  248. return FALSE;
  249. }
  250. }
  251. // Check all the conditions to see whether the query is suitable for altering.
  252. foreach ($query->conditions() as $condition) {
  253. if (is_array($condition)) {
  254. // @todo For some complex queries, like search ones, field is a DatabaseCondition object
  255. if (!isset($condition['field']) || !is_string($condition['field'])) {
  256. // There's a weird condition field, we won't take any chances.
  257. return FALSE;
  258. }
  259. else {
  260. // Just check the condition doesn't include the language field
  261. if (strpos($condition['field'], '.') === FALSE) {
  262. $field = $condition['field'];
  263. }
  264. else {
  265. list($table, $field) = explode('.', $condition['field']);
  266. }
  267. if ($field == 'language') {
  268. return FALSE;
  269. }
  270. // Check 'IN' conditions for index field, usually entity loading for specific objects.
  271. if ($field == $index_field && $condition['operator'] == 'IN') {
  272. return FALSE;
  273. }
  274. }
  275. }
  276. }
  277. // If the language field is present we don't want to do any filtering.
  278. $fields = $query->getFields();
  279. if (isset($fields['language'])) {
  280. return FALSE;
  281. }
  282. return TRUE;
  283. }
  284. /**
  285. * Get main language for content selection
  286. */
  287. function i18n_select_language() {
  288. return $GLOBALS[LANGUAGE_TYPE_CONTENT];
  289. }
  290. /**
  291. * Get language codes for content selection to use in query conditions
  292. */
  293. function i18n_select_langcodes() {
  294. return array(i18n_select_language()->language, LANGUAGE_NONE);
  295. }