テーマDrupal Form APIはグリッドとしてチェックボックスをチェックします

drupal.stackexchange https://drupal.stackexchange.com/questions/389

  •  16-10-2019
  •  | 
  •  

質問

約2ダースのチェックボックスのフォーム要素を表示するカスタムフォームがあります。可能であれば、1行あたり3つのテーブルに出力したいと思います。どうすればそれをすることができますか?

$form['preference'] = array(
    '#type' => 'checkboxes',
      '#default_value' => 1373,
    '#required' => TRUE,
    '#title' => 'Choose all that apply',
    '#options' => $preference_options,
    '#prefix' => '<div id="preference-options">',
    '#suffix' => '</div>',
  );
役に立ちましたか?

解決

まず、fook_theme()でカスタムテーマ関数を定義し、#Themeを使用してフォーム要素に割り当てます。

そのテーマ関数では、使用できます Expand_Checkboxes 個別のチェックボックス要素の配列に変換します。次に、それぞれ3つの要素を持つ配列に再構築し、チェックボックスをレンダリングしてtheme_table()に渡します。

このようなもの、すべてがテストされていません。

function theme_yourmodule_preference($element) {
  $elements = element_children(expand_checkboxes($element));

  $rows = array();
  for ($i = 0; $i < count($elements); $i += 3) {
    $row = array(drupal_render($elements[$i]));
    // The following two might not always exist, check first.
    if (isset($elements[$i + 1]) {
      $row[] = drupal_render($elements[$i + 1]);
    }
    if (isset($elements[$i + 2]) {
      $row[] = drupal_render($elements[$i + 2]);
    }
    $rows[] = $row;
  }
  return theme('table', array(), $rows);
}

他のヒント

コラムとDrupal 7に表示するためにBerdirのソリューションを適応させました。

function yourmodule_theme() {
  return array
      (
      'form_yourmodule_form' => array
          (
          'render element' => 'form'
      ),
  );
}

function theme_form_yourmodule_form($variables) {

  $form = $variables['form'];

  $element = $form['checkboxelement'];
  unset($form['checkboxelement']);

  $elements = element_children(form_process_checkboxes($element));

  $colnr = 3;   // set nr of columns here

  $itemCount = count($elements);
  $rowCount = $itemCount / $colnr;
  if (!is_int($rowCount))
    $rowCount = round((($itemCount / $colnr) + 0.5), 0, PHP_ROUND_HALF_UP);
  $rows = array();
  for ($i = 0; $i < $rowCount; $i++) {
    $row = array();
    for ($col = 0; $col < $colnr; $col++) {
      if (isset($elements[$i + $rowCount * $col]))
        $row[] = drupal_render($element[$elements[$i + $rowCount * $col]]);
    }
    $rows[] = $row;
  }

  $variable = array(
      'header' => array(),
      'rows' => $rows,
      'attributes' => array('class' => 'checkbox_columns'),
      'caption' => NULL,
      'colgroups' => NULL,
      'sticky' => NULL,
      'empty' => NULL,
  );


  $output = theme_table($variable);

  $output .= drupal_render_children($form);

  return $output;
}

さらに簡単な解決策が見つかりました。これにはモジュールがあります!マルチカラムチェックボックスラジオ

ライセンス: CC-BY-SA帰属
所属していません drupal.stackexchange
scroll top