3

I have a rather newbie question, I am following a Drupal 7 module examples, specifically an ajax wizard form. All form creation / manipulation is included in ajax_example_graceful_degradation.inc which when is called in file attribute:

// A classic multi-step wizard, but with no page reloads.
  // See ajax_example_graceful_degradation.inc.
  $items['examples/ajax_example/wizard'] = array(
    'title' => 'Wizard (with graceful degradation)',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('ajax_example_wizard'),
    'access callback' => TRUE,
    **'file' => 'ajax_example_graceful_degradation.inc',**
    'weight' => 7,
  );

Next i would like to create a block which will be included in my template. It seems that i cant include a file attribute in block_view function. How can I do that? Note: If I include all fuctions of ajax_example_graceful_degradation.inc in .module file then my block works ok.

3 Answers 3

1

What @anil sagar said is correct. i think you are misunderstanding how hook_menu() functions. Remember, hook_menu() "enables modules to register paths in order to define how URL requests are handled." a block doesn't really register a path as it is part of the content of a node (content, article, blog, whatever). Hopefully that part makes some sense... let me know if it doesn't.

So you don't necessarily need the hook_menu() function for blocks (although exceptions can always been seen). Again, you'll need to write your code in your my_cool_block.module using the above stated hooks hook_block_info() and hook_block_view().

however, you could include a file that has other custom module specific code in there. i tend to write my code with drupal hooks always in the my_module.module file. any supporting code i write either in a class or static class

4
  • Hi, Now I think that my question is not clear at all. I not disagree with @anil sagar nor with you. I know how hook_menu() works and I don't want to register any path nor to have hook_menu() (I used it as an example). What I am asking is: I like to have my source code to be in separate files 1: .module- where my hooks are living and 2:ajax_example_graceful_degradation.inc - where I have my code for form generation etc. If you see in hook_menu() example above there is an attribute "file"=>blabla.inc, I cant find similar attribute in block_view() or block_info(). Hope now its clear, thanks
    – Jim
    Commented Oct 13, 2012 at 4:05
  • 1
    for a block, put your supporting source code in another file and simply include it in your .module. however, you should put hook_block_info() and hook_block_view in your .module file for drupal purposes. again, any supporting functions can be in another file. this is nothing new in php. as for the file key, this key does not exist for hook_block_info(). its only apart of hook_menu(). check the api again under return values for hook_block_info(). anything in bold is what key you can set for that assoc array.
    – au_stan
    Commented Oct 15, 2012 at 15:41
  • 1
    Ok, since I saw a file key in hook_menu() I thought it is available in hook_block_info() and/or hook_block_view, which seems to be more drupal way to include any file. Thanks, I will stick with plain php way
    – Jim
    Commented Oct 15, 2012 at 19:36
  • no problem. i know the key => value stuff for drupal can be a little hard to wrap your head around (at least for me it can be). maybe you could suggest this to the drupal community for enhancement?
    – au_stan
    Commented Oct 16, 2012 at 13:00
3

There is no such an attribute in block_view function, but you can always use something like that (assuming that you have already implemented hook_block_info() and you have defined your own MYBLOCKDELTA):

function MYMODULENAME_block_view($delta = '') {
  switch ($delta) {
    case 'MYBLOCKDELTA':
      $block['subject'] = t('This is my block');
      module_load_include('inc', 'MYMODULENAME', 'my_inc_file');
      $block['content'] = myblock_contents($delta);
      break;
  }
  return $block;
}

Then define function myblock_contents($delta) in your my_inc_file.inc in MYMODULENAME module folder.

1

Please find answers for your question.

  • You can create a block using hook_block_info() and hook_block_view($delta = '')
  • Hooks should be written in .module file not in any other files inside module.
  • So, create two functions in your.module file which will implement above two hooks to expose a custom block from module.
  • You need to enable the block you created using administer blocks page.
  • If you want to embed your block using code any where you can use below code !

    $block = module_invoke('module_name', 'block_view', 'block_delta'); print render($block);

Hope it clarifies you !

1
  • Hi, perhaps my question is not clear. I already did what you correctly suggested meaning my hooks are in .module file, but the function for the form creation is in another file (ajax_example_graceful_degradation.inc). If you see the above code there is an "file" attribute which includes the above file during menu creation. I hope to find similar attribute for the block, but cant find it.
    – Jim
    Commented Oct 12, 2012 at 8:20

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.