You are here

function synonyms_update_7103 in Synonyms 7

Expanding Synonyms module to work with all entity types.

Namely, the following happens:

  • general "synonyms" behavior gets removed as too ambiguous.
  • alter schema of synonyms table
  • "select" and "autocomplete" behaviors undergo change in settings ("@term" placeholder is replaced with "@entity" one)
  • taxonomy term reference widgets get renamed to their new names
  • all enabled behavior implementations are renamed to their new names in order to support field- and property-based synonyms implementations

File

./synonyms.install, line 238
Install, update, and uninstall functions for the Synonyms module.

Code

function synonyms_update_7103() {
  db_delete('synonyms_settings')
    ->condition('behavior', 'synonyms')
    ->execute();
  db_drop_index('synonyms_settings', 'behavior');
  db_drop_unique_key('synonyms_settings', 'instance_behavior');
  db_add_field('synonyms_settings', 'entity_type', array(
    'description' => 'Entity type whose behavior implementation is stored in this row.',
    'type' => 'varchar',
    'length' => 255,
    'not null' => TRUE,
    'default' => '',
  ));
  db_add_field('synonyms_settings', 'bundle', array(
    'description' => 'Bundle name whose behavior implementation is stored in this row.',
    'type' => 'varchar',
    'length' => 255,
    'not null' => TRUE,
    'default' => '',
  ));
  db_add_field('synonyms_settings', 'provider', array(
    'description' => 'Provider name whose behavior implementation is stored in this row.',
    'type' => 'varchar',
    'length' => 255,
    'not null' => TRUE,
    'default' => '',
  ));
  db_query('UPDATE {synonyms_settings} s INNER JOIN {field_config_instance} i ON i.id = s.instance_id SET s.entity_type = i.entity_type, s.bundle = i.bundle, s.provider = i.field_name');
  db_drop_field('synonyms_settings', 'instance_id');
  db_add_unique_key('synonyms_settings', 'behavior_implementation', array(
    'behavior',
    'entity_type',
    'bundle',
    'provider',
  ));
  db_add_unique_key('synonyms_settings', 'all_enabled', array(
    'entity_type',
    'bundle',
    'provider',
    'behavior',
  ));
  module_enable(array(
    'synonyms_provider_field',
  ));
  $alter_behaviors = array(
    'select',
    'autocomplete',
  );
  foreach (synonyms_behavior_get_all_enabled() as $behavior_implementation) {
    db_delete('synonyms_settings')
      ->condition('provider', $behavior_implementation['provider'])
      ->execute();
    $behavior_implementation['provider'] = synonyms_provider_field_provider_name(field_info_field($behavior_implementation['provider']));
    if (in_array($behavior_implementation['behavior'], $alter_behaviors)) {
      $behavior_implementation['settings']['wording'] = str_replace('@term', '@entity', $behavior_implementation['settings']['wording']);
    }
    synonyms_behavior_implementation_save($behavior_implementation);
  }

  // Keys are the old widget names, whereas corresponding values are the new
  // widget names.
  $migrate_map = array(
    'synonyms_select' => 'synonyms_select_taxonomy_term',
    'synonyms_autocomplete' => 'synonyms_autocomplete_taxonomy_term',
  );
  foreach (field_info_field_map() as $field_name => $field_info) {
    if ($field_info['type'] == 'taxonomy_term_reference') {
      foreach ($field_info['bundles'] as $entity_type => $bundles) {
        foreach ($bundles as $bundle) {
          $instance = field_read_instance($entity_type, $field_name, $bundle);
          if (in_array($instance['widget']['type'], array_keys($migrate_map))) {
            $instance['widget']['type'] = $migrate_map[$instance['widget']['type']];
            if ($instance['widget']['type'] == 'synonyms_autocomplete_taxonomy_term' && $instance['widget']['settings']['synonyms_autocomplete_path'] == 'synonyms/autocomplete') {
              $instance['widget']['settings']['synonyms_autocomplete_path'] = 'synonyms/autocomplete-taxonomy-term';
            }
            field_update_instance($instance);
          }
        }
      }
    }
  }
}