You are here

function drush_title_upgrade_title_fields in Title 7

Command callback for upgrade-title-fields.

File

./title.drush.inc, line 42
Drush commands for Title module.

Code

function drush_title_upgrade_title_fields() {
  $entity_type = 'node';
  $entity_info = entity_get_info($entity_type);
  if (empty($entity_info['field replacement'])) {
    drush_log(t('There are no available title fields to upgrade.'), 'error');
    return FALSE;
  }
  $field_replacement_info = $entity_info['field replacement'];
  $upgraded = $failed = $skipped = 0;
  foreach (title_content_types() as $bundle) {
    $dt_args['@bundle'] = $bundle;
    $field_instances = field_info_instances($entity_type, $bundle);
    $extra_fields = field_info_extra_fields($entity_type, $bundle, 'form');
    $possible_fields = array_merge($field_instances, $extra_fields);
    if ($info = array_intersect_key($field_replacement_info, $possible_fields)) {
      foreach (array_keys($info) as $legacy_field) {
        $dt_args['@legacy_field'] = $legacy_field;
        if (drush_invoke_process('@self', 'upgrade-title-field', array(
          $entity_type,
          $bundle,
          $legacy_field,
        ))) {
          $upgraded++;
        }
        else {
          $failed++;
        }
      }
    }
    else {
      $skipped++;
      drush_log(dt('Title fields have already been upgraded on content type @bundle.', $dt_args), 'ok');
    }
  }
  $messages = array();
  if ($upgraded) {
    $dt_args['@upgraded'] = $upgraded;
    $messages[] = 'Upgraded @upgraded title field(s).';
  }
  if ($skipped) {
    $dt_args['@skipped'] = $skipped;
    $messages[] = 'Skipped @skipped title field(s).';
  }
  if ($failed) {
    $dt_args['@failed'] = $failed;
    $messages[] = '@failed failed.';
  }
  $message = empty($messages) ? 'No fields upgraded.' : implode(' ', $messages);
  drush_log(dt($message, $dt_args), 'completed');
  if ($failed) {
    return FALSE;
  }
}