From 88f59c8bab093f71de552eacfb45f0c42c5ac631 Mon Sep 17 00:00:00 2001
From: Rosenstrauch <Rosenstrauch@2318810.no-reply.drupal.org>
Date: Wed, 4 Jun 2014 16:45:52 +0200
Subject: [PATCH] Issue #1054702 implement suggestions for default value
 prepopulation

---
 commerce_bpc.module | 86 ++++++++++++++++++++++++++++++++++-------------------
 1 file changed, 56 insertions(+), 30 deletions(-)

diff --git a/commerce_bpc.module b/commerce_bpc.module
index 1be3f98..13edb07 100644
--- a/commerce_bpc.module
+++ b/commerce_bpc.module
@@ -197,42 +197,48 @@ function commerce_bpc_field_delete_instance($instance) {
   }
 }
 
+
 /**
- * Implements  hook_form_BASE_FORM_ID_alter().
+ * Implements  hook_default_value().
  *
  * Prepopulate product reference fields in the node creation form after
  * bulk creation.
  */
-function commerce_bpc_form_node_form_alter(&$form, &$form_state, $form_id) {
-  // If we reach the node form after being redirected from the bulk
-  // creation form, there will be a GET parameter that contains a
-  // unique ID telling us where to find the IDs of the created products
-  // in the session array.
-  if (isset($_GET['bulk_creation_id'])
-  && isset($_SESSION['bulk_product_ids'][$_GET['bulk_creation_id']])) {
-
-    $ids = $_SESSION['bulk_product_ids'][$_GET['bulk_creation_id']];
-    $form['title']['#default_value'] = $_SESSION['bulk_title'][$_GET['bulk_creation_id']];
-
-    $field_name = _commerce_bpc_get_reference_field_name($form['type']['#value']);
-    $field = field_info_field($field_name);
-
-    switch ($form[$field_name][LANGUAGE_NONE]['#type']) {
-      case 'textfield':
-        // The autocomplete text field uses SKUs, so we need to retrieve those.
-        $products = commerce_product_load_multiple($ids);
-        $skus = array();
-        foreach ($products as $product) {
-          $skus[] = $product->sku;
-        }
-        $form[$field_name][LANGUAGE_NONE]['#default_value'] = implode(', ', $skus);
-        break;
-
-      case 'select':
-      case 'checkboxes':
-        $form[$field_name][LANGUAGE_NONE]['#default_value'] = $ids;
-        break;
+function commerce_bpc_default_value($entity_type, $entity, $field, $instance, $langcode) {
+
+  if ($instance['field_name'] == _commerce_bpc_get_reference_field_name($instance['bundle'])) {
+    return commerce_bpc_commerce_product_default_value($entity_type, $entity, $field, $instance, $langcode);
+
+  }
+  if ($items = commerce_bpc_get_values($field, $instance)) {
+    // TODO: Check field cardinality.
+    return $items;
+  }
+}
+/**
+ * function for $instance['default_value_function'] that takes into account the $_GET / $_SESSION values.
+ */
+function commerce_bpc_get_values($field, $instance, $flat_array = FALSE) {
+  $ids = $_SESSION['bulk_product_ids'][$_GET['bulk_creation_id']];
+  return $ids;
+}
+
+function commerce_bpc_commerce_product_default_value($entity_type, $entity, $field, $instance, $langcode) {
+  if ($items = commerce_bpc_get_values($field, $instance)) {
+    $products = array();
+    foreach ($items as $item) {
+      // Determine the correct weight of the new element.
+      $weight = 0;
+
+      $products[] = array(
+       // 'entity' => commerce_product_load($item),
+        'product_id' => $item,
+        'weight' => $weight,
+        'form' => NULL,
+        'needs_save' => FALSE,
+      );
     }
+    return $products;
   }
 }
 
@@ -611,3 +617,23 @@ function _commerce_bpc_get_reference_field_name($node_type) {
     }
   }
 }
+
+/**
+ * update commerce_product_reference fields with default_value_function
+ * does this update the product_reference instances on install?
+ */
+function commerce_bpc_update_7101() {
+  $types = commerce_bpc_get_node_types($product_type = FALSE);
+
+  foreach ($types as $key => $type) {
+    $field_name = _commerce_bpc_get_reference_field_name($key);
+    $instances = field_info_instances('node', $key);
+    foreach ($instances as $instance_name => $instance) {
+      if ($field_name == $instance_name) {
+        $instance['default_value_function'] = 'commerce_bpc_default_value';
+        field_update_instance($instance);
+      }
+    }
+  }
+  return t('updated commerce references');
+}
-- 
1.8.4.2

