From 2406d299d13abe7886bbac902f71baf010a61444 Mon Sep 17 00:00:00 2001
From: jucallme <jucallme@gmail.com>
Date: Tue, 10 Apr 2012 16:01:48 -0300
Subject: [PATCH] rerolled patch against latest head

---
 includes/commerce_line_item.type.inc |  126 ++++++++++++++++++++++++++++++++++
 inline_entity_form.module            |   20 +++++-
 2 files changed, 145 insertions(+), 1 deletions(-)
 create mode 100644 includes/commerce_line_item.type.inc

diff --git a/includes/commerce_line_item.type.inc b/includes/commerce_line_item.type.inc
new file mode 100644
index 0000000..961520d
--- /dev/null
+++ b/includes/commerce_line_item.type.inc
@@ -0,0 +1,126 @@
+<?php
+
+/**
+ * @file
+ * Provides functionality for inline managing commerce line items.
+ */
+
+function inline_entity_form_commerce_line_item_default_fields($types) {
+  $fields = array();
+
+  $fields['line_item_label'] = array(
+    'type' => 'extra_field',
+    'label' => t('Line item'),
+    'visible' => TRUE,
+    'weight' => 1,
+  );
+
+  $fields['quantity'] = array(
+    'type' => 'extra_field',
+    'label' => t('Quantity'),
+    'visible' => TRUE,
+    'weight' => 1,
+  );
+
+  return $fields;
+}
+
+/**
+ * IEF add/edit form callback: Returns the line item form to be embedded.
+ */
+function inline_entity_form_commerce_line_item_form($form, &$form_state) {
+  $line_item = $form['#entity'];
+
+  $form['#element_validate'] = array(
+    'inline_entity_form_commerce_line_item_form_validate',
+    'inline_entity_form_process_submit',
+  );
+  $form['#element_submit'] = array(
+    'inline_entity_form_commerce_line_item_form_submit',
+  );
+  // Ensure this include file is loaded when the form is rebuilt from the cache.
+  $form_state['build_info']['files']['inline_form'] = drupal_get_path('module', 'inline_entity_form') . '/includes/commerce_line_item.type.inc';
+
+  $form['#pre_render'][] = 'inline_entity_form_pre_render_add_fieldset_markup';
+  $form['line_item_details'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Line item details'),
+    '#attributes' => array('class' => array('ief-line_item-details')),
+  );
+  $form['line_item_label'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Line item label'),
+    '#description' => t('Supply the line item label to be used for this line item.'),
+    '#default_value' => $line_item->line_item_label,
+    '#maxlength' => 128,
+    '#required' => TRUE,
+    '#fieldset' => 'line_item_details',
+  );
+  $form['quantity'] = array(
+    '#type' => 'textfield',
+    '#datatype' => 'integer',
+    '#title' => t('Quantity'),
+    '#description' => t('The quantity of line items.'),
+    '#default_value' => (int) $line_item->quantity,
+    '#size' => 4,
+    '#maxlength' => max(4, strlen($line_item->quantity)),
+    '#required' => TRUE,
+    '#fieldset' => 'line_item_details',
+  );
+  field_attach_form('commerce_line_item', $line_item, $form, $form_state, LANGUAGE_NONE);
+
+  // Add all fields to the main fieldset.
+  foreach (field_info_instances('commerce_line_item', $line_item->type) as $a => $instance) {
+    $form[$instance['field_name']]['#fieldset'] = 'line_item_details';
+  }
+
+  return $form;
+}
+
+/**
+ * IEF add/edit form validation callback.
+ */
+function inline_entity_form_commerce_line_item_form_validate($form, &$form_state) {
+  $line_item = $form['#entity'];
+
+  $parents_path = implode('][', $form['#parents']);
+  $line_item_values = drupal_array_get_nested_value($form_state['values'], $form['#parents']);
+  $line_item_label = trim($line_item_values['line_item_label']);
+  $quantity = $line_item_values['quantity'];
+  // Trim leading and trailing whitespace from the line item label.
+  drupal_array_set_nested_value($form_state['values'], array_merge($form['#parents'], array('line_item_label')), $line_item_label);
+
+  if (!is_numeric($quantity) || $quantity <= 0) {
+    form_set_error($parents_path . '][quantity', t('You must specify a positive number for the quantity'));
+  }
+  elseif ($form['quantity']['#datatype'] == 'integer' &&
+    (int) $quantity != $quantity) {
+    form_set_error($parents_path . '][quantity', t('You must specify a whole number for the quantity.'));
+  }
+
+  field_attach_form_validate('commerce_line_item', $line_item, $form, $form_state);
+}
+
+/**
+ * IEF add/edit form submit callback: Modifies the passed-in line item before it
+ * is saved.
+ */
+function inline_entity_form_commerce_line_item_form_submit($form, &$form_state) {
+  $line_item = $form['#entity'];
+  $line_item_values = drupal_array_get_nested_value($form_state['values'], $form['#parents']);
+  $line_item->line_item_label = $line_item_values['line_item_label'];
+  $line_item->quantity = $line_item_values['quantity'];
+  field_attach_submit('commerce_line_item', $line_item, $form, $form_state);
+}
+
+/**
+ * IEF delete form callback: Returns the confirmation message.
+ */
+function inline_entity_form_commerce_line_item_delete_form($form, $form_state) {
+  $line_item = $form['#entity'];
+  $form['message'] = array(
+    '#markup' => '<div>' . t('Are you sure you want to delete %title?', array('%title' => $line_item->line_item_label)) . '</div>',
+  );
+
+  return $form;
+}
diff --git a/inline_entity_form.module b/inline_entity_form.module
index e957bbe..f3b396a 100644
--- a/inline_entity_form.module
+++ b/inline_entity_form.module
@@ -30,6 +30,20 @@ function inline_entity_form_inline_entity_type_info() {
       'base' => drupal_get_path('module', 'inline_entity_form') . '/includes/entity_types/commerce-product.css',
     ),
   );
+  $types['commerce_line_item'] = array(
+    'file' => drupal_get_path('module', 'inline_entity_form') . '/includes/commerce_line_item.type.inc',
+    'callbacks' => array(
+      'default fields' => 'inline_entity_form_commerce_line_item_default_fields',
+      'form' => 'inline_entity_form_commerce_line_item_form',
+      'delete form' => 'inline_entity_form_commerce_line_item_delete_form',
+    ),
+    'labels' => array(
+      'add fieldset' => t('Add new line item'),
+      'add button' => t('Add line item'),
+      'save button' => t('Save line item'),
+    ),
+    'empty text' => t('No line items found.'),
+  );
 
   return $types;
 }
@@ -127,7 +141,7 @@ function inline_entity_form_field_widget_info() {
 
   $widgets['inline_entity_form'] = array(
     'label' => t('Inline entity form'),
-    'field types' => array('commerce_product_reference', 'entityreference'),
+    'field types' => array('commerce_line_item_reference', 'commerce_product_reference', 'entityreference'),
     'settings' => array('fields' => array()),
     'behaviors' => array(
       'multiple values' => FIELD_BEHAVIOR_CUSTOM,
@@ -175,6 +189,10 @@ function inline_entity_form_settings($field, $instance) {
       }
     }
   }
+  elseif ($field['type'] == 'commerce_line_item_reference') {
+    $settings['entity_type'] = 'commerce_line_item';
+    $settings['column'] = 'line_item_id';
+  }
 
   // By default, allow entities of all bundles to be created.
   if (empty($settings['bundles'])) {
-- 
1.7.5.4

