0
votes

how to display any message or data in mymodule.module file in drupal 7

i have used following line but it didn't display any thing

drupal_set_message(t('test message'));

aslo i want to display any variable data like for example $data = "hello"

then how to display this variable data in drupal 7

i am new to drupal , so if any one knows please let me know .

i have search a lot , but didn't get anything.

thanks in advance.


I have used folllowing code by creating module in drupal 7

 <?php

  function form_example_menu() {
  $items = array();


   $items['form_example/form'] = array( 
        'title' => 'Example Form', //page title
        'description' => 'A form to mess around with.',
        'page callback' => 'drupal_get_form',
        'page arguments' => array('form_example_form'),
        'access arguments' => array('access content'), //put the name of the form here
        'access callback' => TRUE
     );

    return $items;
  }


   function form_example_form($form, &$form_state) {

    $form['price'] = array(
         '#type' => 'textfield', 
         '#title' => 'What is Your Price?',
         '#size' => 10,
         '#maxlength' => 10,
         '#required' => TRUE, //make this field required
         );

        $form['submit'] = array(
         '#type' => 'submit',
         '#value' => t('Click Here!'),
       );


    $form['form_example_form']['#submit'][] = 'form_example_form_submit'; 
    return $form;

    }



  function form_example_form_validate(&$form, &$form_state) {

 if (!($form_state['values']['price'] > 0)){
    form_set_error('price', t('Price must be a positive number.'));
  }
 }

  function form_example_form_submit($form, &$form_state) {

       $result = db_insert('test')->fields(array('price' => $form_state['values']['price'],))->execute();
      drupal_set_message(t('Your Price was saved')); 

  }

In above code data is inserted in database , but message didn't displaying . If you know , what is problem please let me know , i have search a lot for this problem . Thanks in advance.

2
Where exactly do you use this line drupal_set_message(t('test message')); ?!Muhammad Reda
To print debugging information, I suggest printing via dpm('Your price was saved') with the devel module instead. It will also print out variables in a nice format for you if you did dpm($data)nmc

2 Answers

3
votes

Here's the proper way to display some data in the message:

drupal_set_message(t('test message: !data', array('!data' => $data)));

As for the message not displaying, if other messages do display on your site, it sounds like your function isn't executing. I'd need more info on what you're attempting to do (including the code involved) to debug it.

0
votes

The function watchdog is also available in Drupal 7

Here is an exemple of how you can use it:

watchdog('MyModule', '<pre>'. print_r($variable, TRUE) .'</pre>', array(), WATCHDOG_INFO, NULL);

You can watch the log in Reports -> Recent log messages (admin/reports/dblog) if the core module "Database logging" is activated.