问题地址,http://drupal.org/node/1863434。当我在一个实际的生产站点,添加面包屑的时候,报错了,错误信息如下:
Notice: Undefined index: alias in path_form_element_validate() (line 156 in /var/www/eplus.cn/modules/path/path.module).
Recoverable fatal error: Argument 2 passed to drupal_array_set_nested_value() must be an array, null given, called in /var/www/eplus.cn/includes/form.inc on line 2518 and defined in drupal_array_set_nested_value() (line 6510 in /var/www/eplus.cn/includes/common.inc).
这是path模块报的错,我当时怀疑,是不是系统环境的问题,因为在我本地,一切都是好好的,但是到了线上,就出了问题。
我把线上的代码,下载到本地,然后使用NotePad++的强大的搜索功能,搜索都有哪个模块调用了path_form_element_validate函数,问题的原因一下子就找出来了,原来罪魁祸首是pathauto模块。pathauto.module里面有这样的一段代码:
/**
* Implements hook_field_attach_form().
*
* Add the automatic alias form elements to an existing path form fieldset.
*/
function pathauto_field_attach_form($entity_type, $entity, &$form, &$form_state, $langcode) {
list($id, , $bundle) = entity_extract_ids($entity_type, $entity);
if (!isset($form['path'])) {
// This entity must be supported by core's path.module first.
// @todo Investigate removing this and supporting all fieldable entities.
return;
}
else {
// Taxonomy terms do not have an actual fieldset for path settings.
// Merge in the defaults.
$form['path'] += array(
'#type' => 'fieldset',
'#title' => t('URL path settings'),
'#collapsible' => TRUE,
'#collapsed' => empty($form['path']['alias']),
'#group' => 'additional_settings',
'#attributes' => array(
'class' => array('path-form'),
),
'#access' => user_access('create url aliases') || user_access('administer url aliases'),
'#weight' => 30,
'#tree' => TRUE,
'#element_validate' => array('path_form_element_validate'),
);
}
我的眼睛一下子亮了,我们定义了同样的表单元素’path’, pathauto模块为它追加了验证规则path_form_element_validate。这是我休整后的样子:
function breadcrumb2_form($form, &$form_state, $breadcrumb) {
// Save the breadcrumb for later, in case we need it.
$form['#breadcrumb'] = $breadcrumb;
$form_state['breadcrumb'] = $breadcrumb;
$form['bid'] = array(
'#type' => 'value',
'#value' => isset($breadcrumb->bid) ? $breadcrumb->bid : NULL,
);
// Add the field related form elements.
field_attach_form('breadcrumb2', $breadcrumb, $form, $form_state);
$form['path'] = array(
'#type' => 'textfield',
'#title' => t('Path'),
'#maxlength' => 60,
'#default_value' => !empty($breadcrumb->path) ? $breadcrumb->path : '',
'#weight' => -10,
);
if (!empty($breadcrumb->path)) {
$form['path']['#disabled'] = TRUE;
}
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
'#weight' => 40,
);
if (!empty($breadcrumb->bid)) {
$form['actions']['delete'] = array(
'#type' => 'submit',
'#value' => t('Delete breadcrumb'),
'#weight' => 45,
'#limit_validation_errors' => array(),
'#submit' => array('breadcrumb2_form_submit_delete')
);
}
$form['#validate'][] = 'breadcrumb2_form_validate';
$form['#submit'][] = 'breadcrumb2_form_submit';
return $form;
}
就是将field_attach_form放到了$form['path']的前面,两者调整了一下位置而已,这样pathauto模块就找不到$form['path']这个元素了。pathauto模块里面的注释让我害怕,它要把它的$form['path']添加到所有的实体表单上。还让不让人活啊。不过,我们这里的办法仍然适用。