Multiblock: an example

Last modified: March 12, 2009 - 13:50

Here's an actual example of a Multiblock enabled block (with a bit left out for brevity):

function taxonomy_image_block($op = 'list', $delta = 0, $edit = array()) {
  global $user;
  drupal_add_css(drupal_get_path('module', 'taxonomy_image') .'/taxonomy_image.css');
  $multi_id = isset($edit['multiblock_delta']) ? '_'. $edit['multiblock_delta']['#value'] : NULL;

  switch ($op) {
    case 'list':
      $blocks[0] = array(
        'info' => t('Taxonomy Image: Node Images'),
        'cache' => BLOCK_CACHE_PER_PAGE,
        );
      return $blocks;

    case 'mb_enabled':
      return 'mb_enabled';

    case 'view':
      switch ($delta) {
        case 0:
          // Node Images.
          if (arg(0) != 'node' || !is_numeric(arg(1))) {
            return array();
          }
          $nid = (int)arg(1);
          $rows = array();
          $count = 0;
          $max = variable_get('taxonomy_image_block_max_images'. $multi_id, 3);
          $suppress = variable_get('taxonomy_image_block_suppress'. $multi_id, FALSE);
...
            $block = array(
              'subject' => strtr(variable_get('taxonomy_image_block_title', 'Term Images for "@title"'), $subs),
              'content' => theme('table', array() , $rows, array('id' => 'taxonomy_image_terms')),
              );
          }
          break;

      }
      return $block;

    case 'configure':
      switch ($delta) {
        case 0:
          $form['ti'] = array(
            '#type' => 'fieldset',
            '#title' => t('Taxonomy Image '),
            '#collapsible' => TRUE,
            );

          $form['ti']['taxonomy_image_block_max_images'] = array(
            '#type' => 'textfield',
            '#size' => 6,
            '#title' => t('Number of terms to show'),
            '#description' => t('This controls the number of terms that appear in the "Node Images" block. If you use the next option, suppressed terms do not count.'),
            '#default_value' => variable_get('taxonomy_image_block_max_images'. $multi_id, 3),
            );

          $form['ti']['taxonomy_image_block_suppress'] = array(
            '#type' => 'checkbox',
            '#title' => t('Suppress term if no image'),
            '#description' => t('Do not show the term if it has no image. Suppressed terms do not count towards the limit above.'),
            '#default_value' => variable_get('taxonomy_image_block_suppress'. $multi_id, FALSE),
            );
...
      return $form;

    case 'save':
      switch ($delta) {
        case 0:
          // Node Images.
          variable_set('taxonomy_image_block_max_images'. $multi_id, $edit['taxonomy_image_block_max_images']);
          variable_set('taxonomy_image_block_suppress'. $multi_id, $edit['taxonomy_image_block_suppress']);
...
          if (!$multi_id) {
            // Don't let blocks.module have the real title.
            db_query("UPDATE {blocks} SET title='' WHERE module='taxonomy_image' AND delta=0");
          }
          break;

      }
      return;
  } // end switch($op)
}

 
 

Drupal is a registered trademark of Dries Buytaert.