Drupal 7自定义字段小部件不保存用户输入

时间:2013-03-12 17:09:17

标签: drupal drupal-7 drupal-modules drupal-forms drupal-field-api

我正在构建一个自定义字段模块,以便与我们的流媒体视频提供商合作。作为其中的一部分,我需要根据提供商的API中的选定类别抓取视频。该字段需要存储多个部分 - 所选类别,所选视频以及尚未添加的一些其他详细信息(如标题)。

我可以将此表单显示在节点编辑页面上(将字段附加到内容类型之后),但它似乎没有保存用户的输入。以下是编辑节点屏幕上的表单:Field on node editing screen。用户的工作流程是他们选择一个类别,该类别填充要选择的视频列表。当他们选择视频时,会在$ element ['teacherstv'] ['teacherstv_video_details']中显示预览。这一直工作到用户保存表单的程度。此时,所选值将丢失。

我查看了D7的Field API和Forms API文档,但看不到有关如何设置此类字段窗口小部件的任何明确说明,并让它保存用户输入。

这是hook_field_widget_form()

的代码
function teacherstv_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
//  $value = $isset($items[$delta['teacherstv']) ? $items[$delta]['teacherstv'] : '';
  $widget['#delta'] = $delta;

  //lets get an array of our categories from core
  $coreapi = new coreapi();
  $APIsettings = variable_get('teacherstv_API_settings');
  $coreapi->apiuser = $APIsettings['username'];
  $coreapi->apipwd = $APIsettings['password'];
  $coreapi->apiurl = $APIsettings['api_url'];
  $coreapi->sortcriteria = "alpha";

  //$categoriesavailable = $coreapi->get_categories();
  $categories = coreapi_get_categories();
  $defaults = $field['settings'];
  $settings = $instance['settings'];

  $category = !is_null($form_state['values'][$field['field_name']]['und'][$delta]['teacherstv']['teacherstv_category']) ? $form_state['values'][$field['field_name']]['und'][$delta]['teacherstv']['teacherstv_category'] : $defaults['teacherstv']['defaultcategory'];

  switch ($instance['widget']['type']) {

    case 'teacherstv':

      $element['teacherstv'] = array(
        '#tree' => TRUE,
        '#type' => 'fieldset',
        '#title' => t('TeachersTV Video'),
        '#description' => '<p>' . t('Select a video from the TeachersTV service to embed.') . '</p>',
        '#delta' => $delta,
      );

      $element['teacherstv']['teacherstv_category'] = array(
        '#type' => 'select',
        '#field_parents' => 'teacherstv',
        '#title' => t('Video Categories'),
        '#options' => $categories,
        '#description' => t('Select a video category for a list of videos'),
        '#default_value' => !is_null($settings['teacherstv_category']) ? array($settings['teacherstv_category']) : array($category),
        '#ajax' => array(
          'callback' => 'teacherstv_ajax_videolist',
          'wrapper' => 'teacherstv-videolist-' . $delta . '-div',
          'method' => 'replace',
        ),
        '#delta' => $delta,
      );

  $videos = coreapi_list_videos($category);
  $videos[0] = "--Please select a video--";
  asort($videos);

      $element['teacherstv']['teacherstv_video'] = array(
        '#type' => 'select',
        '#title' => t('Select a video'),
        '#field_parents' => 'teacherstv',
        '#prefix' => '<div id="teacherstv-videolist-' . $delta . '-div">',
        '#suffix' => '</div>',
        '#options' => array(0 => 'Video 1'),
        '#default_value' => 
        isset($form_state['values'][$field['field_name']]['und'][$delta]['teacherstv']['teacherstv_video']) ? $form_state['values'][$field['field_name']]['und'][$delta]['teacherstv']['teacherstv_video'] : NULL,
        '#description' => t('Select a video.'),
        '#options' => $videos,
        '#ajax' => array(
          'callback' => 'teacherstv_ajax_videoselect',
          'wrapper' => 'teacherstv-videodetails-' . $delta . '-div',
        ),
        '#delta' => $delta,
      );

      $video_keys = array_keys($videos);
      $selected_video = isset($form_state['values'][$field['field_name']]['und'][$delta]['teacherstv_category']) ? $form_state['values'][$field['field_name']]['und'][$delta]['teacherstv_category'] : NULL;
      $element['teacherstv']['teacherstv_video_details'] = array(
        '#type' => 'markup',
        '#field_parents' => 'teacherstv',
        '#title' => t('Video details'),
        '#prefix' => '<div id="teacherstv-videodetails-' . $delta . '-div">',
        '#suffix' => '</div>',
        '#description' => t('Details about the video.'),
        '#markup' => teacherstv_ajax_render_video($selected_video),
        '#delta' => $delta,
      );

      break;

  }
  return $element;
}

1 个答案:

答案 0 :(得分:0)

它可能是AJAX回调,在你的AJAX回调中,使用

$form_state['rebuild'] = TRUE;

或将ajax值分配给新元素以将其放在$ form_state数组中。 使用devel和dpm($ form_state)。我打赌你的价值是“”