Skip to main content

An Introduction to Bean Module

Bean stands for Block Entities Aren’t Nodes and provides a new method of displaying content on a Drupal site. Still require some clarification as to what this module provides? I sure did when I first looked at Bean so I will take you through the basics of implementing your own module that takes advantage of Beans. 

by nick.schuch /
Overview
Recently in a project it was a requirement for the client to have the ability to have an unlimited amount of blocks that could be placed around a site, enter Bean Module. Bean stands for Block Entities Aren’t Nodes and provides a new method of displaying content on a Drupal site. Still require some clarification as to what this module provides? I sure did when I first looked at Bean so I will take you through the basics of implementing your own module that takes advantage of Beans.
What is Bean?
Think of a Bean as a method to provide new types (compared to node this would be a content type) which then provides an add content interface to create as many blocks as you require (see screenshot below). The bean content can then be placed around the site just like any other block.
How do I Implement a Bean?
For this demonstration I will be taking snippets of code out of a Hello World module that I wrote for this blog post. This example can be found at the sandbox page.

1. First we need to define a new Bean type. In this case we are defining a Hello World type with the class HelloWorldBean. As seen the class is defined in the modules “plugins/bean/” directory under the file hello_world.hello_world.inc. 

/**
* Implements hook_bean_types_api_info().
*/
function bean_hello_world_bean_types_api_info() {
 return array('api' => 4);
}
 
/**
* Implements hook_bean_types().
*/
function bean_hello_world_bean_types() {
 $plugins = array();
 $plugin_path = drupal_get_path('module', 'hello_world') . '/plugins/bean';
 $plugins['article_listing'] = array(
   'label' => t('Hello World'),
   'description' => t('A demonstration of the Bean module.'),
   'handler' => array(
     'class' => 'HelloWorldBean',
     'parent' => 'bean',
   ),
   'path' => $plugin_path,
   'file' => 'bean_hello_world.hello_world.inc',
 );
 return $plugins;
}
Conclusion

Bean module is a new and flexible way to provide another method for a client to display data on a site. This module can provide something as simple as a Title/Body block or get as complex as a graphing bean of type “Line Chart” where the content editor could provide a set of data.


In my personal opinion I really enjoy the structure that Bean provides.

  • Declare the Bean
  • Set the values
  • Create a form
  • View the data.


This is a must for all Drupal developers to check out.


 

 

 

 

 

 

 

Where can I learn more?
Example module: http://drupal.org/project/bean_examples
Other module utilising Bean: http://drupal.org/node/1475632

 

Posted by nick.schuch
Systems Operations Developer

Dated

Comments

Comment by Tony

Dated

Why.... why would someone use this module? Why not just create a node content type, or a block? Or an entity? And how does a user place it in the site?

Comment by heather

Dated

@ Tony -

1- a block form only has a title and body. You'd also have to give a user permission to administer blocks to edit one block.

2- an item of content might be too much overhead. if it's something like the sidebar example shown in the article above... you'd have to have someone edit an item of content, create a view to display that 1 item as a block? Or you'd have to hard-code that node/ID into the template. Or you'd create a custom module to do something like the Bean module does.

You place it like a block :)

Comment by glamajamma

Dated

Still does not make much sense.
It would be easier to define a block using a content type, and then work logic on the content fields.
I don't know why you have this other layer of abstraction. Either way you still have to write code. Now you have written code another Drupal developer may not recognize, and is poorly documented. With blocks, you should recognize the code and it is heavily documented.

Comment by houssem

Dated

Thank you for this article :)

Comment by wanni

Dated

Thank you for this module, its excellently solves a problem for the scenario you described, and saves non coders a lot of headache and time

Pagination

Add new comment

Restricted HTML

  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <h2> <h3> <h4> <h5> <h6>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.