I'm not sure how to best go about implementing this. I'd think it's fine for beans of custom bean types created through Bean Admin UI to have generated machine names (they could still be simply bean id), but in general, beans are very useful to create in installation profiles and feature modules, and in these cases I want to be able to reliably place bean XYZ in a context or page manager panels page, so I want a machine name. Perhaps the bean plugin should define whether it wants / requires a machine name? Or perhaps machine names should be reserved for beans created through code (fine with me).

Sort of relatedly, here's a slideshow bean feature using field_collection and Views Slideshow: https://github.com/opensourcery/os_slideshow (7.x-2.x branch). This is one type of bean I want to place in a Panels page. :)

Comments

adamdicarlo’s picture

StatusFileSize
new3.2 KB

Well, I went ahead and wrote an initial patch. It doesn't expose any interface for the machine name (which I've added as 'delta', following the model of Boxes module), but for building features and install profiles, this works, as you can set the delta through code.

What this doesn't do, which I just thought of, is set a default value for delta when creating new beans. So I'll leave this issue as active for now, though I'd appreciate comments, especially what should be done to get this into shape to actually get committed.

indytechcook’s picture

I like the idea of machine names. We need to be careful though. I do not like the idea of exporting the beans themselves. The Bean types are already exportable. I'm going to add this patch which is pretty much the most basic implementation and add a check on the Bean::save() that makes the delta something. I don't think we even need to expose the delta to the UI since it's just for blocks added by code.

Perhaps adding support in the UUID Features module might work but that's out of the scope of what I want this module to do.

adamdicarlo’s picture

Yeah, I don't want to export beans themselves either -- I'm with you there. I've already been through the whole exporting Boxes (via features) thing where you realize it's better to just create them in a hook_install() so that you don't accidentally revert their contents after the client has customized them. IIRC you or another Treehouse dev presented on this at DC Chicago. :)

Let me know if you want me to help with finishing the patch.

indytechcook’s picture

Yeah, that was me alright. I haven't used features since :)

indytechcook’s picture

StatusFileSize
new4.67 KB

Check out this patch

indytechcook’s picture

Status: Active » Needs review
adamdicarlo’s picture

Status: Needs review » Needs work
+++ b/bean.moduleundefined
@@ -356,6 +356,30 @@ function bean_load($bid, $reset = FALSE) {
+ * @see @TODO bean_load_delta_multiple()

Do we need to actually add a bean_load_delta_multiple() function? Or should we remove this comment I had thrown in?

+++ b/bean.moduleundefined
@@ -356,6 +356,30 @@ function bean_load($bid, $reset = FALSE) {
+  $result = db_select('bean', 'b')

Should we drupal_static() cache a delta => bean mapping here? Hard to imagine this function being pounded with the same delta in one page load, but you never know.

+++ b/includes/bean.core.incundefined
@@ -164,7 +165,7 @@ class Bean extends Entity {
+      $values = is_array($this->data) ? $this->data : unserialize($this->data);

unrelated whitespace fix (not a huge deal)

+++ b/includes/bean.core.incundefined
@@ -239,6 +240,10 @@ class Bean extends Entity {
+    if (empty($this->delta)) {
+      $this->delta = preg_replace('/[^a-z0-9_]+/i', '-', $this->label);
+    }

Any reason not to just call drupal_clean_css_identifier()?

(Also, the Entity getter thing is interesting, I hadn't seen that before. I'll have to investigate how that works at some point.)

indytechcook’s picture

Do we need to actually add a bean_load_delta_multiple() function? Or should we remove this comment I had thrown in?

I'd say no.

Any reason not to just call drupal_clean_css_identifier()?

Just the fact that I didn't know it existed.

The entity getter thing is different. I"m not sure I totally like it but I know it's necessary. I do need to spend more time reviewing it also to make sure I used the correct ones.

adamdicarlo’s picture

The latest patch doesn't use the delta in hook_block_info() -- I'm adding that back in and rerolling now.

adamdicarlo’s picture

Status: Needs work » Needs review
StatusFileSize
new5.59 KB

New patch:

  • Uses delta for hook_block_info()
  • Uses strtolower() + drupal_clean_css_identifier() + substr() for default delta
  • Removes the @TODO comment I had added
  • Puts Issue # in both commit messages, fixes a typo in one
  • Keeps it as one commit by you, one commit by me
  • Notice the [PATCH 1/2] thing? I haven't done this before, but that was the result of git format-patch HEAD~2. Sweet!
adamdicarlo’s picture

You know, maybe that "strtolower() + drupal_clean_css_identifier() + substr()" stuff should be run even if the delta was already set -- the substr() especially since it makes sure the database query doesn't crash. What do you think?

indytechcook’s picture

Any non generated delta needs to match the delta in code if you are using context or other methods of block placement. If you are adding a Bean in code (which is manually/not allowed in features) then you are responsible for making sure it works. A big fat fatal error on db insert is easier to troubleshoot then "why the hell is my block not showing put where it's supposed to.

indytechcook’s picture

Status: Needs review » Fixed

I applied the patch but it didn't keep our separate commits so i just did the by thing for both of us. http://drupal.org/commitlog/commit/22232/cc553e6c25cacfeb5276344cdd66deb...

adamdicarlo’s picture

Oh... I'm a bit disappointed not to get credit in my d.o stats, but glad you applied it. Good point about non-generated deltas.

indytechcook’s picture

You do get your CTR score included with the "by" part.

adamdicarlo’s picture

StatusFileSize
new717 bytes

(Interesting. Some day I'll get out of the lowly CTR score of 3! :))

Unfortunately, we missed a bug in the patch that was committed: bean_block_view() didn't get changed to use bean_load_delta(). D'oh!

Follow-up patch attached.

indytechcook’s picture

Nice. So just applying the patch doesn't seem to give you credit. The author really doesn't seem to matter for some reason. I still have to set the --authoer in the commit. So i did this time.

Cheers,

http://drupal.org/commitlog/commit/22232/4e71d6323bd547440fc7d5883300e0b...

indytechcook’s picture

Give you please give this a final test today? I want to do another release before BADCamp this weekend (and my presentation on this module).

adamdicarlo’s picture

Cool. It's working fine for me.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.