【问题标题】:Adding attribute to option element using forms api : drupal 7使用表单 api 向选项元素添加属性:drupal 7
【发布时间】:2012-02-13 13:17:45
【问题描述】:

我想在使用视图呈现的选择列表中为以下每个选项添加 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" ?

}

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

干杯, 维沙尔

更新 我尝试按照 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>

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

我的表格:

$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) = 每个($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

【问题讨论】:

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


【解决方案1】:

恐怕你必须深入挖掘才能做到这一点,#options 数组在form_select_options() 中是扁平的,它目前不包含任何添加属性的方式。这是代码:

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

如您所见,其中根本没有属性的范围。不过,您将能够覆盖它,但这将涉及实现您自己的 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
}

MYTHEME_form_select_options的构造参考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 中物理添加更多属性。

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

【讨论】:

  • 你赢了这一轮@Clive。你赢了这一轮。 +1 努力。
  • clive 我已经更新了帖子,我可以在 html 中获得标题,但值似乎总是一个。
  • 在我的情况下,我在 $element = $variables['element']; 上收到错误“Undefined index: render element in theme()”;在 theme.inc 文件中。
  • 该死的郁闷...主题解决方案总是让人觉得很脏。哦,好吧。
  • 感谢@Clive 的启发。这是我对复选框做同样事情的方法:drupal.stackexchange.com/questions/89226/…
【解决方案2】:

测试

试试:

$form["tid"][1]['#attributes'] = array('title' => t('nooo chatter'));

代替:

$form["tid"]['#options'][1]['#attributes'] = array('title' =&gt; t('nooo chatter'));

可以通过这种方式向元素添加任意属性。我是在根据this 答案进行一些测试后发现的。

还提到了herehere

【讨论】:

  • 这对我不起作用,它将它们添加到选项数组之外,因此没有为每个元素呈现任何内容。
【解决方案3】:

未经测试,但您是否尝试过:

$form["tid"]["#options"][1]['#attributes'] = array('title' => t('YOUR NEW TITLE'));

您可能需要弄乱元素的名称等,但#attributes 标签应该可以工作。

编辑: 正如 Clive 的回答中指出的那样,这行不通,但如果有人想知道如何将属性添加到其他字段(文本框等),我会保留它。

【讨论】:

  • +1,因为我认为对善意的回答有否定的代表是不公平的!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多