You are here

Beans - Create, Preprocess, and Theme.

This is a simple example of code (template) snippets to work with Beans, including the creation, preprocessors, and theming.

For this example I will be creating the Bean through the GUI and locking it in a Feature. This is good for high-paced development, and at least for what I have used Beans for, I haven't had the need to create one programmatically, yet.

Beans turns blocks into an entity, Block Types; which then you can create instances of each block type, have fields, view modes, etc. Just like you would with a content type.

Important! Read all notes and comments.

Install & Create

First enable Bean along with all its dependencies, e.g. Entity, cTools.

$ drush en bean -y

Create a Bean Type. Go to admin/structure/block-types/add, give it a "Label" and "Description", click on "Save Block Type".

Under the "Manage Fields" tab, you can add fields just like you would in a Content Type. For the simplicity of this example, let's skip it.

Preprocessor

Second, let's add specific preprocess functions to Bean entities since it does not do that out of the box. This helps keep things clean.

/**
* Implements hook_preprocess_entity().
*/
function example_preprocess_entity(&$variables) {
  if($variables['entity_type'] == 'bean') {
    switch($variables['bean']->type) {
      // get the machine name from page where the bean was created.
      case '{bean_machine_name}':
        $function_name = "example_preprocess_bean_{$variables['bean']->type}";
        if(function_exists($function_name)) {
          call_user_func($function_name, &$variables);
        }
    }
  }
}

Now, let's add the specific preprocess for our Bean Type.

/**
* Implements hook_preprocess_bean_BEAN_TYPE().
*
* Note: replace {bean_machine_name} with the Bean's machine name.
*/
function example_preprocess_bean_{bean_machine_name}(&$variables) {
  // pass additional values, or perform any modifications to an existing value.
  // e.g. pass the variable $hello
  $variables['hello'] = 'world!';
}

Theming

Lastly, the following will allow us to use our own Bean template.

/**
* Implements hook_theme().
*/
function example_theme() {
  // override the default bean template from this module.
  $path = drupal_get_path('module', 'example') . '/templates/';
  $beans = _example_get_bean_entity_types($path);
  foreach($beans as $bean) {
    $bean_theme = array(
      "bean__{$bean}_tile" => array(
        'render element' => 'page',
        'template' => "templates/bean--{$bean}-tile"
      ),
    );
  }
  return $bean_theme;
}

This helper function saves me the headache from keep on adding theme definitions to the hook_theme() function.

/**
* Finds all Bean integrated in this module by simply scanning the template
* directory for Bean templates.
*
* @param string $path
*    The path to the directory where the bean templates are stored.
*/
function _example_get_bean_entity_types($path) {
  $files = array_diff(scandir($path), array('.', '..'));
  $replace = array('bean--', '-tile.tpl.php');
  foreach($files as $key => $bean_name) {
    $files[$key] = str_replace($replace, '', $bean_name);
  }
  return $files;
}

Feature

Once you ready to have this tested on your Dev environment, lock the Bean Type in a Feature. Not gonna get onto how to create this, but you can read on here https://drupal.org/project/features

Enjoy.

lifestyle:

medium: