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



我正在构建一个自定义字段模块与我们的流媒体视频提供商工作。作为其中的一部分,我需要从提供商的API中抓取基于选定类别的视频。该字段将需要存储许多部分—所选的类别、所选的视频和一些尚未添加的其他详细信息(如标题)。

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

我已经浏览了D7的字段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;
}

可能是AJAX回调,在AJAX回调中,使用

$form_state['rebuild'] = TRUE;

或将ajax值赋给一个新元素,使其在$form_state数组中。使用devel和dpm($form_state)。我打赌你的值是"

相关内容

  • 没有找到相关文章

最新更新