Drupal CCK Select Values to an Array
I really enjoy using CCK to define custom content types and it is usually does the trick all on its own. However, sometimes I need to get the field data out in some other way. This example retrieves CCK select options and returns them as an associative array, which can then be used in a custom form or elsewhere.
function _module_events_types_array() {
$query = "SELECT global_settings
FROM {content_node_field} AS cnt
WHERE cnt.field_name = 'field_meeting_type'";
$queryResult = db_query($query);
while ($meeting_types = db_fetch_object($queryResult)) {
$meetings = unserialize($meeting_types->global_settings);
}
$meeting_list = explode("\n", $meetings['allowed_values']);
$meeting_array = array("" => "All Meeting Types");
foreach($meeting_list as $meeting_type)
{
$meeting = explode("|", $meeting_type);
$meeting_array = array_merge($meeting_array,
array($meeting[0]=>$meeting[1]));
}
return $meeting_array;
}

February 4th, 2010 at 7:04 am
How can i print this somewhere on page?
February 4th, 2010 at 7:10 am
Or maybe better question; i want that users (with some role) can edit CCK custom content type field.
Example: i created CCK select field “Fruit”. It has some values: “Orange”, “Apple”,…
Now i want that user can edit fruit, or add some more fruit.
February 9th, 2010 at 2:46 pm
Adding this via code using the form api is pretty straightforward. If you haven’t built forms using code before, check out the Forms API Beginners Guide.
$form['meeting_type'] = array(
'#type' => 'select',
'#default_value' => '',
'#options' => _module_events_types_array()
);
There is also an option in the Drupal GUI in defining a CCK text field to load the select options from an array. I’ve never tried it that way though.
August 8th, 2010 at 3:17 pm
After writing my own function, and then finding the above code, it turns out CCK’s way of parsing allowed values into an array is significantly faster.
$field = content_fields(‘fieldNameHere’, ‘contentTypeNameHERE’);
$allowed_values = content_allowed_values($field);
In your example content_fields( ‘field_meeting_type’, ‘meeting’)
Thanks,
Jim