0
votes

I am new at Drupal 7 and I'm creating a Block by code, following this tutorial.

So I create a new module folder at drupal/sites/all/modules and created two files:

block_square_menu.info: it has the info of the module:

name = Block Square Menu
description = Module that create a Block for Square menu, menu shown only in home page
core = 7.x
package = custom

block_square_menu.module: it contains the PHP code:

<?php

/**
 * Implements hook_block_info().
 */
function block_square_block_info() {
    $blocks = array();
    $blocks['block_square'] = array(
        'info' => t('Block Square'),
        'cache' => DRUPAL_CACHE_PER_ROLE,
    );

    return $blocks;
}

/**
 * Implements hook_block_view().
 */
function block_square_block_view($delta = '') {
    $block = array();
    switch ($delta) {
        case 'block_square':
            $block['subject'] = t('block Title');
            $block['content'] = t('Hello World!');
            break;
    }
    return $block;
}

After save the files, I go to Admin/Modules, I activate the new module and save the configuration. Now I go to Structure/Blocks and it should list my new Block, but it doesn't do.

I have followed all the tutorial steps and I cleaned Drupal cache, but I'm still having the problem.

2

2 Answers

1
votes

First solve your mistake: change the function name where you implemented hook_block_view(), you need to change it as function blocks_square_block_view()

/** * Implements hook_block_view(). */

function blocks_square_block_view($delta = '') {
    $block = array();
     ...... 

After also if not solve then remove 'cache' attribute from hook_block_info() it is optional.

Then follow 2 steps if you missed.

1) Clear all cache (/admin/config/development/performance).

2) Enable your custom module (/admin/modules).

After trying again, your block should appear in (/admin/structure/block).

0
votes

Solved, the problem was the name of the functions. So the names started with "block_square" which it have the word "block" and it causes some trouble so I changed the all the names with menu_square.

So the functions are now:

  • menu_square_block_info()
  • menu_square_block_view($delta = '')

And the files are:

  • menu_square.info
  • menu_square.module

The code of the files are:

info:

name = Menu Square
description = Module that create a Block for Square menu, menu shown only in home page
core = 7.x
package = custom

module:

<?php

/**
 * Implements hook_block_info().
 */
function menu_square_block_info() {
    $blocks['menu_square'] = array(
        'info' => t('Block Square'),
        //'cache' => DRUPAL_CACHE_PER_ROLE,
    );

    return $blocks;
}

/**
 * Implements hook_block_view().
 */
function menu_square_block_view($delta = '') {
    $block = array();
    switch ($delta) {
        case 'menu_square':
            $block['subject'] = t('block Title');
            $block['content'] = t('Hello World!');
            break;
    }
    return $block;
}