使用表单 api 添加属性到选项元素:drupal 7
我想为使用视图呈现的选择列表中的以下每个选项添加 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 & 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);
,而 ( (列表($key,$choice)=每个($choices)) && (列表($keytwo,$vtitle)=每个($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
I want to add title="icons/icon_cart.gif" for each of the below options in my select list which is rendered using views.
After trying and reading many articles I can't seem to find the way to add this html into my form.
Below is my code.
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" ?
}
My html code:
<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 & Gadgets</option>
</select>
Cheers,
Vishal
UPDATE
I tried to update the code as per Clive's advice but the values are still not coming up right. Below is my story.
So below is the html output I am able to achieve but the title always seems to be the number 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>
As you can see title is there but the value is wrong. Below is my form and the functions which I wrote.
My form:
$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.
);
My Theme functions.
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;
}
below is the correct code for the last theme function
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);
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
恐怕你必须深入挖掘才能做到这一点,
#options
数组在form_select_options()
目前不包含任何添加属性的方式。这是代码:正如您所看到的,其中根本没有属性的范围。不过,您将能够覆盖此设置,但这将涉及实现您自己的
theme_select()
和您自己的 FAPI 属性。我没有时间充实整个内容,但在您的主题文件中,您将执行以下操作:
请参阅 form_select_options 用于构造
MYTHEME_form_select_options
并且表单中的代码将是例如:
在
MYTHEME_form_select_options()
中,您可以检查元素的#extra_option_attributes
键(如果存在),看看是否需要在您创建的 HTML 中物理添加更多属性。希望有帮助,我知道这似乎是一种疯狂冗长的做你需要做的事情的方法,但据我所知这是唯一的方法。
I'm afraid you're going to have to dig quite far down to do this, the
#options
array is flattened inform_select_options()
and it doesn't currently include any way of adding attributes. This is the code:As you can see there's simply no scope for attributes in there. You will be able to override this though, but it will involve implementing your own version of
theme_select()
and your own FAPI property.I haven't got time to flesh the entire thing out but in your theme file you would be doing something like this:
Refer to form_select_options for constructing
MYTHEME_form_select_options
And your code in the form would be something like:
In
MYTHEME_form_select_options()
you can then inspect the element's#extra_option_attributes
key (if one exists) to see if you need to physically add any more attributes in the HTML you create.Hope that helps, I know it seems like a crazily long-winded way of doing what you need to but as far as I know it's the only way.
已测试
尝试:
而不是:
$form["tid"]
['#options']
[1]['#attributes'] = array('title' => t('nooo chatter'));
可以通过这种方式向元素添加任意属性。我根据 this 答案执行一些测试后发现了这一点。
还提到此处和此处。
Tested
Try:
instead of:
$form["tid"]
['#options']
[1]['#attributes'] = array('title' => t('nooo chatter'));
It is possible to add arbitrary attributes to elements this way. I discovered this after performing some tests based on this answer.
Also mentioned here and here.
未经测试,但您尝试过吗:
您可能需要弄乱元素的名称等,但 #attributes 标签应该可以工作。
编辑:正如克莱夫的回答所指出的,这行不通,但我会保留它,以防有人想知道如何向其他字段(文本框等)添加属性。
Untested, but have you tried:
You might need to mess around with the name of the element etc but the #attributes tag should work.
EDIT: As pointed out in Clive's answer, this won't work but I'll leave it up in case anyone wants to know how to add attributes to other fields (textboxes etc).