Hello everyone!

After searching for a long time I am still without an answer, so I apologize in advance for having to ask for help. I started working on the form API tutorial, but I can't get my sample code to load. Here is the snippet in question:

<?php

/** 
 * This function defines the URL to the page created etc.
 * See http&#58;//api.drupal.org/api/function/hook_menu/6
 */
function walkthrough_menu() {
  $items = array();
  $items['walkthrough/form'] = array(
    'title' => t('My form'),
    'page callback' => 'walkthrough_form',
    'access arguments' => array('access content'),
    'description' => t('My form'),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

Error is coming from line 8, saying there is an unexpected t_variable. Any help on what I've done wrong? You'll really be setting me on the right path, so thank you in advance!
-JB

Comments

jaypan’s picture

Are you sure you are looking at the right file? Because everything there is fine. It will tell you the file name in the error - check that.

Contact me to contract me for D7 -> D10/11 migrations.

dabellator’s picture

I thought everything looked fine as well! I know it's the right snippet of code because when I delete it I avoid the problem, site comes back up. I messed up something when I created the file is my belief. I'm running on a local host through MAMP, and I created the file using a text program. My module doesn't have an .install file, could I be missing anything else I need?
-JB

jaypan’s picture

The code itself is fine. Maybe you have a corruption in the file, or maybe there are some invisible metacharacters that is screwing up your code.

To fix this, copy it, then open a new file in notepad (Windows) or textedit (OSX) and paste the code into this file. Save the file, overwriting the original (by using the same filename), and see if this helps. Neither of these programs should allow for any metadata to be added to the file, so hopefully this will solve your problem.

There is nothing wrong with that code though. I can say that much.

Contact me to contract me for D7 -> D10/11 migrations.

dabellator’s picture

I'm reaching here to find anything that could be wrong. Is it possible that the API could be corrupted, or not installed correctly? I know it seems strange since the rest of my site functions without this module, but there has to be something going on here!
-JB

heine’s picture

You most likely have a character on line 8 that is treated as whitespace by your editor, but not by PHP. Remove all whitespace, then add spaces.

dabellator’s picture

Yep, it didn't like any of the white space, but changing editors didn't do the trick, I ended up doing what you recommended and deleting all the white space and re entering as space. Thanks for the input.
-JB

Web Assistant’s picture

Don't use the t() function for titles in hook_menu().

From the docs:

"title": Required. The untranslated title of the menu item.

dabellator’s picture

Can you help me out with that a little further? My code snippet comes from Drupal documentation, and I see in a lot of places Drupal uses the t() function, except in the link you provided. What happens with that function? Should we never use t(), or is it just with hook_menu?

Thanks for you help!
-JB

jaypan’s picture

Just with hook_menu(). Hook menu allows for a custom 'page callback', as well as 'page arguments'. It also allows for 'access callback', as well as 'access arguments'. And finally, it allows for 'title callback', and 'title' (which really can be thought of as 'title arguments'. The title callback is set to t() as a default, so you don't need to set it unless you are overriding it. So if you pass the title through t(), it then gets passed through the title callback after that - which is also t(). The second time you would be attempting to translate the already translated title. So in the hook_menu() title attribute, you don't pass it through t().

Contact me to contract me for D7 -> D10/11 migrations.

dabellator’s picture

Great reply, thank you for explaining it!
-JB