Drupal 7 Form Api在text_format字段上的AJAX回调不起作用

时间:2014-07-19 21:47:17

标签: ajax forms api drupal field

我的自定义模块中有一个字段

$form['update']['text'] = array(
    //'#type' => 'textarea',
    '#type' => 'text_format',

如果我使用textarea一切正常,但如果我使用text_format我的ajax回调不会改变字段的值。

function maintenance_update_form_update_callback($form, $form_state) {
  $entry = $form_state['entries'][$form_state['values']['aid']];
  // Setting the #value of items is the only way I was able to figure out
  // to get replaced defaults on these items. #default_value will not do it
  // and shouldn't.

  dpm($form_state['entries'][$form_state['values']['aid']]);

  foreach (array('status', 'type', 'title', 'text', 'name', 'email', 'phone', 'notes') as $item) {
    $form['update'][$item]['#value'] = $entry->$item;
    //dpm($entry);
    //drupal_set_message("entry->item értéke: ENTRY: $entry, ITEM: $item , $entry->$item");
  }

这个领域有什么问题?据我所知,这种类型也支持ajax请求....

  //drupal_set_message('Callback $form');
  dpm($form);
  return $form;
}

2 个答案:

答案 0 :(得分:3)

非常简单 - text_format 不是简单的单值表单项。它需要参数的数组

array(
 'value' => '123123',
 'format' => 'html'
)

因此,如果您想更改值,请尝试以下代码:

$form['update']['text']['value']['#value'] = 'text message';

答案 1 :(得分:0)

添加到@ProFak解决方案,如果要更改text_format字段类型的格式,则必须使用以下代码

$altered_text_format = 'full_html';

$form['update']['format']['format']['#value'] = $altered_text_format;

所以完整的解决方案如下所示

function maintenance_update_form_update_callback($form, $form_state) {
  $entry = $form_state['entries'][$form_state['values']['aid']];
  // Setting the #value of items is the only way I was able to figure out
  // to get replaced defaults on these items. #default_value will not do it
  // and shouldn't.

  foreach (array('status', 'type', 'title', 'text', 'name', 'email', 'phone', 'notes') as $item) {
        // @ProFak Solution
        $form['update']['text']['value']['#value'] = $entry->$item;
        // @Sukhjinder Singh additional solution
        $altered_text_format = 'full_html';
        $form['update']['format']['format']['#value'] = $altered_text_format;
 }

 return $form['update'];
}