From bbc200158bd253a68ff1e1591d0c75762f9de802 Mon Sep 17 00:00:00 2001
From: adamdicarlo <adamdicarlo@100783.no-reply.drupal.org>
Date: Mon, 17 Oct 2011 17:07:40 -0400
Subject: [PATCH 1/2] Issue #1309126: Add delta row on table.

---
 bean.install |   27 +++++++++++++++++++++++++++
 bean.module  |   26 +++++++++++++++++++++++++-
 2 files changed, 52 insertions(+), 1 deletions(-)

diff --git a/bean.install b/bean.install
index 00246a8..8e61ad4 100644
--- a/bean.install
+++ b/bean.install
@@ -17,6 +17,12 @@ function bean_schema() {
         'not null' => TRUE,
         'description' => 'Primary Key: Unique bean item ID.',
       ),
+      'delta' => array(
+        'description' => "The bean's {block}.delta.",
+        'type' => 'varchar',
+        'length' => 32,
+        'not null' => TRUE,
+      ),
       'label' => array(
         'description' => 'The Displays in the Admin page.',
         'type' => 'varchar',
@@ -53,6 +59,9 @@ function bean_schema() {
       ),
     ),
     'primary key' => array('bid'),
+    'unique keys' => array(
+      'delta' => array('delta'),
+    ),
   );
 
   return $schema;
@@ -64,4 +73,22 @@ function bean_schema() {
 function bean_update_7001() {
   registry_rebuild();
   return t('Registry has been rebuilt');
+}
+
+/**
+ * Add delta field.
+ */
+function bean_update_7002() {
+  $spec = array(
+    'description' => "The bean's {block}.delta.",
+    'type' => 'varchar',
+    'initial' => '',
+    'length' => 32,
+    'not null' => TRUE,
+  );
+  db_add_field('bean', 'delta', $spec);
+  db_update('bean')
+    ->expression('delta', 'bid')
+    ->execute();
+  return t('Bean delta field added.');
 }
\ No newline at end of file
diff --git a/bean.module b/bean.module
index 2ee3ddb..aae3be6 100644
--- a/bean.module
+++ b/bean.module
@@ -356,6 +356,30 @@ function bean_load($bid, $reset = FALSE) {
 }
 
 /**
+ * Fetch a bean object by its delta.
+ *
+ * @param $delta
+ *   String specifying the bean delta.
+ * @param $reset
+ *   A boolean indicating that the internal cache should be reset.
+ * @return
+ *   A fully-loaded $bean object or FALSE if it cannot be loaded.
+ *
+ * @see @TODO bean_load_delta_multiple()
+ */
+function bean_load_delta($delta, $reset = FALSE) {
+  $result = db_select('bean', 'b')
+    ->fields('b', array('bid'))
+    ->condition('delta', $delta)
+    ->execute();
+  if ($bid = $result->fetchField()) {
+    $beans = bean_load_multiple(array($bid), array(), $reset);
+    return reset($beans);
+  }
+  return FALSE;
+}
+
+/**
  * Load multiple beans based on certain conditions.
  *
  * @param $bids
@@ -570,7 +594,7 @@ function bean_block_info() {
   $blocks = array();
   $beans = bean_get_all_beans();
   foreach ($beans as $bean) {
-    $blocks[$bean->bid] = array(
+    $blocks[$bean->delta] = array(
       'info' => $bean->label,
       'cache' => $bean->getInfo('cache_level'),
     );
-- 
1.7.4.1

From ce45560cd539b689a9b7066cc902600bf2df4619 Mon Sep 17 00:00:00 2001
From: Neil Hastings <nhastings@treehouseagency.com>
Date: Mon, 17 Oct 2011 17:10:49 -0400
Subject: [PATCH 2/2] Issue #1309126: Add delta to meta property and give it a default value.

---
 bean.module            |    2 --
 includes/bean.core.inc |    9 ++++++++-
 includes/bean.info.inc |    7 +++++++
 3 files changed, 15 insertions(+), 3 deletions(-)

diff --git a/bean.module b/bean.module
index aae3be6..429c3ca 100644
--- a/bean.module
+++ b/bean.module
@@ -364,8 +364,6 @@ function bean_load($bid, $reset = FALSE) {
  *   A boolean indicating that the internal cache should be reset.
  * @return
  *   A fully-loaded $bean object or FALSE if it cannot be loaded.
- *
- * @see @TODO bean_load_delta_multiple()
  */
 function bean_load_delta($delta, $reset = FALSE) {
   $result = db_select('bean', 'b')
diff --git a/includes/bean.core.inc b/includes/bean.core.inc
index 015f31b..50ab88a 100644
--- a/includes/bean.core.inc
+++ b/includes/bean.core.inc
@@ -114,6 +114,7 @@ class Bean extends Entity {
   public $title;
   public $type;
   public $data;
+  public $delta;
   protected $plugin;
 
   /**
@@ -164,7 +165,7 @@ class Bean extends Entity {
   public function setFields() {
     // NOTE: When setFields is caled externally $this->data is already unserializd.
     if (!empty($this->plugin)) {
-      $values = is_array($this->data)? $this->data : unserialize($this->data);
+      $values = is_array($this->data) ? $this->data : unserialize($this->data);
       foreach ($this->plugin->values() as $field => $default) {
         $this->$field = isset($values[$field]) ? $values[$field] : $default;
       }
@@ -239,6 +240,12 @@ class Bean extends Entity {
    * Override the save to add clearing of caches
    */
   public function save() {
+    // Set the delta if it's not set already
+    if (empty($this->delta)) {
+      // Base it on the label and make sure it isn't too long for the database
+      $this->delta = drupal_clean_css_identifier(strtolower($this->label));
+      $this->delta = substr($this->delta, 0, 32);
+    }
     $return = parent::save();
     block_flush_caches();
     return $return;
diff --git a/includes/bean.info.inc b/includes/bean.info.inc
index 682bec9..448d783 100644
--- a/includes/bean.info.inc
+++ b/includes/bean.info.inc
@@ -28,6 +28,13 @@ class BeanMetadataController extends EntityDefaultMetadataController {
       'schema field' => 'title',
     );
 
+    $properties['delta'] = array(
+      'getter callback' => 'entity_property_getter_method',
+      'required' => TRUE,
+      'schema filed' => 'delta',
+      'description' => t('The bean delta.'),
+    );
+
     $properties['type'] = array(
       'type' => 'bean_type',
       'getter callback' => 'entity_property_getter_method',
-- 
1.7.4.1

