私は多くの作業の後にこれを達成することができ、インターネットの奥深くにある別のサイトで見つけたスマートチップを使用してすべての投稿された方法を試しました:http : //e9p.net/altering-individual-radio-or-checkbox-items-drupal- 7-fapiは、#after_build
それらがdrupalレンダー配列になると、フォーム要素の個々のラジオを変更できるようにするために使用します。
各ラジオをクラスのあるコンテナーにラップしたかったので、それを使用#prefix
し#suffix
ました:
function _MYMODULE_options_after_build(&$element, &$form_state){
// Each renderable radio element.
foreach (element_children($element) as $key) {
$element[$key]['#prefix'] = '<div class="class1 class2">';
$element[$key]['#suffix'] = '</div>';
}
// Always return the element to render in after_build callbacks.
return $element;
}
使用例:
$form['style'] = array(
'#type' => 'radios',
'#title' => t('Select your style option'),
'#options' => $style_options,
'#default_value' => NULL,
'#required' => TRUE,
'#after_build' => array(
'_MYMODULE_options_after_build'
)
);
ただし、input
要素にクラスのみを含める場合は、https: //api.drupal.org/comment/60197#comment-60197のdrupal.orgに投稿したソリューションを実装して、#options_attributesを使用できるようにする必要があります。個々のオプションに対して正しく。ここにコードを再投稿:
function MYMODULE_element_info_alter(&$info) {
// You might want more advanced logic here, to replace instead of override altogether,
// in case other modules have already altered the core info.
$info['radios']['#process'] = array('safetycal_request_a_quote_process_radios');
}
function MYMODULE_process_radios($element) {
// for some reason when I take over processing the radios the structure
// is slightly different than with form_process_radios and it needs to be fixed
if(isset($element['element'])){
$element = $element['element'];
}
if (count($element ['#options']) > 0) {
$weight = 0;
foreach ($element ['#options'] as $key => $choice) {
// Maintain order of options as defined in #options, in case the element
// defines custom option sub-elements, but does not define all option
// sub-elements.
$weight += 0.001;
$element += array($key => array());
// Generate the parents as the autogenerator does, so we will have a
// unique id for each radio button.
$parents_for_id = array_merge($element ['#parents'], array($key));
$element [$key] += array(
'#type' => 'radio',
'#title' => $choice,
// The key is sanitized in drupal_attributes() during output from the
// theme function.
'#return_value' => $key,
// Use default or FALSE. A value of FALSE means that the radio button is
// not 'checked'.
'#default_value' => isset($element ['#default_value']) ? $element ['#default_value'] : FALSE,
// changed below line to use the #options_attributes array
'#attributes' => $element['#option_attributes'][$key],
'#parents' => $element ['#parents'],
'#id' => drupal_html_id('edit-' . implode('-', $parents_for_id)),
'#ajax' => isset($element ['#ajax']) ? $element ['#ajax'] : NULL,
'#weight' => $weight,
);
}
}
return $element;
}
使用例:
$style_options = array(
'red' => 'Red',
'green' => 'Green',
'yellow' => 'Yellow'
);
$style_option_attributes = array(
'red' => array(
'class' => array(
'red-class'
)
),
'green' => array(
'class' => array(
'green-class'
)
),
'yellow' => array(
'class' => array(
'yellow-class'
)
)
);
$form['style'] = array(
'#type' => 'radios',
'#title' => t('Select your style option'),
'#options' => $style_options,
'#option_attributes' => $style_option_attributes,
'#default_value' => NULL,
'#required' => TRUE,
'#attributes' => array(
'class' => array(
'radio-element-class'
)
)
);