0

I'm trying to create a custom block programmatically in a Drupal 7 project. I've implemented the two hooks hook_block_info() and hook_block_view($delta=''), I can't find the created block on my_site/admin/structure/block admin page. Here is my code:

<?php

/**
 * @file
 * File for Header module block
 */

/**
 * Implements hook_block_info().
 */
function my_header_block_info() {
  return array(
    "headers" => array(
      "info" => "Header Block",
      "cache" => DRUPAL_NO_CACHE,
    ),
  );
}

/**
 * Implements hook_block_view().
 */
function my_header_block_view($delta = '') {
  $block = array();
  switch ($delta) {
    case "headers" :
      $block['subject'] = 'HEADER';
      $block['content'] = 'Hello world';
      break;
  }

  return $block;
}

What did I do wrong?

UPDATE It's a basic project should I install a necessary contrib module for make it working?

  • Is your module called my_header? Did you clear the cache? And finally, $block['content'] should be a render array. – Jaypan Aug 25 '18 at 13:27
  • yes the module name is "my_header", I've cleared the cache and I will try to use "#markup" => 'Hello world' as content I will test it now. thanks – MiharbKH Aug 25 '18 at 13:29
  • 1
    $block['content'] = array( '#markup' => 'Hello world'); it doesn't work either! – MiharbKH Aug 25 '18 at 13:33
  • Sorry, your code looks fine, I don't see anything wrong with it. – Jaypan Aug 25 '18 at 13:35
  • 1
    Have you enabled the module? – Clive Aug 25 '18 at 13:44
0

Both of the below formats should work,

function my_header_block_info() {
  return array(
    "headers" = array( //I have Replaced => with = on your code
      "info" => "Header Block",
      "cache" => DRUPAL_NO_CACHE,
    ),
  );
}

-----OR-----

function my_header_block_info() {
    $blocks = array();
    $blocks['headers'] = array(
        'info' => 'Header Block',
        'cache' => DRUPAL_NO_CACHE
    );
    return $blocks;
}

Make sure to clear the cache after updated your code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.