You are here

function uuid_commerce_product_features_rebuild_products in UUID Features Integration 7

Runs the product import multiple times to resolve dependencies.

We might need several runs of ths function to resolve the dependencies created by reference fields. Those can only be resolved if the target product already exists.

Parameters

array $products: The products to process.

string $module: The module to rebuild for.

int $max_nesting: Maximal nesting level.

int $nesting_level: Current nesting level.

Return value

bool TRUE if all products could be restored.

1 call to uuid_commerce_product_features_rebuild_products()
uuid_commerce_product_features_rebuild in includes/uuid_commerce_product.features.inc
Implements hook_features_rebuild().

File

includes/uuid_commerce_product.features.inc, line 176
Features hooks for the uuid_commerce_product features component.

Code

function uuid_commerce_product_features_rebuild_products($products, $module, $max_nesting = 5, $nesting_level = 0) {

  // Max nesting level hit.
  if ($max_nesting < $nesting_level) {
    watchdog('UUID Features', 'Unable to restore commerce products. Max nesting level reached.', array(), WATCHDOG_ERROR);
    return FALSE;
  }
  $second_run_products = array();
  $entity_info = entity_get_info('commerce_product');
  foreach ($products as $data) {
    try {

      // Double-check that bean can be created/reverted.
      if (!isset($entity_info['bundles'][$data['type']])) {
        drupal_set_message('Bundle not found for commerce product of type ' . $data['type'] . '. Product was not created/reverted.', 'warning');
      }
      else {

        // If this is an update, there will be a by-UUID matching product.
        $existing = entity_get_id_by_uuid('commerce_product', array(
          $data['uuid'],
        ));
        if (!empty($existing)) {
          $product = entity_load_single('commerce_product', $existing[$data['uuid']]);
          foreach ($data as $key => $value) {
            $product->{$key} = $value;
          }
        }
        else {

          // Create a new product.
          $product = entity_create('commerce_product', $data);
        }
        $entity_type = 'commerce_product';
        drupal_alter('uuid_entity_features_rebuild', $entity_type, $product, $data, $module);
        drupal_alter('uuid_commerce_product_features_rebuild', $product, $module);
        uuid_features_file_field_import($product, 'commerce_product');
        commerce_product_save($product);
      }
    } catch (Exception $e) {
      $second_run_products[] = $data;
    }
  }
  if (!empty($second_run_products)) {
    return uuid_commerce_product_features_rebuild_products($second_run_products, $module, $max_nesting, ++$nesting_level);
  }
  return TRUE;
}