View source
<?php
function taxonomy_features_api() {
return array(
'taxonomy' => array(
'name' => t('Taxonomy'),
'feature_source' => TRUE,
'default_hook' => 'taxonomy_default_vocabularies',
'default_file' => FEATURES_DEFAULTS_INCLUDED,
),
);
}
function taxonomy_features_export_options() {
$vocabularies = array();
foreach (_taxonomy_features_get_vocabularies() as $machine_name => $vocabulary) {
$vocabularies[$machine_name] = $vocabulary->name;
}
return $vocabularies;
}
function taxonomy_features_export($data, &$export, $module_name = '') {
$export['dependencies']['features'] = 'features';
$export['dependencies']['taxonomy'] = 'taxonomy';
$map = features_get_default_map('taxonomy');
foreach ($data as $machine_name) {
if (isset($map[$machine_name]) && $map[$machine_name] != $module_name) {
$export['dependencies'][$map[$machine_name]] = $map[$machine_name];
}
else {
$export['features']['taxonomy'][$machine_name] = $machine_name;
}
}
return array();
}
function taxonomy_features_export_render($module, $data) {
$vocabularies = _taxonomy_features_get_vocabularies();
$code = array();
foreach ($data as $machine_name) {
foreach ($vocabularies as $k => $v) {
if ($k == $machine_name) {
unset($v->vid);
$code[$machine_name] = $v;
}
}
}
$code = " return " . features_var_export($code, ' ') . ";";
return array(
'taxonomy_default_vocabularies' => $code,
);
}
function taxonomy_features_revert($module) {
taxonomy_features_rebuild($module);
}
function taxonomy_features_rebuild($module) {
if ($vocabularies = features_get_default('taxonomy', $module)) {
foreach ($vocabularies as $machine_name => $vocabulary) {
if ($vid = db_result(db_query("SELECT vid FROM {vocabulary} WHERE module = '%s'", 'features_' . $machine_name))) {
$vocabulary['vid'] = $vid;
}
taxonomy_save_vocabulary($vocabulary);
}
}
}
function _taxonomy_features_get_vocabularies() {
$vocabularies = array();
foreach (taxonomy_get_vocabularies() as $k => $vocabulary) {
if (strpos($vocabulary->module, 'features_') === 0) {
$vocabularies[substr($vocabulary->module, 9)] = $vocabulary;
}
}
return $vocabularies;
}
function _features_form_taxonomy_form_vocabulary_alter(&$form, &$form_state) {
$elem = array(
'#type' => 'textfield',
'#title' => t('Machine name'),
'#description' => t('A system name for this vocabulary, may not be changed once it has been set.'),
);
if (!isset($form['module']) || $form['module']['#value'] == 'taxonomy') {
$elem['#element_validate'] = array(
'features_taxonomy_machine_name_validate',
);
unset($form['module']);
}
elseif (strpos($form['module']['#value'], 'features_') === 0) {
$elem['#disabled'] = true;
$elem['#value'] = substr($form['module']['#value'], 9);
}
else {
return;
}
$new = array();
foreach ($form['identification'] as $k => $v) {
$new[$k] = $v;
if ($k == 'name') {
$new['module'] = $elem;
}
}
$form['identification'] = $new;
}
function features_taxonomy_machine_name_validate($element, &$form_state) {
if (!empty($element['#value']) && !preg_match('!^[a-z0-9_]+$!', $element['#value'])) {
form_error($element, t('The machine name can only consist of lowercase letters, underscores, and numbers.'));
}
if (!empty($element['#value'])) {
$name = 'features_' . $element['#value'];
$vid = db_result(db_query("SELECT vid FROM {vocabulary} WHERE module = '%s'", $name));
if ($vid) {
form_error($element, t('This machine name is already taken. Please enter a unique machine name or this taxonomy.'));
}
else {
$form_state['values']['module'] = $name;
}
}
else {
$form_state['values']['module'] = 'taxonomy';
}
}