使用表单api:drupal 7向option元素添加属性

Vis*_*ani 7 drupal drupal-7 drupal-fapi drupal-modules

我想为使用视图渲染的选择列表中的以下每个选项添加title ="icons/icon_cart.gif".

在尝试阅读了很多文章后,我似乎找不到将这个html添加到我的表单中的方法.

以下是我的代码.

function customchatter_form_alter(&$form, &$form_state, $form_id) {

$form["tid"]["#options"][1]=t("nooo chatter");
// this works to change the label of the option but how do I add title="icons/icon-  
cart.gif" ?

}
Run Code Online (Sandbox Code Playgroud)

我的HTML代码:

<select id="edit-tid" name="tid" class="form-select">
<option value="All">- Any -</option>
<option value="1">nooo chatter</option>
<option value="2">Complaints Complaints</option>
<option value="3">Gear &amp; Gadgets</option>
</select>
Run Code Online (Sandbox Code Playgroud)

干杯,维沙尔

更新 我尝试根据Clive的建议更新代码,但值仍未正确.以下是我的故事.

所以下面是我能够实现的html输出,但标题似乎总是数字1.

<select id="edit-select" class="form-select" name="select">
<option value="1" title="1">One</option>
<option value="2" title="1">Two</option>
</select>
Run Code Online (Sandbox Code Playgroud)

你可以看到标题存在,但价值是错误的.下面是我的表格和我写的功能.

我的表格:

$form['select'] = array(
'#type' => 'select',
'#options' => array(1 => 'One', 2 => 'Two'),
'#title' => array(1 => 'One', 2 => 'Two') 
// I tried many combinations but nothing seems to work.
);
Run Code Online (Sandbox Code Playgroud)

我的主题功能.

function kt_vusers_select($variables) {
$element = $variables['element'];
element_set_attributes($element, array('id', 'name', 'size'));
_form_set_class($element, array('form-select'));

return '<select' . drupal_attributes($element['#attributes']) . '>' .    
kt_vusers_form_select_options($element) . '</select>';
}


function kt_vusers_form_select_options($element, $choices = NULL) {

if (!isset($choices)) {
$choices = $element['#options'];
}
// array_key_exists() accommodates the rare event where $element['#value'] is NULL.
// isset() fails in this situation.
$value_valid = isset($element['#value']) || array_key_exists('#value', $element);


// @vishal  so there I have declared the variable to accept the values.
$vtitle = isset($element['#title']) || array_key_exists('#title', $element);


$value_is_array = $value_valid && is_array($element['#value']);
$options = '';
foreach ($choices as $key => $choice) {
if (is_array($choice)) {
  $options .= '<optgroup label="' . $key . '">';
  $options .= form_select_options($element, $choice);
  $options .= '</optgroup>';
}
elseif (is_object($choice)) {
  $options .= form_select_options($element, $choice->option);
}
else {
  $key = (string) $key;
  if ($value_valid && (!$value_is_array && (string) $element['#value'] === $key || 
($value_is_array && in_array($key, $element['#value'])))) {
    $selected = ' selected="selected"';
  }
  else {
    $selected = '';
  }

 // @vishal this is where the variable is being used.

 $options .= '<option title="'.$vtitle.'" value="' . check_plain($key) . '"' . $selected . 
 '>' . check_plain($choice) . '</option>';
}
}
return $options;
}
Run Code Online (Sandbox Code Playgroud)

下面是最后一个主题函数的正确代码

function kt_vusers_form_select_options($element, $choices = NULL, $vtitles=NULL) {
// Build up your own version of form_select_options here
// that takes into account your extra attribute needs.
// This will probably involve inspecting your custom FAPI property,
// which we'll call #extra_option_attributes

if (!isset($choices)) {
$choices = $element['#options'];
$vtitles = array();
$vtitles = $element['#title'];
}
// array_key_exists() accommodates the rare event where $element['#value'] is NULL.
// isset() fails in this situation.
$value_valid = isset($element['#value']) || array_key_exists('#value', $element);

$value_is_array = $value_valid && is_array($element['#value']);
$options = '';

//  print_r($vtitles);
Run Code Online (Sandbox Code Playgroud)

while((list($ key,$ choice)= each($ choices))&&(list($ keytwo,$ vtitle)= each($ vtitles))){// printf("%s =>%s,% s =>%s \n",$ key1,$ value1,$ key2,$ value2);

 if (is_array($choice)) {
    $options .= '<optgroup label="' . $key . '">';
    $options .= kt_vusers_form_select_options($element, $choice);
    $i++;
    // $options .= form_select_options($element, $vtitle);
    $options .= '</optgroup>';
    } // end if if is_array

 elseif(is_object($choice)) {
  $options .= form_select_options($element, $choice->option);
  } // end of else if



else {
      $key = (string) $key;
  if ($value_valid && (!$value_is_array && (string) $element['#value'] === $key ||  
($value_is_array       && in_array($key, $element['#value'])))) {
    $selected = ' selected="selected"';
    }
  else {
    $selected = '';
  }
  // $options .= '<option title="'.$vtitle.'" value="' . check_plain($key) . '"' . 
  $selected . '>' . check_plain($choice) . '</option>';


 }

 $options .= '<option value="'. check_plain($key) .'" title="' . $vtitle . '"' . $selected 
 .'>'. check_plain($choice) .'</option>';



 } // end of choice



return $options;


} // end of function
Run Code Online (Sandbox Code Playgroud)

Cli*_*ive 13

我担心你将不得不深入挖掘这一点,#options数组被压平form_select_options()并且它目前不包含添加属性的任何方式.这是代码:

$options .= '<option value="' . check_plain($key) . '"' . $selected . '>' . check_plain($choice) . '</option>';
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,那里没有属性的范围.您可以覆盖它,但它将涉及实现您自己的版本theme_select()和您自己的FAPI属性.

我没有时间把整个事情搞得一团糟但是在你的主题文件中你会做这样的事情:

function MYTHEME_select($variables) {
  $element = $variables['element'];
  element_set_attributes($element, array('id', 'name', 'size'));
  _form_set_class($element, array('form-select'));

  return '<select' . drupal_attributes($element['#attributes']) . '>' . MYTHEME_form_select_options($element) . '</select>';
}


function MYTHEME_form_select_options($element) {
  // Build up your own version of form_select_options here
  // that takes into account your extra attribute needs.
  // This will probably involve inspecting your custom FAPI property,
  // which we'll call #extra_option_attributes
}
Run Code Online (Sandbox Code Playgroud)

请参阅form_select_options进行构建MYTHEME_form_select_options

并且您在表单中的代码将类似于:

$form['select'] = array(
  '#type' => 'select',
  '#options' => array(1 => 'One', 2 => 'Two'),
  '#extra_option_attributes' => array(
    1 => array('title' => 'Test title'), // Attributes for option with key "1",
    2 => array('title' => 'Test title'), // Attributes for option with key "2",
  ) 
);
Run Code Online (Sandbox Code Playgroud)

MYTHEME_form_select_options()你就可以检查元素的#extra_option_attributes键(如果存在的话),看看你需要在你创建HTML身体增加任何更多的属性.

希望有所帮助,我知道这似乎是一种疯狂啰嗦的方式来做你需要的事情,但据我所知,这是唯一的方法.