You are here

function bpc_display_commerce_bpc_post_create in Commerce Bulk Product Creation 7.2

Implements hook_commerc_bpc_post_create().

Handles the auto-creation of nodes in "auto" and "onetoone" mode.

File

modules/bpc_display/bpc_display.module, line 61
Allows automatic display node creation for Commerce bulk product creation.

Code

function bpc_display_commerce_bpc_post_create($product_ids, &$form_state) {
  $product_type = $form_state['values']['product_type'];
  $method = commerce_bpc_setting('display', 'creation_method', $product_type);
  if ($method == 'auto' || $method == 'onetoone') {
    $data['bulk_data'] = array(
      'product_type' => $product_type,
      'sku_fragment' => $form_state['values']['sku_fragment'],
      'title_fragment' => $form_state['values']['title_fragment'],
    );
    $node_type = commerce_bpc_setting('display', 'auto_content_type', $product_type);
    $title_pattern = commerce_bpc_setting('display', 'auto_node_title_pattern', $product_type);
    $node_title = token_replace($title_pattern, $data, array(
      'sanitize' => FALSE,
    ));
    if ($method == 'auto') {
      $node = bpc_display_create_node($node_type, $node_title, $product_ids);
      $form_state['commerce_bpc']['bpc_display']['nid'] = $node->nid;
    }
    elseif ($method == 'onetoone') {
      foreach ($product_ids as $product_id) {
        $data = array(
          'commerce-product' => commerce_product_load($product_id),
        );

        // Do another round of token replacement, replacing product-specific
        // tokens
        $product_node_title = token_replace($node_title, $data, array(
          'sanitize' => FALSE,
        ));
        $node = bpc_display_create_node($node_type, $product_node_title, $product_id);

        // TODO: We really should disallow the "redirect to created node" option
        // for one-to-one creation. For now, we redirect to the last node.
        $form_state['commerce_bpc']['bpc_display']['nid'] = $node->nid;
      }
    }
  }
}