I am trying to upgrade a module from 5.x to 6.x.
Need some help. I can't seem to figure out how to theme a form.
When I click on the menu item item for the form, I get this warning:
warning: Invalid argument supplied for foreach() in includes/menu.inc on line 258.
I see the realms listed, but the form isn't rendered at all. I put a "drupal_set_message" in the "theme_node_multinode" function, and I don't see it when I click on the form. This means that function is failing as soon as it is hit, or not being hit at all.
Here's the code snippet for the form. With the exception of the hook_menu code, it's more or less the same as in 5.x version of patch:
//////////////////
/// hook_menu ////
//////////////////
// multinode_access
$items['admin/content/multinode'] = array(
'title' => t('Multinode user interface'),
'description' => t('Interface for configuring multiple node access.'),
'page callback' => 'drupal_get_form',
'page arguments' => array('node_multinode'),
'access arguments' => user_access('administer nodes'),
'type' => MENU_NORMAL_ITEM,
);
//////////////////////////////////////
////// Multinode Access Form ////////
/////////////////////////////////////
/**
* Generate multinode access UI form.
*/
function node_multinode($theme = NULL) {
global $theme_key, $custom_theme, $user;
if (isset($uid)) {
$account = user_load(array('uid' => $uid));
} else {
$account = $user;
$uid = $user->uid;
}
// Get whatever is stored in multinode table.
$existing = array();
$result = db_query("SELECT * FROM {multinode_access}");
while ($row = db_fetch_object($result)) {
$existing[$row->realm] = $row;
}
// Get all rule realms from node_access table.
$result = db_query("SELECT DISTINCT realm FROM {node_access}");
while ($row = db_fetch_object($result)) {
$rules[] = $row->realm;
}
// Get all rule realms from modules. User doing this should have grants in all modules affected.
$op = 'view';
$mrules = node_access_grants($op, $account);
foreach ($mrules as $realm => $grants) {
$rules[] = $realm;
}
// Get only unique values
$rules = array_unique($rules);
// Sort the rule realms
asort($rules);
$form['#tree'] = TRUE;
foreach ($rules as $realm) {
$form[$realm]['checkbox'] = array('#type' => 'checkbox', '#default_value' => (isset($existing[$realm]->realm) ? 1 : 0));
$form[$realm]['realm'] = array('#type' => 'textfield', '#size' => 25, '#disabled' => TRUE, '#value' => check_plain($realm), '#default_value' => $realm);
$form[$realm]['group'] = array('#type' => 'textfield', '#size' => 5, '#default_value' => (isset($existing[$realm]->groupname) ? $existing[$realm]->groupname : '') );
$form[$realm]['logic'] = array('#type' => 'select', '#default_value' => (isset($existing[$realm]->logic) ? $existing[$realm]->logic : 'AND'), '#options' => array('AND' => 'AND', 'OR' => 'OR') );
$form[$realm]['weight'] = array('#type' => 'select', '#default_value' => (isset($existing[$realm]->weight) ? $existing[$realm]->weight : '0'), '#options' => array('0' => '0', '1' => '1', '2' => '2', '3' => '3', '4' => '4', '5' => '5', '6' => '6', '7' => '7', '8' => '8', '9' => '9') );
$form[$realm]['check'] = array('#type' => 'select', '#default_value' => (isset($existing[$realm]->checkstatus) ? $existing[$realm]->checkstatus : '0'), '#options' => array('0' => '0', '1' => '1') );
}
$form['submit'] = array('#type' => 'submit', '#value' => t('Save changes'));
return $form;
}
/**
* Theme function to render the table for the node_multinode
* Multinode access UI
*/
function theme_node_multinode($form) {
$output .= "\n<div id=\"node-multinode-form\">\n";
$output .= '<div id="desc">'. t('Here you can configure multinode access. You can only modify values here if TAC/OG Integration is ON. If you turn TAC/OG Integration ON, then later wish to turn it off, you must first UNCHECK all items here and <strong>Save changes</strong> (otherwise, multinode access will continue).') ."</div>\n";
$header = array(t(''), t('Realm'), t('Group'), t('Logic'), t('Weight'), t('Check'));
$rows = array();
foreach (element_children($form) as $i) {
$block = &$form[$i];
$rows[] = array(drupal_render($block['checkbox']), drupal_render($block['realm']),
drupal_render($block['group']), drupal_render($block['logic']),
drupal_render($block['weight']), drupal_render($block['check']),
);
}
$output .= theme('table', $header, $rows, array('id' => 'node-multinode-table'));
$output .= drupal_render($form['submit']);
$output .= "</div>\n";
$output .= drupal_render($form);
return $output;
}
/**
* Process multinode form submission.
*/
function node_multinode_submit($form, $form_state) {
// Erase all existing settings
db_query ("DELETE FROM {multinode_access}");
// Insert new settings
foreach ($form_state['values'] as $block) {
if ($block['checkbox'] == 1) {
db_query("INSERT INTO {multinode_access} (realm, groupname, logic, weight, checkstatus) VALUES ('%s','%s','%s','%s',%d)", $block['realm'], $block['group'], $block['logic'], $block['weight'], $block['check']);
}
}
drupal_set_message(t('The multinode access settings have been updated.'));
cache_clear_all();
}
This is a general depiction of the form I'm trying to generate, modified for 6.x and specific to node module now:
http://drupal.org/files/issues/OGRConfigureMultinodeUI_ACL.jpg
What I'm getting is this:
http://drupal.org/files/issues/multinode_ui_error.jpg
Can someone who is way more familiar with 6.x than I am tell me what I'm doing wrong here? Thanks.
Comments
Added appropriate code to hook_theme
Added this code to hook_theme to register the theme:
as per: http://drupal.org/node/114774#theme_registry
No change.
Sounds more like a menu issue to me
The error is happening in a function unserialising menu data from the database. That makes me suspect the menu hook or data in the menu tables rather than the theme functions.
You don't mention whether or not you cleared the cache after adding the theme hook. There may be dodgy menu data in the cache as well.
I don't know if it would cause problems, but your module seems to be taking the node modules namespace - what happens if you prefix all the hook and function names with something else?
--
Anton
New to Drupal? | Troubleshooting FAQ
Example knowledge base built with Drupal
Whoa! That's it?
Spent all day trying to figure out what I was doing wrong. I'm sure I optimized the code.
But, clearing the cache does the trick. For anyone else who runs into this, go to: admin->site settings->performance and click on the "clear cached data" button.
Thank you styro!
Cool
Clearing the cache is something you'll learn to do often when developing for Drupal 6 :)
--
Anton
New to Drupal? | Troubleshooting FAQ
Example knowledge base built with Drupal