? plugins/.spaces_controller_boxes.inc.swp
Index: boxes.admin.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/boxes/boxes.admin.inc,v
retrieving revision 1.5.2.1
diff -u -r1.5.2.1 boxes.admin.inc
--- boxes.admin.inc	25 Apr 2010 18:41:54 -0000	1.5.2.1
+++ boxes.admin.inc	28 May 2010 19:48:26 -0000
@@ -38,8 +38,7 @@
  * Submit handler for box_add_form.
  */
 function boxes_add_form_submit($form, &$form_state) {
-  $box = boxes_create($form_state['values']['plugin_key']);
-  $box->set($form_state['values']);
+  $box = boxes_create($form_state['values']['plugin_key'], $form_state['values']);
   $box->save();
   drupal_set_message(t('%name has been created.', array('%name' => $box->description)));
   $form_state['redirect'] = 'admin/build/block';
@@ -156,9 +155,8 @@
  */
 function boxes_box_inline_form_submit($form, $form_state) {
   if (!$box = boxes_load($form_state['values']['delta'])) {
-    $box = boxes_create($form_state['values']['plugin_key']);
+    $box = boxes_create($form_state['values']['plugin_key'], $form_state['values']);
   }
-  $box->set($form_state['values']);
   if (module_exists('spaces') && $space = spaces_get_space()) {
     $space->controllers->boxes->set($box->delta, $box);
   }
Index: boxes.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/boxes/boxes.module,v
retrieving revision 1.10.2.4
diff -u -r1.10.2.4 boxes.module
--- boxes.module	25 May 2010 19:03:10 -0000	1.10.2.4
+++ boxes.module	28 May 2010 19:48:26 -0000
@@ -123,9 +123,8 @@
     case 'save':
       $edit['delta'] = $delta;
       if (!$box = boxes_load($delta)) {
-        $box = boxes_create($edit['plugin_key']);
+        $box = boxes_create($edit['plugin_key'], $edit);
       }
-      $box->set($edit);
       $box->save();
       break;
 
@@ -146,8 +145,7 @@
           $hash = dechex(crc32($user->sid . microtime()));
         }
         $delta = "{$identifier}-{$hash}";
-        $box = boxes_create($plugin_key);
-        $box->set(array('delta' => $delta));
+        $box = boxes_create($plugin_key, array('delta' => $delta));
       }
       else {
         $box = boxes_load($delta);
@@ -197,11 +195,18 @@
 }
 
 /**
- * Create a new box object.
+ * Instantiate box object.
+ *
+ * @param $plugin_key
+ *   The string id of the boxes plugin.
+ * @param $values
+ *   The values to set on the box.
+ *
+ * @return a box object.
  */
-function boxes_create($plugin_key) {
+function boxes_create($plugin_key, $values = array()) {
   module_load_include('inc', 'boxes', 'plugins/boxes_box');
-  return boxes_box::create($plugin_key);
+  return boxes_box::factory($plugin_key, $values);
 }
 
 /**
@@ -453,8 +458,7 @@
 function boxes_boxes_view_alter(&$box, $delta) {
   if (module_exists('spaces') && $space = spaces_get_space()) {
     if ($space_box = $space->controllers->boxes->get($delta)) {
-      $box = boxes_create($space_box['plugin_key']);
-      $box->set($space_box);
+      $box = boxes_create($space_box['plugin_key'], $space_box);
       $box->new = FALSE;
     }
   }
Index: plugins/boxes_box.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/boxes/plugins/boxes_box.inc,v
retrieving revision 1.1.2.1
diff -u -r1.1.2.1 boxes_box.inc
--- plugins/boxes_box.inc	14 May 2010 14:59:49 -0000	1.1.2.1
+++ plugins/boxes_box.inc	28 May 2010 19:48:26 -0000
@@ -8,7 +8,7 @@
  * @see boxes_simple.
  */
 abstract class boxes_box {
-  static $boxes;
+  static $boxes; // Static cache of box objects.
   public $delta;
   public $title;
   public $description;
@@ -18,44 +18,51 @@
   public $export_type;
  
   /**
-   * Load an existing box by its unique identifier $delta.
+   * Load existing box by its unique identifier $delta.
    */
   public static function load($delta) {
     if (!isset(self::$boxes[$delta])) {
       ctools_include('export');
       if ($values = array_pop(ctools_export_load_object('box', 'names', array($delta)))) {
-        // Attempt to create an instance of the box class.
-        // Use 'simple' as the plugin class to handle legacy boxes.
-        try {
-          $box = self::create(isset($values->plugin_key) ? $values->plugin_key : 'simple');
-          foreach ($box as $key => $value) {
-            if (isset($values->$key)) {
-              $box->$key = $values->$key;
-            }
-          }
-          $box->new = FALSE; // Set in __construct().
-          self::$boxes[$delta] = $box;
-        }
-        catch (Exception $exception) {
-          return FALSE;
-        }
+        self::$boxes[$delta] = self::factory($values->plugin_key, $values);
+        self::$boxes[$delta]->new = false;
       }
     }
     return isset(self::$boxes[$delta]) ? self::$boxes[$delta] : NULL;
   }
  
   /**
-   * Create a new box. Factory for creating a box of a particular type.
+   * Instantiate, populate and return a box object.
+   *
+   * @param $plugin_key
    *
    * @param $values
-   * An array with at least a plugin_key key identifying the plugin class to
-   * use for instantiating this box.
+   *   An array with at least a plugin_key key identifying the plugin class to
+   *   use for instantiating this box.
    */
-  public static function create($plugin_key) {
+  public static function factory($plugin_key, $values) {
     ctools_include('plugins');
     if ($class = ctools_plugin_load_class('boxes', 'plugins', $plugin_key, 'handler')) {
+
+      // While we actually prefer to get objects, we need to allow for either,
+      // so we convert it all to arrays.
+      if (is_object($values)) {
+        $values = (array)$values;
+      }
+
       $box = new $class();
       $box->plugin_key = $plugin_key;
+
+      foreach ($box as $key => $value) {
+        if (isset($values[$key])) {
+          $box->$key = $values[$key];
+        }
+      }
+      foreach ($box->options_defaults() as $key => $value) {
+        if (isset($values[$key])) {
+          $box->options[$key] = $values[$key];
+        }
+      }
       return $box;
     }
     else {
@@ -72,30 +79,6 @@
   }
  
   /**
-   * Set class variables.
-   *
-   * @param $values
-   * A keyed array where the keys are class variables or keys in
-   * $this->options declared by options_defaults().
-   */
-  public function set($values) {
-    foreach ($this as $key => $value) {
-      if (isset($values[$key])) {
-        $this->$key = $values[$key];
-      }
-    }
-    foreach ($this->options_defaults() as $key => $value) {
-      if (isset($values[$key])) {
-        $this->options[$key] = $values[$key];
-      }
-    }
-    // Make sure box is present in static cache as soon as delta is set.
-    if (!empty($this->delta)) {
-      self::$boxes[$this->delta] = $this;
-    }
-  }
- 
-  /**
    * Reset the boxes cache.
    */
   public static function reset() {
Index: plugins/spaces_controller_boxes.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/boxes/plugins/spaces_controller_boxes.inc,v
retrieving revision 1.2
diff -u -r1.2 spaces_controller_boxes.inc
--- plugins/spaces_controller_boxes.inc	30 Mar 2010 16:23:00 -0000	1.2
+++ plugins/spaces_controller_boxes.inc	28 May 2010 19:48:26 -0000
@@ -6,15 +6,19 @@
  */
 class spaces_controller_boxes extends spaces_controller {
 
+  // Override of load_original_values
   protected function load_original_values($id = NULL) {
     if (empty($this->loaded_all['original'])) {
+      // TODO can we always rely on haveing export.inc loaded?
       if (!isset($id)) {
-        $boxes = boxes_load();
+        foreach (ctools_export_load_object('box') as $box) {
+          $boxes[$box->delta] = (array)$box;
+        }
         $this->values['original'] = $boxes;
         $this->loaded_all['original'] = TRUE;
       }
       else if (!isset($this->loaded['original'][$id])) {
-        if ($box = boxes_load($id)) {
+        if ($box = array_pop(ctools_export_load_object('box', 'names', array($id)))) {
           $this->values['original'][$id] = $box;
           $this->loaded['original'][$id] = TRUE;
         }
