You are here

function farm_log_update_7002 in farmOS 7

Migrate Areas and Assets fields to Area and Asset field.

File

modules/farm/farm_log/farm_log.install, line 163
Farm Log install file.

Code

function farm_log_update_7002(&$sandbox) {

  // Start by reverting the fields component of this module
  // so that the new Area and Asset fields are available.
  features_revert(array(
    'farm_log' => array(
      'field',
    ),
  ));

  // Load all activity logs.
  $query = new EntityFieldQuery();
  $query
    ->entityCondition('entity_type', 'log');
  $query
    ->entityCondition('bundle', 'farm_activity');
  $result = $query
    ->execute();
  if (isset($result['log'])) {
    $ids = array_keys($result['log']);
    $logs = entity_load('log', $ids);
  }

  // If logs were loaded, iterate through them...
  if (!empty($logs)) {
    foreach ($logs as $log) {

      // Copy the first area, discard the rest.
      if (!empty($log->field_farm_areas[LANGUAGE_NONE][0])) {
        $log->field_farm_area[LANGUAGE_NONE][] = $log->field_farm_areas[LANGUAGE_NONE][0];
      }

      // Copy the first asset, discard the rest.
      if (!empty($log->field_farm_assets[LANGUAGE_NONE][0])) {
        $log->field_farm_asset[LANGUAGE_NONE][] = $log->field_farm_assets[LANGUAGE_NONE][0];
      }

      // Save the log.
      log_save($log);
    }
  }

  // Delete the areas field.
  $field = field_info_instance('log', 'field_farm_areas', 'farm_activity');
  field_delete_instance($field);

  // Delete the assets field.
  $field = field_info_instance('log', 'field_farm_assets', 'farm_activity');
  field_delete_instance($field);
}