You are here

function drush_entity_translation_upgrade in Entity Translation 7

Implements drush_hook_COMMAND().

Parameters

The content type of which the nodes shall be upgraded:

Runs the batch upgrading nodes of the specified content_type to Entity Translation. Lets user choose content type from a list, if argument has not been provided.

File

entity_translation_upgrade/entity_translation_upgrade.drush.inc, line 56

Code

function drush_entity_translation_upgrade($content_type = "") {

  // Get all installed content types.
  $available_types = array();
  $available_types_chose = array();
  $available_types_str = '';
  foreach (node_type_get_types() as $type) {
    $available_types[$type->type] = $type->type;
    $available_types_chose[$type->type] = $type->name;
    if (strlen($available_types_str) > 0) {
      $available_types_str .= ', ';
    }
    $available_types_str .= $type->type;
  }

  // If argument content_type is empty, prompt user for content type.
  if (!$content_type) {
    $content_type = drush_choice($available_types_chose, dt('Choose the content type of the nodes to be upgraded to Entity Translation:'));
  }

  // Do content type argument checks.
  if (!$content_type) {
    return TRUE;
  }
  if (strlen($available_types_str) == 0) {
    return drush_set_error(dt('Entity Translation Upgrade cannot run as no content type has been installed.'));
  }
  if (!in_array($content_type, $available_types)) {
    return drush_set_error(dt('"@content_type" is not a valid content type machine name. Please use one of these installed content types as argument: @available_types_str.', array(
      '@content_type' => $content_type,
      '@available_types_str' => $available_types_str,
    )));
  }

  // Start batch to upgrade nodes of the specified content type.
  $types = array(
    $content_type => $content_type,
  );
  $batch = array(
    'operations' => array(
      array(
        'entity_translation_upgrade_do',
        array(
          $types,
        ),
      ),
      array(
        'entity_translation_upgrade_complete',
        array(),
      ),
    ),
    'finished' => 'entity_translation_upgrade_drush_end',
    'file' => drupal_get_path('module', 'entity_translation_upgrade') . '/entity_translation_upgrade.admin.inc',
    'progressive' => FALSE,
  );
  batch_set($batch);
  drush_backend_batch_process();
}