【问题标题】:Drupal 7 Form Api AJAX callback on a text_format field do not work文本格式字段上的 Drupal 7 Form Api AJAX 回调不起作用
【发布时间】:2014-07-19 21:47:16
【问题描述】:

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

$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;
}

【问题讨论】:

    标签: ajax forms api drupal field


    【解决方案1】:

    非常简单 - text_format 不是简单的单值表单项。像这样的 value 参数需要数组

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

    所以,如果你想改变值试试这个代码:

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

    【讨论】:

    • 非常感谢 ProFak!你是最好的:-) 我不专心。现在可以使用了。
    • 我在表单字段中使用了相同的 text_format,但是用户在字段中输入的值并没有反映在表单状态中。 $form['update']['value'] 为空。知道为什么吗?
    【解决方案2】:

    添加到@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'];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-17
      • 1970-01-01
      • 2015-01-05
      • 1970-01-01
      • 1970-01-01
      • 2013-03-14
      相关资源
      最近更新 更多