I've ran into a problem: I am following along with the book "Learning Drupal 6 Module Development" published by Packt Publishing.

I'm in chapter 6; this is the process my module is supposed to follow:

  1. hook: emailusers_menu
  2. page callback from hook: function: emailusers_compose. Pass the fourth item from the items symbol.
  3. if the passed value equals zero, tell the user that the value needs to be an integer
  4. if the account doesn't exist, let the user know
  5. pass the account to a form value
  6. continue happily

However, the following is happening:
When I enter an invalid user, the code does as expected and says "no such user found" (in the case of 0, "User ID must be an integer"); however, when I enter a valid user ID (I've tried with multiple users), I get a blank page.

Here is the code (only the necessary parts):

/**
 * Implementation of hook_menu()
 */
function emailusers_menu() {
	// Need to pass User ID here:
	$items['admin/emailusers/compose/%'] = array(
		'title' => 'Compose a Message',
		'page callback' => 'emailusers_compose',
		'page arguments' => array(3), // <- UserID (from % in node path)
		'access arguments' => array('administer users'),
		'type' => MENU_CALLBACK,
	);
	return $items;
}

function emailusers_compose($userid) {
	$userid = intval($userid);
	if ($userid == 0) {
		return t("User ID must be an integer.");
	}
	$account = user_load($userid);
	if (empty($account)) {
	{
		return t('No such user found.');
	}
	$to = $account->mail;
	$sb = '<p>'
		.t('Send a message to @email.', array('@email' => $to))
		.'</p>';
	$sb .= drupal_get_form('emailusers_compose_form', $account);
	return $sb;
	}
}

function emailusers_compose_form($context, $account) {
	// This is a value only -- equivalent to a hidden field, except
	// that it is never rendered into the HTML
	$form['to'] = array(
		'#type' => 'value',
		'#value' => $account,
	);
	// Create a fieldset for the body:
	$form['message'] = array(
		'#type' => 'fieldset',
		'#title' => t('Compose the Message'),
	);
	// Textfield for subject of the body
	$form['message']['subject'] = array(
		'#type' => 'textfield',
		'#title' => t('Subject'),
		'#size' => 50,
		'#maxlength' => 255,
		'#description' => t('The subject of the email message.'),
	);
	$form['message']['body'] = array(
		'#type' => 'textarea',
		'#title' => t("Message"),
		'#cols' => 50,
		'#rows' => 5,
		'#description' => t('The body of the email message.'),
	);

	//Create a fieldset for details
	$form['details'] = array(
		'#type' => 'fieldset',
		'#title' => t("Details"),
	);
	//Checkbox: if checked, BCC the author too
	$form['details']['cc_me'] = array(
		'#type' => 'checkbox',
		'#title' => t('BCC Yourself'),
		'#default_value' => 1,
		'#description' => t('If this is checked, the message will also be sent to you.'),
	);
	//Finally, a submit button:
	$form['submit'] = array(
		'#type' => 'submit',
		'#value' => t('Send Mail'),
	);
	return $form;
}

Can somebody help me figure out what's going on?

Comments

knpwrs’s picture

Oh wow, I feel dumb now. I had an extra curly bracket on the check to see if the account existed.