commit 40f74a558597e36261a7153022970078ad883942
Author: Dave Long <dave@longwaveconsulting.com>
Date:   Wed Dec 18 08:50:32 2013 +0000

    Convert product stock edit table to table form element.

diff --git a/uc_stock/lib/Drupal/uc_stock/Form/StockEditForm.php b/uc_stock/lib/Drupal/uc_stock/Form/StockEditForm.php
index 5750c3b..404c355 100644
--- a/uc_stock/lib/Drupal/uc_stock/Form/StockEditForm.php
+++ b/uc_stock/lib/Drupal/uc_stock/Form/StockEditForm.php
@@ -26,52 +26,40 @@ class StockEditForm extends FormBase {
    * {@inheritdoc}
    */
   public function buildForm(array $form, array &$form_state, NodeInterface $node = NULL) {
-    $form['stock'] = array('#tree' => TRUE);
-
-    $skus = uc_product_get_models($node->id());
-
-    // Remove 'Any'.
-    unset($skus[NULL]);
-
-    if (!$skus) {
-      drupal_set_message(t('No SKU found.'), 'error');
-    }
-    else {
-      foreach (array_values($skus) as $id => $sku) {
-        $stock = db_query("SELECT * FROM {uc_product_stock} WHERE sku = :sku", array(':sku' => $sku))->fetchAssoc();
-
-        $form['stock'][$id]['sku'] = array(
-          '#type' => 'value',
-          '#value' => $sku,
-        );
-
-        // Checkbox to mark this as active.
-        $form['stock'][$id]['active'] = array(
-          '#type' => 'checkbox',
-          '#default_value' => !empty($stock['active']) ? $stock['active'] : 0,
-        );
-
-        // Sanitized version of the SKU for display.
-        $form['stock'][$id]['display_sku'] = array(
-          '#markup' => check_plain($sku),
-        );
-
-        // Textfield for entering the stock level.
-        $form['stock'][$id]['stock'] = array(
-          '#type' => 'textfield',
-          '#default_value' => !empty($stock['stock']) ? $stock['stock'] : 0,
-          '#maxlength' => 9,
-          '#size' => 9,
-        );
-
-        // Textfield for entering the threshold level.
-        $form['stock'][$id]['threshold'] = array(
-          '#type' => 'textfield',
-          '#default_value' => !empty($stock['threshold']) ? $stock['threshold'] : 0,
-          '#maxlength' => 9,
-          '#size' => 9,
-        );
-      }
+    $form['stock'] = array(
+      '#type' => 'table',
+      '#header' => array(
+        array('data' => '&nbsp;&nbsp;' . $this->t('Active'), 'class' => array('select-all')),
+        $this->t('SKU'),
+        $this->t('Stock'),
+        $this->t('Threshold'),
+      ),
+    );
+    $form['#attached']['library'][] = array('system', 'drupal.tableselect');
+
+    $skus = uc_product_get_models($node->id(), FALSE);
+    foreach ($skus as $sku) {
+      $stock = db_query("SELECT * FROM {uc_product_stock} WHERE sku = :sku", array(':sku' => $sku))->fetchAssoc();
+
+      $form['stock'][$sku]['active'] = array(
+        '#type' => 'checkbox',
+        '#default_value' => !empty($stock['active']) ? $stock['active'] : 0,
+      );
+      $form['stock'][$sku]['sku'] = array(
+        '#markup' => check_plain($sku),
+      );
+      $form['stock'][$sku]['stock'] = array(
+        '#type' => 'textfield',
+        '#default_value' => !empty($stock['stock']) ? $stock['stock'] : 0,
+        '#maxlength' => 9,
+        '#size' => 9,
+      );
+      $form['stock'][$sku]['threshold'] = array(
+        '#type' => 'textfield',
+        '#default_value' => !empty($stock['threshold']) ? $stock['threshold'] : 0,
+        '#maxlength' => 9,
+        '#size' => 9,
+      );
     }
 
     $form['nid'] = array(
@@ -92,18 +80,18 @@ class StockEditForm extends FormBase {
    * {@inheritdoc}
    */
   public function submitForm(array &$form, array &$form_state) {
-    foreach (element_children($form_state['values']['stock']) as $id) {
-      $stock = $form_state['values']['stock'][$id];
+    foreach (element_children($form_state['values']['stock']) as $sku) {
+      $stock = $form_state['values']['stock'][$sku];
 
       db_merge('uc_product_stock')
-        ->key(array('sku' => $stock['sku']))
+        ->key(array('sku' => $sku))
         ->updateFields(array(
           'active' => $stock['active'],
           'stock' => $stock['stock'],
           'threshold' => $stock['threshold'],
         ))
         ->insertFields(array(
-          'sku' => $stock['sku'],
+          'sku' => $sku,
           'active' => $stock['active'],
           'stock' => $stock['stock'],
           'threshold' => $stock['threshold'],
diff --git a/uc_stock/uc_stock.admin.inc b/uc_stock/uc_stock.admin.inc
index ba5b864..17f4c77 100644
--- a/uc_stock/uc_stock.admin.inc
+++ b/uc_stock/uc_stock.admin.inc
@@ -142,38 +142,3 @@ function uc_stock_report_form_submit($form, &$form_state) {
     drupal_goto('admin/store/reports/stock');
   }
 }
-
-/**
- * Returns HTML for uc_stock_edit_form().
- *
- * @param $variables
- *   An associative array containing:
- *   - form: A render element representing the form.
- *
- * @see uc_stock_edit_form()
- * @see uc_stock_edit_form_submit()
- * @ingroup themeable
- */
-function theme_uc_stock_edit_form($variables) {
-  $form = $variables['form'];
-  drupal_add_js('misc/tableselect.js');
-
-  $header = array(
-    array('data' => '&nbsp;&nbsp;' . t('Active'), 'class' => array('select-all')),
-    array('data' => t('SKU')),
-    array('data' => t('Stock')),
-    array('data' => t('Threshold')),
-  );
-  $rows = array();
-
-  foreach (element_children($form['stock']) as $id) {
-    $rows[] = array(
-      array('data' => drupal_render($form['stock'][$id]['active'])),
-      array('data' => drupal_render($form['stock'][$id]['display_sku'])),
-      array('data' => drupal_render($form['stock'][$id]['stock'])),
-      array('data' => drupal_render($form['stock'][$id]['threshold'])),
-    );
-  }
-
-  return theme('table', array('header' => $header, 'rows' => $rows)) . drupal_render_children($form);
-}
diff --git a/uc_stock/uc_stock.module b/uc_stock/uc_stock.module
index b10fb2b..c975ee5 100644
--- a/uc_stock/uc_stock.module
+++ b/uc_stock/uc_stock.module
@@ -72,18 +72,6 @@ function uc_stock_permission() {
 }
 
 /**
- * Implements hook_theme().
- */
-function uc_stock_theme() {
-  return array(
-    'uc_stock_edit_form' => array(
-      'render element' => 'form',
-      'file' => 'uc_stock.admin.inc',
-    ),
-  );
-}
-
-/**
  * Implements hook_mail().
  */
 function uc_stock_mail($key, &$message, $params) {
