Scenario:
I am programmatically trying to import pagerouts as part of creating a 'mysite' module. I exported my pageroutes and saved them into files. I know that I will need to handle the list of roles at some point. For now, as I have an empty database, I know what my role numbers will be, so that is not an issue at this time.
The problem I have is that the first one is imported and created and the subsequent ones fail, but create unnamed/empty pageroutes.
Symptom:
warning: Invalid argument supplied for foreach() in /websites/{mysitename}/sites/all/modules/pageroute/pageroute_ui.forms.inc on line 493.
Diagnosis:
This error message is displayed because the variable 'route' on line 487 is not set.
Further debugging shows that the call order on the first import is to pageroute_ui_import_validate, then to
pageroute_ui_import_submit. The subsequent calls do not invoke pageroute_ui_import_validate and go directly to
pageroute_ui_import_submit. I've tried to debug further, but this is getting way involved and I have to meet
a deadline.
I have found a hack-around. If you test 'route' and it is FALSE, if you just invoke validate again, I can get
the imports to work. For now I will use that.
Sample code for how I invoke import:
function mymod_create_pageroutes($pageroute_file) {
include_once('./'. drupal_get_path('module', 'pageroute') .'/pageroute.page_add.inc');
include_once('./'. drupal_get_path('module', 'pageroute') .'/pageroute_ui.forms.inc');
$form_id = 'pageroute_ui_import';
$modulepath = drupal_get_path ('module', 'mymod');
$pageroute_definition_file = $modulepath ."/pageroutes/" . $pageroute_file;
$values = array();
$values['import'] = file_get_contents($pageroute_definition_file);
$values['op'] = 'Import';
$form_state = array();
$form_state['values'] = $values;
drupal_execute($form_id,$form_state);
}
Code for 'hack-around':
add after line 487 - $route = &$form_state['route'];
// there is a problem that validate is not being called on multiple calls
// when not validated, route is FALSE -- in that case force a validation
if ($route == FALSE) {
pageroute_ui_import_validate($form, &$form_state);
$route = &$form_state['route'];
if ($route == FALSE) {
return FALSE;
}
}