I have the following query that fetches all the nodes of a certain type.

function shop_home_page() {
  $render = array();  
  $sql = 'SELECT nid FROM {node} n WHERE n.type = :type AND n.status = :status';
  $result = db_query($sql,
      ':type' => 'music_albums',
      ':status' => 1,

  foreach ($result as $row) {
    $node = node_load($row->nid);
    $render['my_list'][] = node_view($node, 'teaser');       
  return $render;
  1. How do I display in my page node elements such as $node-title, $node-body? I tried as $render['my_list'][] = node->title but it doesn't seem to work.
  2. I also need to add a form with a buy button to each element. How do I attach it?

2 Answers 2


1) You can use code like the following:

$render['my_list'][] = array('#markup' => '<div>'.$node->title.'</div>');
$render['my_list'][] = array('#markup' => $node->body['und'][0]['value']);

2) Forms can be added similarly to node_view():

$render['my_list'][] = drupal_get_form('mymodule_buy_form_'.$node->nid, $node->nid);

I'm assuming you want the forms built with a single function, but with different id-s to work correctly (attached $node->nid for that). I also added an extra argument ($node->nid) since you may want to add it to every form in a hidden field to determine which form got submitted. Then you use hook_forms() to get the same builder function called for all the buying forms:

function mymodule_forms($form_id, $args) {
  $forms = array();
  if (strpos($form_id, 'mymodule_buy_form_') === 0) {
    $forms[$form_id] = array(
      'callback' => 'mymodule_buy_form',
  return $forms;

The form builder function for this example:

function mymodule_buy_form($form, &$form_state, $nid) {
  $form = array();
  $form['nid'] = array(
    '#type' => 'hidden',
    '#value' => $nid,
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
  return $form;

And a submit handler:

function mymodule_buy_form_submit($form, &$form_state) {
  drupal_set_message('Form for node '.$form_state['values']['nid'].' submitted.');
  • I have build up the form builder but the problem if I am keeping a composite function name like this: drupal_get_form('mymodule_buy_form_'.$node->nid, $node->nid); I am constantly getting error, callback not found or invalid, while if I am using this name: drupal_get_form('mymodule_buy_form', $node->nid) it works but it adds these ids to the <form> : pix-product-buy-form, pix-product-buy-form--2 very strange. Can you help me up further? Thanks Jan 21, 2012 at 22:38
  • You're probably going wrong somewhere. I added further examples into my answer that should get you a working solution. Add the missing code into your module file, replace all 'mymodule' occurrences with your actual module name, clear caches and then test it out.
    – Madis
    Jan 22, 2012 at 14:03
  • Thanks a lot. You are great! I added also the product picture: $render['my_shop_list'][] = array('#markup' => "<img src=".$base_url."/sites/default/files/".$node->field_cover['und'][0]['filename'].">"); I think also rather that retrieving the product list via sql to do it using a view + views_get_view_result() function. Jan 25, 2012 at 11:39

This looks like it could be done with a view, using view type node list. Display type should be fields and then you select node title and teaser as fields as well as an additional field which has to be rewritten to build a buy button (depending on what action the button shall trigger).

  • That is a very good idea,but despite the fact I installed webform and created a webform which has a submit...there is no way I can make a submit button display in views Jan 21, 2012 at 18:15

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.