0

The batch operation I created works fine, until it's done. Once it's done it just loops back and does it again. It will loop through until the VM runs out of resources.

I am fairly certain this has to do with my completeImport method. If I throw a syntax error in there it will finish the import successfully but then error out once it gets to that method.

The controller for the batch op: (I ommited methods that had nothing to do with the batch itself. They were for CRUD or building URLs.)

class MyModuleController {

  public function import() {
    $endpoints = array(
      'courses' => $this->getEndPoint('courses', NULL),
    );

    foreach ($endpoints as $data_type => $endpoint) {
      $response = $this->getResponse($endpoint);
      $operations = MyModuleController::processResponse($response, $data_type);

      $batch = array(
        'title' => 'Importing ' . $data_type,
        'operations' => $operations,
        'finished' => 'mymodule_complete_import',
        'init_message' => t('Importing...'),
        'progress_message' => t('Operation @current out of @total.'),
        'error_message' => t('Batch failed.'),
        'file' => drupal_get_path('module', 'mymodule') . '/mymodule.import.inc',
      );

      batch_set($batch);

      return batch_process('academy/courses/import');
    }

    return array(
      '#type' => 'markup',
      'value' => t('Imported?'),
    );
  }

  public static function processResponse($response_data, $data_type) {
    $operations = array();

    foreach ($response_data as $data) {
      switch ($data_type) {
        case 'courses':
          // Used to look up if a course was already imported.
          $id = (string) $data->Id;

          $data_array = array(
            'Name'                      => (string) $data->Name,
            'Active'                    => ((string) $data->Active == 'true' ? 1 : 0),
            'CourseCodeForBulkImport'   => (string) $data->CourseCodeForBulkImport,
            'EcommerceLongDescription'  => (string) $data->EcommerceLongDescription,
            'EcommerceShortDescription' => (string) $data->EcommerceShortDescription,
            'ForSale'                   => ((string) $data->ForSale == 'true' ? 1 : 0),
            'Code'                      => (string) $data->Code,
            'Id'                        => $id,
            'OriginalId'                => (string) $data->OriginalId,
            'Price'                     => (string) $data->Price,
            'Description'               => (string) $data->Description,
          );

          break;
      }

      $operations[] = array(
        'mymodule_response_importer',
        array($id, $data_array, $data_type),
      );
    }

    return $operations;
  }

  public static function completeImport() {
    return new RedirectResponse(Url::fromRoute('system.admin_content'));
  }

}

mymodule.import.inc:

function mymodule_response_importer($id, $data, $data_type, &$context) {
  $entity = MyModuleController::findExisting($data_type, $id);

  switch ($data_type) {
    case 'courses':
      if ($entity === FALSE) {
        MyModuleController::createNode($data);
      }
      else {
        MyModuleController::updateNode($data, NULL, $entity);
      }

      break;
  }
}

function mymodule_complete_import() {
  MyModuleController::completeImport();
}
0

I was able to fix it by using the magic send() method for RedirectResponse class and building a full URL (mostly) manually.

  public static function completeImport() {

    $url = $GLOBALS['base_secure_url'];
    $url .= $GLOBALS['base_path'];
    $url .= Url::fromRoute('system.admin_content')->getInternalPath();
    $url .= '?status=All&type=academy_webinar&title=&langcode=All';

    $redirect = new RedirectResponse($url);

    $redirect->send();
  }
  • I am using batch_set($batch); for setting batch inside submitForm() function in admin configuration page. But my batch process is not initiating instead of simple for submission happens. Am I missing anything? Do I need to include any particular class in my page? – ARUN Jul 27 '16 at 8:48
  • @ARUN it doesn't sound like you're having the same issue I was. You'd be better off creating your own question to provide more details. – CR47 Jul 27 '16 at 13:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.