function tmgmt_entity_get_translatable_entities in Translation Management Tool 7
Gets translatable entities of a given type.
Additionally you can specify entity property conditions, pager and limit.
Parameters
string $entity_type: Drupal entity type.
array $property_conditions: Entity properties. There is no value processing so caller must make sure the provided entity property exists for given entity type and its value is processed.
bool $pager: Flag to determine if pager will be used.
Return value
array Array of translatable entities.
2 calls to tmgmt_entity_get_translatable_entities()
- TMGMTEntityDefaultSourceUIController::getEntitiesTranslationData in sources/
entity/ tmgmt_entity.ui.inc - Gets entities data of provided type needed to build overview form list.
- TMGMTEntitySourceLanguageNoneTestCase::testLanguageNeutral in sources/
entity/ tmgmt_entity.source.none.test - Test if language neutral entities are not allowed for translation.
File
- sources/
entity/ tmgmt_entity.module, line 112 - Source plugin for the Translation Management system that handles entities.
Code
function tmgmt_entity_get_translatable_entities($entity_type, $property_conditions = array(), $pager = FALSE) {
if (!in_array($entity_type, variable_get('entity_translation_entity_types', array()))) {
return array();
}
$languages = drupal_map_assoc(array_keys(language_list()));
$entity_info = entity_get_info($entity_type);
$label_key = isset($entity_info['entity keys']['label']) ? $entity_info['entity keys']['label'] : NULL;
$id_key = $entity_info['entity keys']['id'];
$query = db_select($entity_info['base table'], 'e');
$query
->addTag('tmgmt_entity_get_translatable_entities');
$query
->addField('e', $id_key);
// Language neutral entities are not translatable. Filter them out. To do
// that: join {entity_translation} table, but only records with source column
// empty. The {entity_translation}.language will represent the original entity
// language in that case.
$source_table_alias = $query
->leftJoin('entity_translation', NULL, "%alias.entity_type = :entity_type AND %alias.entity_id = e.{$id_key} AND %alias.source = ''", array(
':entity_type' => $entity_type,
));
$query
->condition("{$source_table_alias}.language", LANGUAGE_NONE, '<>');
// Searching for sources with missing translation.
if (!empty($property_conditions['target_status']) && !empty($property_conditions['target_language']) && in_array($property_conditions['target_language'], $languages)) {
$translation_table_alias = db_escape_field('et_' . $property_conditions['target_language']);
$query
->leftJoin('entity_translation', $translation_table_alias, "%alias.entity_type = :entity_type AND %alias.entity_id = e.{$id_key} AND %alias.language = :language", array(
':entity_type' => $entity_type,
':language' => $property_conditions['target_language'],
));
// Exclude entities with having source language same as the target language
// we search for.
$query
->condition('e.language', $property_conditions['target_language'], '<>');
if ($property_conditions['target_status'] == 'untranslated_or_outdated') {
$or = db_or();
$or
->isNull("{$translation_table_alias}.language");
$or
->condition("{$translation_table_alias}.translate", 1);
$query
->condition($or);
}
elseif ($property_conditions['target_status'] == 'outdated') {
$query
->condition("{$translation_table_alias}.translate", 1);
}
elseif ($property_conditions['target_status'] == 'untranslated') {
$query
->isNull("{$translation_table_alias}.language");
}
}
// Remove the condition so we do not try to add it again below.
unset($property_conditions['target_language']);
unset($property_conditions['target_status']);
// Searching for the source label.
if (!empty($label_key) && isset($property_conditions[$label_key])) {
$search_tokens = explode(' ', $property_conditions[$label_key]);
$or = db_or();
foreach ($search_tokens as $search_token) {
$search_token = trim($search_token);
if (strlen($search_token) > 2) {
$or
->condition($label_key, "%{$search_token}%", 'LIKE');
}
}
if ($or
->count() > 0) {
$query
->condition($or);
}
unset($property_conditions[$label_key]);
}
// Searching by taxonomy bundles - we need to switch to vid as the bundle key.
if ($entity_type == 'taxonomy_term' && !empty($property_conditions['vocabulary_machine_name'])) {
$property_name = 'vid';
$vocabulary = taxonomy_vocabulary_machine_name_load($property_conditions['vocabulary_machine_name']);
$property_value = $vocabulary->vid;
$query
->condition('e.' . $property_name, $property_value);
// Remove the condition so we do not try to add it again below.
unset($property_conditions['vocabulary_machine_name']);
}
elseif (in_array($entity_type, array(
'comment',
'node',
))) {
$node_table_alias = 'e';
// For comments join node table so that we can filter based on type.
if ($entity_type == 'comment') {
$query
->join('node', 'n', 'e.nid = n.nid');
$node_table_alias = 'n';
}
// Get translatable node types and check if it is worth to continue.
$translatable_node_types = array_keys(tmgmt_entity_get_translatable_bundles('node'));
if (empty($translatable_node_types)) {
return array();
}
// If we have type property add condition.
if (isset($property_conditions['type'])) {
$query
->condition($node_table_alias . '.type', $property_conditions['type']);
// Remove the condition so we do not try to add it again below.
unset($property_conditions['type']);
}
else {
$query
->condition($node_table_alias . '.type', $translatable_node_types);
}
}
// Add remaining query conditions which are expected to be handled in a
// generic way.
foreach ($property_conditions as $property_name => $property_value) {
$query
->condition('e.' . $property_name, $property_value);
}
if ($pager) {
$query = $query
->extend('PagerDefault')
->limit(variable_get('tmgmt_source_list_limit', 20));
}
else {
$query
->range(0, variable_get('tmgmt_source_list_limit', 20));
}
$query
->orderBy($entity_info['entity keys']['id'], 'DESC');
$entity_ids = $query
->execute()
->fetchCol();
$entities = array();
if (!empty($entity_ids)) {
$entities = entity_load($entity_type, $entity_ids);
}
return $entities;
}