You are here

function units_update_7103 in Units of Measurement 7.2

Same name and namespace in other branches
  1. 7 units.install \units_update_7103()

Adapt existing measures to CTools units converters plugin scheme.

File

./units.install, line 239
Install and uninstall hooks of the Units module.

Code

function units_update_7103() {
  $new_field = 'converter';
  $old_field = 'convert_callback';
  db_add_field('units_measure', $new_field, array(
    'type' => 'varchar',
    'length' => 255,
    'not null' => TRUE,
    'default' => '',
    'description' => 'Name of cTools units converter plugin responsible for converting units in this measure.',
  ));
  $outstanding_measures = db_select('units_measure', 'm')
    ->fields('m', array(
    'label',
  ))
    ->condition($old_field, 'units_convert', '<>')
    ->execute()
    ->fetchCol();
  db_update('units_measure')
    ->fields(array(
    $new_field => 'linear',
  ))
    ->condition($old_field, 'units_convert')
    ->execute();
  db_drop_field('units_measure', $old_field);

  // For some reason without this clear cache the schema wouldn't get updated
  // to the latest columns.
  drupal_static_reset();
  _cache_get_object('cache')
    ->clear('schema', TRUE);
  if (empty($outstanding_measures)) {
    return t('Successfully migrated measures onto the new conversion logic.');
  }
  else {
    return t('Successfully migrated measures onto the new conversion logic. The following measures likely to need a new conversion logic: %measures.', array(
      '%measures' => implode(', ', $outstanding_measures),
    ));
  }
}