I am developing a module which generates custom blocks based on some configuration done by site admins in a module-specific config page. For each block I would like the admins to be able to nominate a webform (from a select dropdown), which would then be displayed in the block.

What I don't understand is how to match up the webform selection process with the webform display process. In the module-specific config page, I would generate the options by querying the node table for all nodes of type 'webform', display the titles of the webforms in the select element, and then save the nid of the chosen webform.

However this nid doesn't seem to be much help to me when it comes to my_module_block_view, where (as I understand it) in order to display a webform I need to do something like

$block['content'] = module_invoke('webform','block_view','client-block-N');

where N is the nid of the node to which the webform is attached, NOT the nid of the webform itself ? Incorrect statement - see correction below

So I'm unsure how to request the webform that the users have selected, as all I know about it is its nid?

What I'm seeing:

On my test Drupal, I have a webform with nid = 1, and content items of nid 2 and 3. Whether I specify 'client-block-1','client-block-1', or 'client-block-3' in my call to module_invoke, I am seeing no webform displayed.

Does anyone have any suggestions how I should proceed with this?

All help greatly appreciated.


In the code example above, N should be the node id of the webform.


You're very close - the return from hook_block_view() is an array with keys content and optionally subject. Your current code example:

$block['content'] = module_invoke('webform','block_view','client-block-N');

Produces the equivalent of:

$block['content'] = array(
  'subject' => ...,
  'content' => ...,

When you put that through drupal_render(), it gets angry (because subject isn't a render array but isn't prefixed by #).

You're probably looking for:

$block = module_invoke('webform','block_view','client-block-N');

Or simply:

return module_invoke('webform','block_view','client-block-N');

(once N is substituted for the node ID of the webform).

  • Thanks once again Clive for a great explanation. This will teach to me read the API docs a bit more in future - had I read api.drupal.org/api/drupal/modules!block!block.api.php/function/… then the fog might have cleared for me a bit earlier. Sep 18 '15 at 9:14
  • I was also confused by misinterpreting some tutorials I read (which I can't find now) and incorrectly inferring that the N in client-block-N is the nid of the node the webform is attached to, not the nid of the webform itself. The text at admin/content/webform ("This page lists all of the content on the site that may have a webform attached to it.") also added to my confusion - is this a list of webforms or content-with-webforms? Sep 18 '15 at 9:27

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.