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

发布于 2025-01-05 20:52:30 字数 5238 浏览 5 评论 0原文

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

干杯, 维沙尔

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

所以下面是我能够实现的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

两个我 2025-01-12 20:52:30

恐怕你必须深入挖掘才能做到这一点,#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
}

请参阅 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 中物理添加更多属性。

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

I'm afraid you're going to have to dig quite far down to do this, the #options array is flattened in form_select_options() and it doesn't currently include any way of adding attributes. This is the code:

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

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:

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
}

Refer to form_select_options for constructing MYTHEME_form_select_options

And your code in the form would be something like:

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

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.

避讳 2025-01-12 20:52:30

已测试

尝试:

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

而不是:

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

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

还提到此处此处

Tested

Try:

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

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.

红ご颜醉 2025-01-12 20:52:30

未经测试,但您尝试过吗:

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

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

编辑:正如克莱夫的回答所指出的,这行不通,但我会保留它,以防有人想知道如何向其他字段(文本框等)添加属性。

Untested, but have you tried:

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

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文