Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Drupal Basics Writing Modules Create Custom Forms in a Module

David Bath
David Bath
25,940 Points

Cannot enable Sum module

I am following along with the Create Custom Forms in a Module video for Drupal Basics, and I've gotten to the point where I need to enable the module. When I click Save Configuration the list of modules is redisplayed (with no confirmation at the top) and my Sum module is still unchecked. If I try the same thing a second time I get a big white 500 error page. It doesn't matter if I clear the cache. So I'm thinking it's something in my module code, but I can't spot it. Does anything look incorrect here?

<?php

function sum_menu() {
    $items = array();
    $items['sum'] => array(
        'title' => 'Calculate Sum',
        'type' => MENU_NORMAL_ITEM,
        'access callback' => TRUE,
        'page callback' => 'drupal_get_form',
        'page arguments' => array('sum_form')
    );

    return $items;
}

function sum_form() {
    $form = array();
    $form['left_number'] = array(
        '#title' => 'Left Number',
        '#type' => 'textfield',
        '#description' => t('Please enter your starting number.')
    );

    $form['right_number'] = array(
        '#title' => 'Right Number',
        '#type' => 'textfield',
        '#description' => t('Please enter a number to add to the starting number.')
    );

    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => 'Calculate Sum'
    );

    return $form;
}
Henrik Hansen
Henrik Hansen
23,176 Points

What does your .info file look like?

David Bath
David Bath
25,940 Points
name = Sum
description = Displays the sum of two numbers using a form for input
package = My First Modules
core = 7.x
files[] = sum.module

Pretty much what you would expect, I'd imagine.

3 Answers

Henrik Hansen
Henrik Hansen
23,176 Points
// This should be $items['sum'] = array(); 
 $items['sum'] => array(
        'title' => 'Calculate Sum',
        'type' => MENU_NORMAL_ITEM,
        'access callback' => TRUE,
        'page callback' => 'drupal_get_form',
        'page arguments' => array('sum_form')
    );
David Bath
David Bath
25,940 Points

Ha! It's amazing to me that such a minor syntax error would not be handled in some way by Drupal to provide a more helpful exception.

Thanks so much for the help!

Henrik Hansen
Henrik Hansen
23,176 Points

Took me a while to see that as well ^_^

Henrik Hansen
Henrik Hansen
23,176 Points

For error reporting in your Drupal installations, add these lines in your DrupalDir/sites/default/settings.php

error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);

https://www.drupal.org/node/1056468

Michael Hall
PLUS
Michael Hall
Courses Plus Student 30,909 Points

Thank you Henrik Hansen, that bit of code is very helpful. I had a misspelled word, I might have never seen it