You are here

function userreference_update_6001 in Content Construction Kit (CCK) 6.2

Same name and namespace in other branches
  1. 6.3 modules/userreference/userreference.install \userreference_update_6001()

Create an index by user reference column for all fields.

File

modules/userreference/userreference.install, line 59
Implementation of hook_install().

Code

function userreference_update_6001(&$sandbox) {
  include_once './' . drupal_get_path('module', 'content') . '/content.install';
  drupal_load('module', 'content');
  $ret = array();
  if (!isset($sandbox['progress'])) {
    if ($abort = content_check_update('userreference')) {
      return $abort;
    }

    // Get the latest cache values and schema.
    content_clear_type_cache(TRUE, TRUE);
    $types = content_types_install();
    if (empty($types)) {
      return $ret;
    }
    $sandbox['fields'] = array();
    foreach ($types as $type_name => $fields) {
      foreach ($fields as $field) {
        if ($field['type'] == 'userreference') {
          $sandbox['fields'][] = $field;
        }
      }
    }
    if (empty($sandbox['fields'])) {
      return $ret;
    }
    $sandbox['progress'] = 0;
    $sandbox['visited'] = array();
  }
  $field = $sandbox['fields'][$sandbox['progress']];

  // We only want to process a field once -- if we hit it a second time,
  // that means it's its own table and it should have already been updated.
  if (!in_array($field['field_name'], $sandbox['visited'])) {
    $db_info = content_database_info($field);
    $table = $db_info['table'];
    $attributes = $db_info['columns']['uid'];
    $column = $attributes['column'];
    if (!content_db_index_exists($table, $column)) {
      db_add_index($ret, $table, $column, array(
        $column,
      ));
    }
    $sandbox['visited'][] = $field['field_name'];
  }
  $sandbox['progress']++;
  $ret['#finished'] = $sandbox['progress'] / count($sandbox['fields']);
  return $ret;
}