Skip to content

Instantly share code, notes, and snippets.

@kitikonti
Created September 20, 2014 12:19
Show Gist options
  • Save kitikonti/fa2bd9fc88c4d3f22626 to your computer and use it in GitHub Desktop.
Save kitikonti/fa2bd9fc88c4d3f22626 to your computer and use it in GitHub Desktop.
$city = 'Rome';
// Get term id of city. taxonomy_get_term_by_name don't work because it
// don't works with multilingual values.
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'taxonomy_term')
->entityCondition('bundle', 'property_country_city')
->fieldCondition('name_field', 'value', $city, '=')
->addTag('term_is_child_of')
->addMetaData('parent_term_id', $country_id)
->addMetaData('account', user_load(1)); // Run the query as user 1.
$result = $query->execute();
// If country ex
if ($result) {
// Use existing city.
}
else {
// Create new city.
}
---------------------------------------------------------------------
/**
* Implements hook_query_TAG_alter().
*/
function MY_AMAZING_MODULE_import_query_term_is_child_of_alter(QueryAlterableInterface $query) {
$parent_term_id = $query->alterMetaData['parent_term_id'];
$query->addJoin('INNER', 'taxonomy_term_hierarchy', 'taxonomy_term_hierarchy', 'taxonomy_term_hierarchy.tid = taxonomy_term_data.tid');
$query->condition('taxonomy_term_hierarchy.parent', $parent_term_id);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment