diff -u b/includes/common.inc b/includes/common.inc --- b/includes/common.inc +++ b/includes/common.inc @@ -11,7 +11,8 @@ * modules to provide their own meaningful defaults. * * @param $config_id - * A config id. + * A config id, typically of the form "module-someid", e.g. + * "taxonomy-field_name". * @param $defaults_override * Optionally override the defaults. */ diff -u b/modules/hs_taxonomy.install b/modules/hs_taxonomy.install --- b/modules/hs_taxonomy.install +++ b/modules/hs_taxonomy.install @@ -92,19 +92,16 @@ foreach (field_info_instances() as $entity_type => $bundles) { foreach ($bundles as $bundle => $field_list) { - foreach ($field_list as $fieldname => $instance) { + foreach ($field_list as $field_name => $instance) { if ($instance['widget']['type'] == 'taxonomy_hs') { - $field_info = field_info_field($fieldname); - $allowed_values = $field_info['settings']['allowed_values']; - foreach ($allowed_values as $allowed_value) { - $vocabulary_name = $allowed_value['vocabulary']; - $old_config_id = "taxonomy-{$vocabulary_name}"; - $new_config_id = "taxonomy-{$vocabulary_name}-{$fieldname}"; - $old_config = hierarchical_select_common_config_get($old_config_id); - if (!empty($old_config)) { - hierarchical_select_common_config_set($new_config_id, $old_config); - hierarchical_select_common_config_del($old_config_id); - } + $field_info = field_info_field($field_name); + $allowed_value = $field_info['settings']['allowed_values'][0]; + $vocabulary_name = $allowed_value['vocabulary']; + $old_config_id = "taxonomy-{$vocabulary_name}"; + $new_config_id = "taxonomy-{$field_name}"; + $old_config = hierarchical_select_common_config_get($old_config_id); + if (!empty($old_config)) { + hierarchical_select_common_config_set($new_config_id, $old_config); } } } diff -u b/modules/hs_taxonomy.module b/modules/hs_taxonomy.module --- b/modules/hs_taxonomy.module +++ b/modules/hs_taxonomy.module @@ -56,7 +56,7 @@ 'root_term' => NULL, 'entity_count_for_node_type' => NULL, ); - $config_id = hs_taxonomy_get_config_id($vocabulary, $form['#field_name']); + $config_id = hs_taxonomy_get_config_id($form['#field_name']); $defaults = array( // Enable the save_lineage setting by default if the multiple parents // vocabulary option is enabled. @@ -109,8 +109,7 @@ */ function hs_taxonomy_field_settings_submit(&$form, &$form_state) { $field = field_info_field($form['#field_name']); - $vocabulary = taxonomy_vocabulary_machine_name_load($field['settings']['allowed_values'][0]['vocabulary']); - $config = hierarchical_select_common_config_get(hs_taxonomy_get_config_id($vocabulary, $form['#field_name'])); + $config = hierarchical_select_common_config_get(hs_taxonomy_get_config_id($form['#field_name'])); if ($config['dropbox']['status'] || $config['save_lineage']) { $field = field_info_field($form['#field_name']); @@ -132,8 +131,7 @@ if (isset($form['#field']['type']) && $form['#field']['type'] === 'taxonomy_term_reference' && $form['#instance']['widget']['type'] == 'taxonomy_hs') { require_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'hierarchical_select') . '/includes/common.inc'; - $vocabulary = taxonomy_vocabulary_machine_name_load($form['#field']['settings']['allowed_values'][0]['vocabulary']); - $config = hierarchical_select_common_config_get(hs_taxonomy_get_config_id($vocabulary, $form['#field']['field_name'])); + $config = hierarchical_select_common_config_get(hs_taxonomy_get_config_id($form['#field']['field_name'])); if ($config['dropbox']['status'] || $config['save_lineage']) { $form['field']['cardinality']['#disabled'] = TRUE; @@ -193,51 +191,13 @@ } /** - * Implements hook_taxonomy_vocabulary_update(). + * Implements hook_field_delete(). * - * This enables us to rename HS configs appropriately when machine names of - * taxonomy vocabularies change. - * - * @param stdClass $vocabulary - * The vocabulary that is being updated. + * This enables us to delete HS configs when fields are deleted. */ -function hs_taxonomy_taxonomy_vocabulary_update(stdClass $vocabulary) { +function hs_taxonomy_field_delete($entity_type, $entity, $field, $instance, $langcode, &$items) { require_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'hierarchical_select') . '/includes/common.inc'; - - if (isset($vocabulary->original) && ($vocabulary->machine_name != $vocabulary->original->machine_name)) { - // Get all existing config_ids in variable table for this vocabulary. - $old_config_ids = hs_taxonomy_get_config_ids($vocabulary->original); - // Iterating over existing config_ids, get full config details, then assign it to our new config_id. - foreach($old_config_ids as $old_config_id) { - $config = hierarchical_select_common_config_get($old_config_id); - if (!empty($config)) { - $new_config_id = hs_taxonomy_update_config_id($old_config_id, $vocabulary); - // Make sure we pass the new config_id through to the variable content as well. - $config['config_id'] = $new_config_id; - hierarchical_select_common_config_set($new_config_id, $config); - // Finally, delete the old config variable. - hierarchical_select_common_config_del($old_config_id); - } - } - } -} - -/** - * Implements hook_taxonomy_vocabulary_delete(). - * - * This enables us to delete HS configs when vocabularies are deleted. - * - * @param stdClass $vocabulary - * The vocabulary that is being deleted. - */ -function hs_taxonomy_taxonomy_vocabulary_delete(stdClass $vocabulary) { - require_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'hierarchical_select') . '/includes/common.inc'; - - // Get all configs belonging to this vocabulary and delete each one. - $config_ids = hs_taxonomy_get_config_ids($vocabulary); - foreach($config_ids as $config_id) { - hierarchical_select_common_config_del($config_id); - } + hierarchical_select_common_config_del(hs_taxonomy_get_config_id($field['field_name'])); } //---------------------------------------------------------------------------- @@ -302,7 +262,7 @@ '#default_value' => $tids, ); - hierarchical_select_common_config_apply($element, hs_taxonomy_get_config_id($vocabulary, $field['field_name'])); + hierarchical_select_common_config_apply($element, hs_taxonomy_get_config_id($field['field_name'])); // Append another #process callback that transforms #return_value to the // format that Field API/Taxonomy Field expects. @@ -402,7 +362,7 @@ // Get the config for this field. module_load_include('inc', 'hierarchical_select', 'includes/common'); - $config_id = hs_taxonomy_get_config_id($vocabulary, $field['field_name']); + $config_id = hs_taxonomy_get_config_id($field['field_name']); $config = hierarchical_select_common_config_get($config_id); $config += array( 'module' => 'hs_taxonomy', @@ -834,7 +794,7 @@ $entity_info = entity_get_info($entity_type); $machine_name = $field['settings']['allowed_values'][0]['vocabulary']; $vocabulary = taxonomy_vocabulary_machine_name_load($machine_name); - $config_id = hs_taxonomy_get_config_id($vocabulary, $field_name); + $config_id = hs_taxonomy_get_config_id($field_name); $config_info[$config_id] = array( 'config_id' => $config_id, 'hierarchy type' => t('Taxonomy'), @@ -1104,68 +1064,16 @@ } /** - * Returns the configuration ID that would be used for the specified taxonomy - * vocabulary and field. + * Returns the configuration ID that would be used for the specified field. * - * @param stdClass $vocabulary - * The vocabulary. * @param string $field_name - * The field_machine name. + * The field machine name. * * @return string - * The config id for the provided vocabulary and field. + * The config id for the provided field. */ -function hs_taxonomy_get_config_id(stdClass $vocabulary, $field_name) { - return "taxonomy-{$vocabulary->machine_name}-{$field_name}"; -} - -/** - * Returns all the configuration IDs that have been used for the specified taxonomy - * vocabulary. - * - * @param stdClass $vocabulary - * The vocabulary for which config IDs will be returned. - * - * @return Array - * The config IDs for the provided vocabulary. - */ -function hs_taxonomy_get_config_ids(stdClass $vocabulary) { - // get _all_ hs_config_taxonomy variables from the db for our chosen vocab: - $prefix = "hs_config_taxonomy-{$vocabulary->machine_name}"; - $result = db_select('variable', 'v') - ->fields('v', array('name')) - ->condition('name', db_like($prefix) . '%', 'LIKE') - ->execute() - ->fetchCol(); - - // now iterate over variable names, remove the hs_config_ prefix and - // we are left with the config_id - $config_ids = array(); - foreach($result as $name) { - $config_ids[] = preg_replace('/^(hs_config_)(.*)$/', '$2', $name); - } - return $config_ids; -} - -/** - * Helper function to return the updated config_id on vocabulary change. - * - * @param string $old_config_id - * The original config_id string. - * @param stdClass $vocabulary - * The new vocabulary if this has been updated. - * @return string - * The new config id string including the new vocabulary name. - */ -function hs_taxonomy_update_config_id($old_config_id, $new_vocabulary = null) { - $new_config_id = $old_config_id; - - // Replace the vocabulary name component of the config_id with our new vocabulary name - if (isset($new_vocabulary) && !empty($new_vocabulary)) { - $new_config_id = preg_replace('/^(.*)-(.*)-(.*)$/', "$1-{$new_vocabulary->machine_name}-$3", $new_config_id); - } - - return $new_config_id; +function hs_taxonomy_get_config_id($field_name) { + return "taxonomy-{$field_name}"; } /**