You are here

function i18n_select_check_query in Internationalization 7

Check whether we should apply language conditions here:

  • The query has not been tagged with 'i18n_select'
  • The query doesn't have already a language condition
  • All the conditions have a table alias or there's only one table.
  • We are not loading specific objects (no condition for index field).

Parameters

$query: Query object.

$index_field: Object index field to check we don't have 'IN' conditions for it.

2 calls to i18n_select_check_query()
i18n_select_query_node_access_alter in i18n_select/i18n_select.module
Implementation of hook_query_node_access_alter().
i18n_select_query_term_access_alter in i18n_select/i18n_select.module
Implementation of hook_query_term_access_alter().

File

i18n_select/i18n_select.module, line 257
Multilingual content selection module.

Code

function i18n_select_check_query($query, $index_field = NULL) {
  static $tags;

  // Skip queries with certain tags
  if (!isset($tags)) {
    $tags = ($skip = variable_get('i18n_select_skip_tags', 'views')) ? array_map('trim', explode(',', $skip)) : array();
    $tags[] = 'i18n_select';
  }
  foreach ($tags as $tag) {
    if ($query
      ->hasTag($tag)) {
      return FALSE;
    }
  }

  // Check all the conditions to see whether the query is suitable for altering.
  foreach ($query
    ->conditions() as $condition) {
    if (is_array($condition)) {

      // @todo For some complex queries, like search ones, field is a DatabaseCondition object
      if (!isset($condition['field']) || !is_string($condition['field'])) {

        // There's a weird condition field, we won't take any chances.
        return FALSE;
      }
      else {

        // Just check the condition doesn't include the language field
        if (strpos($condition['field'], '.') === FALSE) {
          $field = $condition['field'];
        }
        else {
          list($table, $field) = explode('.', $condition['field']);
        }
        if ($field == 'language') {
          return FALSE;
        }

        // Check 'IN' conditions for index field, usually entity loading for specific objects.
        if ($field == $index_field && $condition['operator'] == 'IN') {
          return FALSE;
        }
      }
    }
  }

  // If the language field is present we don't want to do any filtering.
  $fields = $query
    ->getFields();
  if (isset($fields['language'])) {
    return FALSE;
  }
  return TRUE;
}