drupal - 使用表单 api : drupal 7 向选项元素添加属性

标签 drupal drupal-7 drupal-modules drupal-fapi

我要加 title="icons/icon_cart.gif"对于使用 View 呈现的选择列表中的以下每个选项。

在尝试并阅读了许多文章后,我似乎找不到将此 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" ?

}

我的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>

干杯,
维沙尔

更新
我尝试按照克莱夫的建议更新代码,但值仍然不正确。下面是我的故事。

所以下面是我能够实现的 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>

如您所见,标题在那里,但值是错误的。下面是我的表格和我写的函数。

我的表格:
$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.
);

我的主题功能。
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;
}

下面是最后一个主题功能的正确代码
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);

尽管
(
(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

最佳答案

恐怕你得挖得很深才能做到这一点,#options数组在 form_select_options() 中被展平它目前不包括任何添加属性的方式。这是代码:

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

如您所见,那里根本没有属性的范围。您将能够覆盖它,但这将涉及实现您自己的 theme_select() 版本。和您自己的 FAPI 属性(property)。

我没有时间充实整个事情,但在你的主题文件中你会做这样的事情:
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
}

引用 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",
  ) 
);

MYTHEME_form_select_options()然后你可以检查元素的 #extra_option_attributes键(如果存在)以查看您是否需要在您创建的 HTML 中物理添加更多属性。

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

关于drupal - 使用表单 api : drupal 7 向选项元素添加属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9261224/

相关文章:

php - 覆盖每个节点类型的 html.tpl.php 不起作用

tinymce - 限制 Drupal TinyMCE 上图像插入的选项

php - 我怎样才能得出这个观点?

Drupal 更新失败

linux - 如何保护 drupal 文件的权限?

mysql - 'record_date' 时间戳的默认值无效

Drupal 7 从 CKEditor 浏览媒体

css - Drupal 7 CSS 问题

php - 作为 View 结果的 drupal 7 内容类型字段

Drupal 8 添加 javascript 并在带有自定义模块的钩子(Hook)中传递数据