Hello,

First, apologize if my question is stupid. I am new to Drupal, only a couple fo weeks.

I am developing a new block/module and would like to use drupal_set_content() function to display my query result, all this is in the _submit() function of my form.

until now, I was using drupal_set_message($content), but I want to display $content in the block content itself.

Any help is appreciated.

Thanks,

Comments

pwolanin’s picture

Why are you using this function? That's not the normal way to add content to a block.

Instead, you should define hook_block in your module, run the query, and return the output.
See, for example, comment_block or forum_block:

http://api.drupal.org/api/5/function/comment_block

http://api.drupal.org/api/5/function/forum_block

---
Work: BioRAFT

wydadi2003’s picture

First Thanks for your response. I use the hook_block

-My SQL query is in the form_submit()
-The result is placed in global $variable.
- In the modulename_block, I call drupal_get_form() to have my form built and submitted,
- I put the result of drupal_get_form() into block['content']

I tried to use a block['content']=drupal_get_form().$variable to get the result displayed after the form , but it doesn't seem to work.

That's I was thinking of another way:

when using drupal_set_message($variable) , is fine, but not what I want.

when using drupal_set_content('content'/'left'...,$variable), I have nothing.

thanks,

pwolanin’s picture

So, if I understand you want something like a preview? The user "submits" the form and then the output is displayed in the block below the form?

If yes, here's how to do it:

1) define your "submit" button as type "button" NOT type "submit"

2) assign an "#after_build" function to your form

3) in the #after_build function you assign the output to the #suffix element of the form.

Thus, you would just have the block content as the usual drupal_get_form();

Below an example from one of my modules (in a page, not a block, but the concept is the same). This is similar to how the node or comment preview works. You might also want to look at that core code.

function datasearch_user_find() {
  global $user;
  
  $form['help'] = array(
    '#value' => '<p>'. _datasearch_message() .'</p>',
  );

  $form['data'] = array(
    '#type' => 'textfield',
    '#title' => t('Zip code'),
    '#required' => TRUE,
    '#size' => 10,
  );
  
  $form['#after_build'] = array('datasearch_add_preview'); 
  
  $form['preview'] = array('#type' => 'button', '#value' => t('Submit'));

  return drupal_get_form('datasearch_user_find', $form);
}

function datasearch_user_find_validate($form_id, $form_values) {
  
  if (!preg_match('/^[0-9]{5}$/', trim($form_values['data']))) {
    form_set_error('data', t('Invaid zip code.'));
  }
}

function datasearch_add_preview($form) {
  global $form_values;

  $op = isset($_POST['op']) ? $_POST['op'] : '';
  if ($op == t('Submit')) { 
    drupal_validate_form($form['form_id']['#value'], $form);
    if (!form_get_errors()) {
      $rows = datasearch_find_data($form_values['data']);
      $preview = theme('datasearch_table', $rows);
      $form['#suffix'] = isset($form['#suffix']) ? $preview . $form['#suffix'] : $preview;
    }
  }
  return $form;
}

---
Work: BioRAFT

wydadi2003’s picture

My form is a search form, and the result will be displayed below the form after the submit button is clicked.

I change the function form_submit to func1,
I change the type to button (not submit)
I have now $form['#after_build'] = array ('func1') (just 'func1 gives an error')
I assign the result of the query to $form['suffix']

