1

I am working on my own form module for Drupal 7.

I tried to set some default text depending on a cookie was set or not. It works perfectly as long as your are logged in; once you are an anonymous user, it doesn't read/write the cookie.

I tried to debug this by putting in a few calls to echo(), and drupal_set_message(), but they are only shown when I'm logged in; they don't show for anonymous users. I can't figure out why.

Can anyone help me with this?

This is the code I am using.

$serviceType = "";
$serviceName = "";
if (isset($_COOKIE["kms_hoststring_name"])) {
  $serviceName = $_COOKIE["kms_hoststring_name"];
  $serviceType = $_COOKIE["kms_hoststring_type"];
}
drupal_set_message("test");
echo "TEST";
$form['serviceType'] = array(
  '#type' => 'textfield',
  '#title' => t('Service Type:'),
  '#default_value' => t($serviceType),
);

$form['serviceName'] = array(
  '#type' => 'textfield',
  '#title' => t('Service Name'),
  '#default_value' => t($serviceName),
);

//Deletes cookies after use.
echo "TEST1";
if (isset($_COOKIE["kms_hoststring_name"])){
  setcookie("kms_hoststring_name", "", time()-60000, "/");
  setcookie("kms_hoststring_type", "", time()-60000, "/");
  echo "TEST2".$_COOKIE["kms_hoststring_name"];
}

None of the echo() or drupal_set_message() calls output anything for anonymous users.
When I log in, I see the output of those function calls.

3
  • 2
    Perhaps you could add a sample of your code? It's hard to diagnose your issue without knowing what you're actually trying to do :(
    – Chapabu
    Dec 20 '12 at 9:43
  • Added a code sample.
    – letifere
    Dec 20 '12 at 9:59
  • What function has the code? Probably, the whole code block is executed for authenticated user only.
    – Sithu
    Dec 20 '12 at 11:23
2

Drupal have much better tools for debugging than echo. You can sent a message to the watchdog log with the watchdog function. And if you install the devel module you get a few more funcions like dpm() to easily send messages or dd() to write onto the drupal debug log.

1
  • 2
    The devel module also needs to set correct permissions, otherwise, dpm() will output nothing for anonymous users.
    – Sithu
    Dec 20 '12 at 11:26

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.