You are here

function i18n_select_check_table in Internationalization 7

Check table exists in query and get alias for it.

Parameters

$query: Query object

$table_name: Table name to find.

$field_name: field to join the table if needed.

Return value

Table alias if found, none if not.

2 calls to i18n_select_check_table()
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 176
Multilingual content selection module.

Code

function i18n_select_check_table($query, $table_name, $field_name) {
  $tables = $query
    ->getTables();
  foreach ($tables as $table) {
    if (!$table instanceof SelectQueryInterface && $table['table'] == $table_name) {
      return _i18n_select_table_alias($table);
    }
  }

  // Join the table if we can find the key field on any of the tables
  // And all the conditions have a table alias (or there's a unique table).
  if (count($tables) == 1) {
    $table = reset($tables);
    $table_alias = _i18n_select_table_alias($table);
    if (i18n_select_check_conditions($query, $table_alias)) {
      $join_table = $table_alias;
    }
  }
  elseif (i18n_select_check_conditions($query)) {

    // Try to find the right field in the table schema.
    foreach ($tables as $table) {
      $schema = drupal_get_schema($table['table']);
      if ($schema && !empty($schema['fields'][$field_name])) {
        $join_table = _i18n_select_table_alias($table);
        break;
      }
    }
  }
  if (!empty($join_table)) {
    return $query
      ->join($table_name, $table_name, $join_table . '.' . $field_name . ' = %alias.' . $field_name);
  }
  else {
    return FALSE;
  }
}