*質問への回答:カスタムモジュールのhook_node_submitでデータを保存するにはヘルプが必要です *
Drupal 7では、カスタムコンテンツタイプがあります。これには、そのコンテンツのカテゴリを選択するための複数選択用語参照があります。
次に、以前に選択したカテゴリから1つを選択し、何らかの方法で「メイン」カテゴリとしてマークする必要があります。
次のオプションを持つ複数選択用語参照フィールドがあるとします。
Apples
Bananas
Pears
Oranges
Grapes
Pineapples
ユーザーは、リンゴ、梨、ブドウを選択します。今私はどちらかが必要です:
- プログラムでこれらの選択した項目ごとに別のフィールドを作成し(おそらくAjaxコールバックを使用)、ラジオボタンを使用して、選択した用語の1つだけを選択できるようにします。
- チェックされたアイテムの横に(おそらくAjaxを使用して)ラジオフィールドを作成します。ここで、選択されたアイテムからメインのラジオフィールドを選択できます。
誰かこれについて何か考えがありますか?
より明確にするために、私は1つのコンテンツタイプにこれらのリストを多数用意しています。各リストを単一の値リストとして繰り返すことはオプションではありません。
私の最善の策は、hook_form_alter()ある種のAJAXコールバックでを使用して、ユーザーがチェックしたティックボックスの横に単一のラジオボタンを作成するか、指定されたリストでチェックされた各項目の新しいラジオフィールドリストをプログラムで作成することです。
更新: わかりました、それを行うための最良の方法は、ajaxを使用して、チェックされた各チェックボックスのラジオボタンを作成するカスタムモジュールを作成し、メイン要素として使用する要素を選択できるようにすることです。
したがって、税条件の値にアクセスする前にフォームがレンダリングされるのを待つ必要があるためhook_form_alter()、#after_build関数を追加するために使用しました。
これがこれまでの私のモジュールです。私はたくさんのコメントを使用しているので、私が何をしようとしているのかがはっきりしているはずです:
MYMODULE.module
/**
 * Implementation of HOOK_form_alter()
 * Do the ajax form alteration
 */
function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
  // 1.CONTENT FORM
  // I created a custom content type 'content' and added a term
  // reference to it 
  if($form_id == 'content_node_form') {
    // tax term ref is the main part, so let us
    // remove title and body fields
    unset($form['body']);
    unset($form['title']);
    // do our stuff after the form has been rendered ...
    $form['#after_build'][] = 'MYMODULE_after_build';
  }
}
/**
 * after_build function for content_node_form
 */
function MYMODULE_after_build(&$form, &$form_state) {
    dsm($form);  
    // In the after_build call we can now actually use the 
    // element_children function to grab the values of the fields that
    // don't start with a hash tag #
    // in this test case 1,2,3,4 and 5
    // wrap each of the elements rendered ...
    foreach(element_children($form['field_taxonomy']['und']) as $key) {
      $form['field_taxonomy']['und'][$key] += array(
        // this is added before the element and then replaced by our callback ..
        // we use the $key value in the id so that we know which div to replace 
        // depending on which checkbox is checked ...
        '#prefix' => '<div class="taxonomy_term_wrapper">
                        <div id="callback_replace_'.$key.'">Replace Me ' . $key . '</div>',
        // this is added after the element so we basically wrap around it ..
        '#suffix' => '</div>',
        // add some ajax stuff here ...
        '#ajax' => array(
          // name of the callback function to call upon change
          'callback' => 'MYMODULE_callback',
          // the id of the element that will be replaced
          'wrapper' => 'callback_replace_'.$key,
          // replace the wrapper
          'method' => 'replace',
          // what kind of effect do we want ...
          'effect' => 'fade',
          // show progress on callback
          'progress' => array('type' => 'throbber'),
        ),
      ); 
      if (!empty($form_state['values']['field_taxonomy']['und'][$key])) {
        // the form to show upon change ...
        $form['field_taxonomy']['und']['main_cat'] = array(
          // we want a radio button
          '#type' => 'radio',
          '#title' => t('Test Title'),
          '#description' => t('Test Description ...'),
          '#default_value' => empty($form_state['values']['field_taxonomy']['und'][$key]) ?
                              $form_state['values']['field_taxonomy']['und'][$key] :
                              $form_state['values']['field_taxonomy']['und'][$key],
        );
      }
    }
  return $form;
} 
function MYMODULE_callback($form, $form_state) {
 return $form['field_taxonomy']['und']['main_cat'];
}
これは、ボックスをチェックする前の状態です。

レンダリングされたフォームのHTMLは次のとおりです。
