Commit 42251b6b authored by catch's avatar catch
Browse files

Issue #1054162 by tim.plunkett, Dave Reid, joachim, Berdir: Taxonomy bundles...

Issue #1054162 by tim.plunkett, Dave Reid, joachim, Berdir: Taxonomy bundles not supported by EntityFieldQuery.
parent 50a7330b
......@@ -1661,3 +1661,35 @@ function taxonomy_taxonomy_term_delete(Term $term) {
/**
* @} End of "defgroup taxonomy_index".
*/
/**
* Implements hook_entity_query_alter().
*
* Converts EntityFieldQuery instances on taxonomy terms that have an entity
* condition on term bundles (vocabulary machine names). Since the vocabulary
* machine name is not present in the {taxonomy_term_data} table itself, we have
* to convert the bundle condition into a proprety condition of vocabulary IDs
* to match against {taxonomy_term_data}.vid.
*/
function taxonomy_entity_query_alter($query) {
$conditions = &$query->entityConditions;
// Alter only taxonomy term queries with bundle conditions.
if (isset($conditions['entity_type']) && $conditions['entity_type']['value'] == 'taxonomy_term' && isset($conditions['bundle'])) {
// Convert vocabulary machine names to vocabulary IDs.
$vocabulary_data = taxonomy_vocabulary_get_names();
$vids = array();
if (is_array($conditions['bundle']['value'])) {
foreach ($conditions['bundle']['value'] as $vocabulary_machine_name) {
$vids[] = $vocabulary_data[$vocabulary_machine_name]->vid;
}
}
else {
$vocabulary_machine_name = $conditions['bundle']['value'];
$vids = $vocabulary_data[$vocabulary_machine_name]->vid;
}
$query->propertyCondition('vid', $vids, $conditions['bundle']['operator']);
unset($conditions['bundle']);
}
}
......@@ -1952,5 +1952,22 @@ class TaxonomyEFQTestCase extends TaxonomyWebTestCase {
$result = $result['taxonomy_term'];
asort($result);
$this->assertEqual(array_keys($terms), array_keys($result), 'Taxonomy terms were retrieved by EntityFieldQuery.');
// Create a second vocabulary and five more terms.
$vocabulary2 = $this->createVocabulary();
$terms2 = array();
for ($i = 0; $i < 5; $i++) {
$term = $this->createTerm($vocabulary2);
$terms2[$term->tid] = $term;
}
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'taxonomy_term');
$query->entityCondition('bundle', $vocabulary2->machine_name);
$result = $query->execute();
$result = $result['taxonomy_term'];
asort($result);
$this->assertEqual(array_keys($terms2), array_keys($result), format_string('Taxonomy terms from the %name vocabulary were retrieved by EntityFieldQuery.', array('%name' => $vocabulary2->name)));
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment