View source
<?php
function spaces_taxonomy_enable() {
$weight = db_query("SELECT weight FROM {system} WHERE type = :type AND name = :name", array(
':type' => 'module',
':name' => 'taxonomy',
))
->fetchField();
db_update('system')
->fields(array(
'weight' => $weight + 1,
))
->condition('name', 'spaces_taxonomy')
->execute();
}
function spaces_taxonomy_ctools_plugin_api($module, $api) {
if ($module == 'spaces' && $api == 'plugins') {
return array(
'version' => 3,
);
}
}
function spaces_taxonomy_purl_provider() {
$items = array();
$items["spaces_taxonomy"] = array(
'name' => t('Term space'),
'description' => t('Sets a space as the active space.'),
'callback' => 'spaces_init_space',
'callback arguments' => array(
'taxonomy',
),
'example' => 'my-space',
);
return $items;
}
function spaces_taxonomy_spaces_plugins() {
$plugins = array();
$plugins['space_taxonomy'] = array(
'handler' => array(
'path' => drupal_get_path('module', 'spaces_taxonomy') . '/plugins',
'file' => 'space_taxonomy.inc',
'class' => 'space_taxonomy',
'parent' => 'space_type_purl',
),
);
return $plugins;
}
function spaces_taxonomy_spaces_registry() {
return array(
'types' => array(
'taxonomy' => array(
'title' => t('Term space'),
'plugin' => 'space_taxonomy',
'path' => 'taxonomy/term/%',
),
),
);
}
function spaces_taxonomy_menu() {
$items = array();
$items['admin/structure/spaces/taxonomy'] = array(
'title' => 'Taxonomy',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'spaces_taxonomy_settings',
),
'access callback' => 'user_access',
'access arguments' => array(
'administer spaces',
),
'type' => MENU_LOCAL_TASK,
);
return $items;
}
function spaces_taxonomy_menu_alter(&$items) {
if (isset($items['taxonomy/term/%'])) {
$items['taxonomy/term/%/view'] = $items['taxonomy/term/%'];
$items['taxonomy/term/%/view']['type'] = MENU_DEFAULT_LOCAL_TASK;
$items['taxonomy/term/%/view']['title'] = 'View';
$items['taxonomy/term/%/view']['weight'] = -10;
}
}
function spaces_taxonomy_form_alter(&$form, $form_state, $form_id) {
if (!empty($form['#node_edit_form']) && arg(0) . '/' . arg(1) != 'admin/content') {
$vocab = variable_get('spaces_taxonomy_machine_name', 0);
$space = spaces_get_space();
if ($vocab && $space && $space->type == 'taxonomy') {
$fields = field_info_fields();
foreach ($fields as $name => $field) {
if ($field['type'] == 'taxonomy_term_reference' && $vocab == $field['settings']['allowed_values'][0]['vocabulary']) {
if ($term_field =& $form[$name]) {
$term_field['und']['#disabled'] = TRUE;
$term_field['und']['#default_value'] = $space->id;
$term_field['und']['#description'] = t('Spaces has disabled this field. You cannot move a node from one term space to another.');
}
}
}
}
}
}
function spaces_taxonomy_form_taxonomy_form_term_alter(&$form, $form_state) {
$vocab_name = variable_get('spaces_taxonomy_machine_name', 0);
if ($vocab_name && $form['vocabulary_machine_name']['#value'] == $vocab_name) {
$tid = isset($form['tid']['#value']) ? $form['tid']['#value'] : 0;
$space = spaces_load('taxonomy', $tid);
if ($space) {
$space
->activate();
$form['spaces'] = array(
'#type' => 'fieldset',
'#title' => t('Spaces'),
'#tree' => FALSE,
'#weight' => 0,
);
$modifier = purl_load(array(
'id' => $tid,
'provider' => 'spaces_taxonomy',
));
$form['spaces']['purl'] = purl_form('spaces_taxonomy', $tid, isset($modifier['value']) ? $modifier['value'] : '');
$presets = spaces_preset_load(NULL, 'taxonomy');
if (count($presets) > 1) {
module_load_include('inc', 'spaces', 'spaces.admin');
$form['spaces']['spaces_preset'] = spaces_preset_form($presets, 'taxonomy');
$space = spaces_load('taxonomy', $tid);
$default_value = $space->controllers->variable
->get('spaces_preset_taxonomy', 'space');
if (isset($default_value, $form['spaces']['spaces_preset']['spaces_preset_taxonomy']['#options'][$default_value])) {
$form['spaces']['spaces_preset']['spaces_preset_taxonomy']['#default_value'] = $default_value;
}
}
}
}
}
function spaces_taxonomy_taxonomy_term_insert($term) {
_spaces_taxonomy_term_save_preset_and_modifier($term);
}
function spaces_taxonomy_taxonomy_term_update($term) {
_spaces_taxonomy_term_save_preset_and_modifier($term);
}
function _spaces_taxonomy_term_save_preset_and_modifier($term) {
$space = spaces_load('taxonomy', $term->tid);
if ($space) {
if (!empty($term->spaces_preset_taxonomy)) {
$space->controllers->variable
->set('spaces_preset_taxonomy', $term->spaces_preset_taxonomy);
}
$modifier = array(
'provider' => 'spaces_taxonomy',
'id' => $term->tid,
'value' => $term->purl['value'],
);
purl_save($modifier);
}
}
function spaces_taxonomy_taxonomy_term_delete($term) {
spaces_delete('taxonomy', $term->tid);
$modifier = array(
'provider' => 'spaces_taxonomy',
'id' => $term->tid,
);
purl_delete($modifier);
}
function spaces_taxonomy_settings($form, &$form_state) {
$form = array();
$vocabs = array(
0 => '---',
);
foreach (taxonomy_get_vocabularies() as $vocab) {
$vocabs[$vocab->machine_name] = $vocab->name;
}
$fields = field_info_fields();
foreach ($fields as $name => $field) {
if ($field['type'] == 'taxonomy_term_reference' && $field['cardinality'] != 1) {
unset($vocabs[$field['settings']['allowed_values'][0]['vocabulary']]);
}
}
$form['spaces_taxonomy_machine_name'] = array(
'#type' => 'select',
'#title' => t('Spaces vocabulary'),
'#description' => t('Choose one of the following vocabularies to enable for use with Spaces.'),
'#options' => $vocabs,
'#default_value' => variable_get('spaces_taxonomy_machine_name', 0),
);
$form = system_settings_form($form);
return $form;
}