Search code examples
phpdrupal-7

Drupal: custom block doesn't appear


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.


Solution

  • 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;
    }