Return to Snippet

Revision: 62266
at February 12, 2013 20:33 by brownrl


Initial Code
<?php
/**
 * @file
 * This file defines the robmod module.
 */




/**
 * Implements hook_block_info().
 */
function robmod_block_info() {

  // Get the module name
  $module = basename(__FILE__, '.module');

  // Build a glob to get all the blocks defined.
  $module_path = drupal_get_path("module", $module);
  $block_path = $module_path . "/" . "blocks";
  $block_files = glob($block_path . "/" . "block.*.php");

  // The empty block info array.
  $r = array();

  foreach($block_files as $file) {
    if(preg_match("/block\.([a-z_]+)\.php/", $file, $matches)) {
      // Block name
      $block = $matches[1];
      // Name can be translated why not?
      $block_name = ucwords(str_replace("_", " ", $block));
      $r[$block]['info'] = t($block_name);
      // Assume no cache.
      $r[$block]['cache'] = DRUPAL_NO_CACHE;
    }
  }

  return $r;
}


/**
 * Implements hook_block_view().
 */
function robmod_block_view($delta = '') {

  // Get the module name
  $module = basename(__FILE__, '.module');

  // Setup a blank block.
  $block = array();
  $block['subject'] = t($delta);
  $block['content'] = t('No Content Defiend for: ') . $delta;

  // Build a path to the block file.
  $module_path = drupal_get_path("module", $module);
  $block_path = $module_path . "/" . "blocks";
  $block_file = $block_path . "/" . "block." . $delta . ".php";

  // Does the file exists?
  if(file_exists($block_file)) {
    // Yes it does so let's include it.
    include_once($block_file);
    // We should now have a function from that.
    $function = "block_".$delta."_content";
    // Does that function exist?
    if(function_exists($function)) {
      // Yes assign the return to the block content.
      $block['content'] = $function();
    }
  }

  return $block;
}

Initial URL
http://www.itsgotto.be/cv.php

Initial Description
This is a hook_block_info and hook_block_view template that will allow you to quickly create blocks in a Drupal module. When you use this all you have to do is create a directory "blocks" in the module and then create block.block_name.php in the directory. Inside the php you need to define a function block_block_name_content().

Initial Title
Drupal Auto Blocks

Initial Tags
drupal

Initial Language
PHP