I need to display a page in drupal that is created by a template and based on a specific record from my table.
I created the module “item” (as an example).
I made the item_menu hook:
$items['items/%item'] = array(
'title' => 'Items',
'page callback' => 'drupal_get_form',
'page arguments' => array('show_item', 1),
'access callback' => true,
'access arguments' => array(),
);
I created a load function for the item:
function item_load($itemid)
{
$sql = 'SELECT * FROM {items} it WHERE it.id = :itemid';
$result = db_query(
$sql,
array(':itemid' => $itemid),
array( 'target' => 'mydatabase' ));
$item = $result->fetchObject();
return $item;
}
Now I want to use a template to display the specifics for the item, so I made a item module
a item.tpl.php. and a item_theme hook to register the template:
function item_theme($existing, $type, $theme, $path)
{
return array(
'show_item' => array(
'template' => 'item',
'variables' => array(),
),
);
}
The final thing I need to do is pass the item object to the item.tpl.php so I can display the item properties.
But I don’t know how to do this. How can I make the item known within the template??
I hope the question is clear enough. Thanks in advance.
[edit]
I found that arg(1) contains the value of the wildcard from the menu page arguments, should I use arg(1) to look up the item in the database?
You need to declare the names and default values for the vars you want to use in the
variablespart of your array inhook_theme:Then call the theme function like this:
** UPDATE **
To address the
load()function being called 4 times you can use static vars to avoid the query being run every time: