当语言发生变化时,是否有办法绑定ajax回调,我想在语言更改时更新nodereference-dropdown(仅显示该语言的值)。
以下代码无效(form_alter),但其他回调正在运行。
有人可以帮助我,我怎样才能实现这一目标?
$form['language']['#ajax'] = array(
'callback' => 'mymodule_something_language_callback',
'wrapper' => 'my-module-replace',
'#weight' => 2
);
感谢。
FROM COMMENTS
继承了$ form ['language']的var_dump;
array
'#type' => string 'select' (length=6)
'#title' => string 'Language' (length=8)
'#default_value' => string 'und' (length=3)
'#options' =>
array
'und' => string 'Language neutral' (length=16)
'en' => string 'English' (length=7)
'ar' => string 'Arabic' (length=6)
答案 0 :(得分:0)
问题是Locale模块在调用hook_form_alter()之后改变了这个表单元素(参见this post)。
以下是我解决这个问题的方法:
首先,更改Drupal实现其挂钩的顺序,将'form_alter'作为最后一个:
<?php
/**
* Implementation of hook_module_implements_alter()
*/
function chronos_module_implements_alter(&$implementations, $hook) {
if ($hook == 'form_alter') {
// Move mymodule_form_alter() to the end of the list. module_implements()
// iterates through $implementations with a foreach loop which PHP iterates
// in the order that the items were added, so to move an item to the end of
// the array, we remove it and then add it.
$group = $implementations['chronos'];
unset($implementations['chronos']);
$implementations['chronos'] = $group;
}
}
接下来,在$ form ['language']上添加一个表单元素和'#ajax'元素:
<?php
/**
* Implements hook_form_alter().
*/
function mymodule_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'page_node_form') {
// alter the form
$form['container'] = array(
'#prefix' => '<div id="ajax-language">',
'#suffix' => '</div>',
);
$form['language']['#ajax'] = array(
'callback' => 'mymodule_save_language_callback',
'wrapper' => 'ajax-language'
);
return $form;
}
}
最后,添加您的回调:
<?php
/**
* Returns changed part of the form.
*
* @return renderable array
*
* @see ajax_example_form_node_form_alter()
*/
function chronos_save_language_callback($form, $form_state) {
# set session variables or perform other actions here, if applicable
return $form['container'];
}