Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Cezarion/41d9830ca86fc4ec6d9e to your computer and use it in GitHub Desktop.
Save Cezarion/41d9830ca86fc4ec6d9e to your computer and use it in GitHub Desktop.
<?php
define('VOCAB_MACHINE_NAME', 'verticals');
function case_studies_install() {
_create_vocabulary();
$terms = array(
array(
'en' => 'Advertisers',
'fr' => 'Annonceurs',
),
array(
'en' => 'Agencies',
'fr' => 'Agences',
),
array(
'en' => 'Automotive',
'fr' => 'Automobile',
),
array(
'en' => 'Resellers',
'fr' => 'Revendeurs',
),
);
$vid = taxonomy_vocabulary_machine_name_load(VOCAB_MACHINE_NAME)->vid;
_create_terms($terms, $vid);
}
/**
* Create a taxonomy and attach a field to it.
*/
function _create_vocabulary() {
$t = get_t();
$vocab = new stdClass();
$vocab->name = $t('Verticals');
$vocab->machine_name = VOCAB_MACHINE_NAME;
$vocab->description = $t('Description');
$vocab->hierarchy = 0;
$vocab->module = 'case_studies';
$vocab->weight = 1;
$vocab->i18n_mode = 1;
taxonomy_vocabulary_save($vocab);
}
function _create_terms($terms = array(), $vid = NULL) {
foreach ($terms as $term_to_create) {
$term = new stdClass();
$term->name = $term_to_create['en'];
$term->vid = $vid;
$term->language = LANGUAGE_NONE;
taxonomy_term_save($term);
_create_term_translation($term, 'fr', $term_to_create['fr']);
}
}
function case_studies_uninstall() {
$vid = taxonomy_vocabulary_machine_name_load(VOCAB_MACHINE_NAME)->vid;
taxonomy_vocabulary_delete($vid);
}
/**
* Create or update a translation string for a taxonomy term.
*
* @param $term
* The term object with the tid set.
* @param $langcode
* The langcode for the translation.
* @param $translation
* The translation string.
*/
function _create_term_translation($term, $langcode, $translation, $property = 'name') {
$context = array(
'term',
$term->tid,
$property,
);
$textgroup = 'taxonomy';
i18n_string_textgroup($textgroup)->update_translation($context, $langcode, $translation);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment