Drupal Developer FAQ

Average: 4 (1 vote)

FAPI - How to decrease/increase the text limit of textfields?

Set the #maxlength property.

// Only allow 5 characters.
$form['example'] = array(
  '#type' => 'textfield',
  '#maxlength' => 5,
  // ...
);

The default value is 128.

Average: 1 (1 vote)

Help! - Cannot access a global variable.

Suppose you have the following module. To your dismay, the value of the variable you think is global is not accessible:

// example.module

$myvar = 'foo';

function example_something() {
  global $myvar;
 
  return $myvar; // returns NULL
}

Cause

From http://www.php.net/manual/en/function.include.php

When a file is included, the code it contains inherits the variable scope of the line on which the include occurs.

As module files are loaded by drupal_load, it follows that $myvar is a variable declared in the local scope of the function drupal_load.

Solution

Use the global keyword or $GLOBALS superglobal.

// example.module

// Declare and initialize $myvar as an explicit global:

global $myvar;
$myvar = 'foo';

// Or use the superglobal:

$GLOBALS['myvar'] = 'foo';

function example_something() {
  global $myvar;
 
  return $myvar; // returns 'foo'
}

Average: 5 (1 vote)

How do I add a class to a link generated with l()

You can add attributes by using the 'attributes' key in the options array:

l('text', 'path', array('attributes' => array('class' => 'myclass')));

No votes yet

How do I recreate a Javascript translation file?

  • Remove the file from [your_files_directory]/languages/ (if any)

Then execute the following queries on the database (see settings.php for which database is in use):

  • UPDATE languages SET javascript = '' WHERE LANGUAGE = '[yourlanguage]';
  • UPDATE variable SET value = 'a:0:{}' WHERE name = 'javascript_parsed';
  • TRUNCATE cache;

If you now visit the relevant page, the translation will be regenerated.

Average: 3.3 (3 votes)

Implemented hook_block, now all blocks are gone

If all your blocks disappear from the site the moment you add a hook_block implementation to your module, check whether you have a theme with the same name as your module.

Cause: The theme system uses your hook_block implementation as if it were a theme_block implementation.

Either rename your module or the theme.

Average: 4.9 (9 votes)

Why is a menu item from hook_menu not accessible?

  1. Is your module enabled?
  2. Whenever you make a change to your module's hook_menu implementation, you need to rebuild the router table by visiting admin/build/modules. The Devel module provides a block that has a menu rebuild shortcut.
  3. Does you hook_menu implementation return the menu item array?
  4. Is your hook_menu properly named as modulename_menu?
  5. Carefully check whether the array keys you use are correct. A common mistake is the use of 'page_callback' instead of the correct 'page callback'.
  6. Make sure that your hook_menu implementation is in the .module file, not an include file.
Average: 5 (4 votes)

Why is my module's update hook not listed on update.php's selection form?

Apart from obvious causes (module not enabled, update hook misnamed), there's a actual pitfall here; You cannot use mixed case filenames for modules.

Background

Suppose you have a module Example with Example.module and Example.install as files. Example.install contains your Example_update_6001() function.

Update.php uses the PHP function get_defined_functions to enumerate update hooks. The function returns lowercase functionnames (PHP function names are case insensitive). Drupal, however, does a case-sensitive string compare (strpos) with the module name. These will never match.

Solution

Use all lowercase module names (example.module vs. Example.module, mymodule.module vs. MyModule.module). Alternatively, wait for a bugfix via Issue #200628.

No votes yet