1

which is the best way of debugging codes in drupal? for instance in election module there is a submodule called conditions, and there is a function:

function election_example_conditions_access_gmail($post, $account) {
  if ($account->uid == 0) {
    return FALSE;
  }
  return (bool) preg_match('/g(oogle)?mail\.com$/i', $account->mail);
}

in the above code, what is the easiest and fastest way to find out what is the $account object passed to function?

1

You can make use of the devel module for debugging.

Download and install the latest version of the Devel module.

dpm() Prints a variable to the ‘message’ area of the page using

dvm() Uses var_dump() to print a variable to the ‘message’ area of the page

So in your situation in your function use dpm($account)

function election_example_conditions_access_gmail($post, $account) {
   dpm($account)
  if ($account->uid == 0) {
    return FALSE;
  }
  return (bool) preg_match('/g(oogle)?mail\.com$/i', $account->mail);
}

and go to the page normally and you should see the debug backtrace appear in the message area

|improve this answer|||||
  • are these for drupal 7? and do "print_r" or "echo" or "dsm" work in this case? – shidsun Jun 20 '13 at 14:37
  • It is both for Drupal 6 and Drupal 7. "dsm" won't work without installing the devel module. With things like prin_r, it is easy to forget them in your code. However, when you use the devel module, only someone with the required permission can see it. Devel also makes it look more presentable and easy to read. – 4life Jun 20 '13 at 15:16

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.