Hi,
my custom module generates a block that contains a form. If I keep this form function in the custom_module.module file, it is invoked. But it is not when I move it to custom_module.inc file.

function custom_module_block($op = 'list', $delta = 0, $edit = array()) { 
  if ($op == "list") {
    // Generate listing of blocks from this module, for the admin/block page
    $block = array();
    $block[0]["info"] = t('Custom Block');
    return $block;
  }
  else if ($op == 'view') {   
	// set up the block
	$block['subject'] = t('Domestic Flights');
	$block['content'] = drupal_get_form('domestic_ticket_frontend_domestic_form');
	return $block;
  }
} // function custom_module_block

$block['content'] = drupal_get_form('domestic_ticket_frontend_domestic_form');

How can I call this form from custom_module.inc file?

Thanks

Comments

Raf’s picture

<?php require_once('custom_module.inc'); ?>
The server won't know it has to use that file if you don't tell it to ;)

I am learning’s picture

ya, thatz true Raf and thanks for the reply.
Is there a way we do it for registering paths and associate functions:

//Register Edit
  $items['admin/content/custom_module/edit'] = array(
        'title' => 'Edit',
        'description' => 'Edit Details',
        'page callback' => 'drupal_get_form',
		'page arguments' => array('custom_edit'),
		'access arguments' => array('Edit Custom Values'),
		'file' => 'custom_module.inc',
        'type' => MENU_CALLBACK,
    );

I don't know if I'm making sense here :)
Regards

Raf’s picture

That's right on the spot, that! If you add that to your site, just refresh caches and it'll work.

I am learning’s picture

my confusion is about invoking the registered path, how will I do that in my hook _block?

$block['content'] = drupal_get_form('domestic_ticket_frontend_domestic_form');

Raf’s picture

You mean you want to know the path that's been filled in?

$_GET['q']

Do make sure to take the q= out from it (paths are always q=[the path])

I am learning’s picture

no, I want to know how to invoke it?
$block['content'] = drupal_get_form('domestic_ticket_frontend_domestic_form');

please check the above statement that invokes a form.

Thanks

Raf’s picture

Oh, if I understand correctly, you want to know how to place your block on the page you set in hook_menu?

I am learning’s picture

no :)
when we create a block through drupal API then the hook used is _block. So when

else if ($op == 'view') {  
// set up the block
$block['subject'] = t('Domestic Flights');
$block['content'] = drupal_get_form('domestic_ticket_frontend_domestic_form');
return $block;
  }

and in the above piece of code the line:

$block['content'] = drupal_get_form('domestic_ticket_frontend_domestic_form');

is creating a form for block content, although the content can be anything but in my case I want a form.
so here I'm calling a function 'domestic_ticket_frontend_domestic_form' using drupal_get_form().

My problem is that if I define

function domestic_ticket_frontend_form(&$form_state, $my_values = array()) {

in .module file, it is invoked, but if I place it in .inc, it is not.

Thanks

Raf’s picture

Alright, I get it now. You're asking whether or not hook_block has a similar way of including files as hook_menu has, right? Well, the quick and simple answer is: nope.

$block takes several values when $op is 'list':

  • info
  • cache
  • weight
  • status
  • region
  • visibility
  • pages

None of them allow including files.

The other option for altering $block, is when $op is 'view', but there's no option for files there, either. That one's just these two:

  • subject
  • content

So the only way to include the file in hook_block, is by using require() / require_once() / include() / include_once() when $op is 'view'

I am learning’s picture

Ah! okay. then I'll include the .inc file.
Thanks Raf, a great help. I've taken so much time of yours. Thanks again for explaining with so much patience.

Regards