0

when

hook_block_info()
hook_block_configure()
hook_block_save()
hook_block_view()

are get called? is it possible to call these hooks manually in a module?

5
  • 1
    Hooks are functions, so you can call them from anywhere - whether they do what you want depends what you're trying to do? You can see where each function is invoked from on the documentation page on Drupal.org. Look for the x invocations of hook_hook() and expand it to show links to the relevant functions
    – Clive
    Jun 11, 2014 at 17:54
  • I'm trying to build a block on form submission which I still failed. what I'm trying to do is to insert the data of the block in a temporary table, call this hook function then build the block and in the end delete the data of the block in the temporary table.
    – M a m a D
    Jun 11, 2014 at 17:57
  • 1
    Why the temporary table if this is all being done in a single request? It's already in memory so that's a wasted trip to the database. Creating a block programatically is quite straight forward, is that the bit you're having trouble with? If so just update the question and add the code from your submit handler (with a bit of description) and I'm sure it'll get sorted
    – Clive
    Jun 11, 2014 at 18:00
  • Because the blocks are going to contain a sort of slider. site administrator is able to create as many slider (block) as he/she wants and the information about the slider is set in a form. clearly on the form submission that block must be generated, that's why I need to call the hook on the form submission. I already asked this question afew minutes ago (building block on form submission).
    – M a m a D
    Jun 11, 2014 at 18:04
  • 1
    bean module makes perfect case for your requirement. The site admin can create multiple blocks of a block-type(which will be slide type in your case), and those can then be grouped together (using a mini-panel maybe and given slider style). Go through the answer i gave here
    – Ranjan
    Jun 11, 2014 at 18:45

1 Answer 1

2

From the comments, I think you're confusing things a bit. There are two types of block:

Custom

the ones you create through the UI. The content for these is stored in the block_custom table.

Programatically defined

the ones that Drupal auto-discovers for modules that implement hook_block_info(). The content for these is defined in hook_block_view(), and is built on-the-fly, so to speak. The content can be cached, but when that cache is invalidated the implementation of hook_block_view() will be responsible for re-building it. Drupal doesn't store this content anywhere on your behalf.

You can use either type for your needs. If you want to use custom blocks there's a good approach here.

If you want to use the hooks, the approach should be like this (this assumes you have a data table called my_table that you've populated with the necessary content for the block):

function MYMODULE_block_info() {
  // Get identifiers for all your custom blocks.
  $deltas = db_query('SELECT delta FROM {my_table}')->fetchCol();
  $blocks = array();

  foreach ($deltas as $delta) {
    $blocks[$delta] = array( /* Block definition */ );
  }

  return $blocks;
}

function MYMODULE_block_view($delta = '') {
  $block = array();     

  if (!empty($delta)) {
    // Get your custom data for this delta
    $data = db_query('SELECT * FROM {my_table} WHERE delta = :delta', array(':delta' => $delta))->fetchObject();

    // And populate the block with it.
    $block['subject'] = $data->title;
    $block['content'] = $data->content;
  }

  return $block;
}

Obviously that's a trivial example and you'll want to add error checking and the like, but it should get you started. Note that you won't be able to have a temporary table as such with this method, your data will need to persist so it can be re-queried when the time comes.

1
  • This is my approach: The content and subject of the block is in database. these data (content and subject) are not provided at the time of module installation, they may be provided at any time during the website life time (on form submission). so will be any problem if I call this hook on form submission? and then read the data from database and build the block?
    – M a m a D
    Jun 12, 2014 at 8:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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