In Drupal 7 you can use field_info_field(), while in Drupal 6 you can
use content_field_instance_read(), which is defined in the CCK module.
欢迎各位兄弟 发布技术文章
这里的技术是共享的
d6 属性值 我用的就是这种方法
$content_field = content_fields('field_make'); // field_make是字段的英文名
$allowed_values = content_allowed_values($content_field);
//这里得到是结果是一个数组 数组的键为英文 值为中文(对于中国人来说)
//(也就是cck字段
field_make 的 select 定义的 英文|中文 的允许值列表 组成的数组)
How can I get all options for a cck field?
For example, There is a field with 3 options in allowed values, like option1, option2, option3.Is there any function which receive the field name and returns option1 to option3?
正确答案 d7 属性值
$field = field_info_field('field_name');
$allowed_values = list_allowed_values($field);
In Drupal 7 you can use field_info_field(), while in Drupal 6 you can use content_field_instance_read(), which is defined in the CCK module.
| ||||
来自 http://drupal.stackexchange.com/questions/6747/how-to-get-all-options-of-a-select-field
When programmatically creating Drupal 6.0 nodes incorporating CCK fields it may be useful to know the allowed values for the field prior to submitting your form with drupal_execute().
NOTE: the listed function is otiose. CCK (aka the content module) supplies the required functionality already.
$content_field = content_fields('field_make');
$allowed_values = content_allowed_values($content_field);
我用的就是这种方法
The following function returns an array of the allowed values for a CCK field.
/**
* Get array of allowed values for a cck field
*/
function get_cck_field_allowed_values($field_name)
{
// read the field
module_load_include('inc', 'Content', 'includes/content.crud');
$fields = content_field_instance_read(array(field_name => $field_name));
// explode the allowed values
$allowed_string = $fields[0]['allowed_values']; // string with \r\n separators
$allowed_values = explode("\r\n", $allowed_string);
// return keys if present
$key_values = array();
foreach ($allowed_values as $allowed_value) {
$pos = strpos($allowed_value, '|', 0);
if ($pos !== false) {
$allowed_value = substr($allowed_value, 0, $pos);
}
$key_values[] = $allowed_value; // add to array
}
// take a peek using the developer module
//dvm($key_values, 'Allowed values for field '.$field_name);
return $key_values;
}
2 | I have a custom content type which has a field called "location" which is a long select list (100 or so items). I want to get an array of all locations which have a piece of content associated with them. The numeric value of this field is stored in content_type_[my_content_type], but I can't find anywhere in the database where the name of the values are stored. I hope that's not too confusing - just to be clear, I want to do something like this:
and then
and then do something with the two arrays to find the names I want. Can anyone help? Thanks a lot. Drupal 6, by the way. |
If you mean select list field of CCK:
Get all values of that field (defined in "Allowed values"):
|
来自 http://stackoverflow.com/questions/5004631/drupal-cck-field-select-option-names-where-are-they
Below is a helpful function that will return the allowed values list for a Drupal CCK Integer Field as an Array.
For example, here is my allowed values list for my cck integer field "field_dg_hole_tee":
CODE:
EXAMPLE USAGE:
OUTPUT:
Drupal 7
Thanks to Laurens from Rotterdam in The Netherlands for this Drupal 7 snippet!
It will return something like this:
3
2
| Given the following select list for the "field_priority" field, how can I display the label, given the key (e.g. 0, 1, 3)?
| |||
add a comment
|
8
| You can get it nice and quickly using
There's a whole host of field functions in field.info.inc, they're very handy. |
来自 http://stackoverflow.com/questions/7731139/display-label-given-key-for-select-list
Comments
Comment#1
apmsooner CreditAttribution: apmsooner commentedFigured it out. Added answer here in snippets: https://www.drupal.org/node/2307109
来自 https://www.drupal.org/node/2307093