开发者

drupal check if checkbox field is checked

I'm trying to see if a CCK checkbox field is chec开发者_开发问答ked.

Is it something like:

if ($node->field_checkbox[0]['value'] = 'checked')

?

Thanks.


You can easily check the contents of an object (e.g. $node) by installing the Devel module and using dsm($node).

In the case of a CCK checkbox, $node->field_fieldname contains an array with at least one element. Each element corresponds to each checked checkbox for the field, and the value key for the checkbox's element is set to the value you specified in the configuration for the field.

Otherwise, if the checkbox is unchecked, it will not appear as an element within $node->field_fieldname.

However, if there are no checkboxes checked, $node->field_fieldname will still contain one element, but the value key for that element will be unset/set to NULL.

So, let's say you had a field, field_checkbox, with two checkboxes: 1) Foo which has a value of foovalue, and 2) Bar which has a value of barvalue.

To check if Foo is checked, you might do:

foreach ($node->field_checkbox as $checkbox) {
  if ($checkbox['value'] == 'foovalue') {
     return TRUE;
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