But still not getting the result, worst now even the form is not displayed :(

pwolanin’s picture

Oops- the code above is for 4.7- you should be returning $form for 5.x. Something like the below- see if you can get this code working.

function datasearch_block($op = 'list', $delta = 0) {
  if ($op == 'list') {
    $blocks[0]['info'] = t('Data search');
    return $blocks;
  }
  else if ($op == 'view' && user_access('search data')) {
    $block['subject'] = t('Data search');
    $block['content'] = drupal_get_form('datasearch_user_find');
    return $block;
  }
}

function datasearch_user_find() {
  global $user;
 
  $form['help'] = array(
    '#value' => '<p> Enter a zip code to find a user </p>',
  );

  $form['data'] = array(
    '#type' => 'textfield',
    '#title' => t('Zip code'),
    '#required' => TRUE,
    '#size' => 10,
  );
 
  $form['#after_build'] = array('datasearch_add_preview');
 
  $form['preview'] = array('#type' => 'button', '#value' => t('Submit'));

  return $form;
}

function datasearch_user_find_validate($form_id, $form_values) {
 
  if (!preg_match('/^[0-9]{5}$/', trim($form_values['data']))) {
    form_set_error('data', t('Invalid zip code.'));
  }
}

function datasearch_add_preview($form) {
  global $form_values;

  $op = isset($_POST['op']) ? $_POST['op'] : '';
  if ($op == t('Submit')) {
    drupal_validate_form($form['form_id']['#value'], $form);
    if (!form_get_errors()) {
      $rows = datasearch_find_data($form_values['data']); // Do your query and return the result here
      $preview = theme('table', array(), $rows);
      $form['#suffix'] = isset($form['#suffix']) ? $preview . $form['#suffix'] : $preview;
    }
  }
  return $form;
}

---
Work: BioRAFT

nancydru’s picture

function pinkslip_block($op = 'list', $delta = 0, $edit = array()) {
  switch ($op) {
    case 'list':
      $blocks[0]['info'] => t('Pink Slip Categories'),
       );
      $blocks[1]['info'] = t('Recent Pink Slip articles'),
       );
      return $blocks;

    case 'view':
      switch ($delta) {
        case 0:
	  // pinkslip Categories
        $show_unused = variable_get('pinkslip_show_unused_category',FALSE);
        if (!variable_get('pinkslip_use_categories', FALSE)) {
          $block['content'] = "Pink Slip categories disabled."; 
          break;          
         }
	  if (module_exists("taxonomy")) { /* show only if categories turned on */
	    $terms = array();
	    foreach (taxonomy_get_vocabularies('pinkslip') as $vocab) {
	      foreach (taxonomy_get_tree($vocab->vid) as $term) {
              $count = db_result(db_query("SELECT COUNT(nid) FROM {term_node} WHERE tid = %d ", $term->tid));
              // check if term has content in it or we show unused 
              if ($count || $show_unused) { $terms[$term->name] = $term->tid; }
	      } /* end term */
	    } /* end vocab */
	    ksort($terms);
	    if (sizeof($terms) > 0) {
	      $block['subject'] = t('Pink Slip Categories');
	      $block['content'] = "<ul>\n";
	      foreach ($terms as $name => $tid) {
              $block['content'] .= '<li>'. l($name, 'pinkslip/term/'. $tid) ."</li>\n";
	      }
	      $block['content'] .= "</ul>\n";
	    }
          else { $block['content'] = "No Pink Slip categories found."; }
	  }
          break;

        case 1:
	  // Recent pinkslips
          $block['subject'] = t('Recent Pink Slip articles');
          $block['content'] = pinkslip_recent(variable_get('pinkslip_block_recent_count', 5));
          break;
      }
      return $block;

    case 'configure':
      if ($delta == 1) {
	// Recent Pink Slips
	$form['pinkslip_block_recent_count'] = array(
	  '#type' => 'textfield',
	  '#title' => t('Number of Pink Slips to show'),
	  '#description' => t("This controls the number of Pink Slip nodes that appear in the 'Recent pinkslips' block"),
	  '#default_value' => variable_get('pinkslip_block_recent_count', 5),
	);
      }
      return $form;

    case 'save':
      if ($delta == 1) {
	variable_set('pinkslip_block_recent_count', $edit['pinkslip_block_recent_count']);
      }
      return;
  }
}

/**
*  pick out the most recent posts
*/
function pinkslip_recent($num = 5) {

  $result = db_query("SELECT title, nid FROM {node} WHERE type='pinkslip' AND status = 1 ORDER BY created DESC LIMIT %d", $num);

  $items = array();
  while ($node = db_fetch_object($result)) {
    $items[] = l($node->title, 'node/'.$node->nid);
  }

  return theme('item_list', $items) . l(t('All pinkslips'), 'pinkslip');
}

I took a few more blocks out of this just to give an example, but this should be fully functional.

BTW, for your CSS, the blocks are named "pinkslip-block-0" and "pinkslip-block-1" by the block numbers in the "case 'list'."

Nancy W.
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in your database