Error message

The spam filter installed on this site is currently unavailable. Per site policy, we are unable to accept new submissions until that problem is resolved. Please try resubmitting the form in a couple of minutes.

Return Drupal CCK Integer Field Allowed Values List as Array

Category: 

Drupal 6 (D7 below)

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

function tf_cck_integer_field_allowed_values_list_as_array ($field_name) {
	$sql = " SELECT global_settings FROM content_node_field WHERE field_name = '%s' AND type = 'number_integer' ";
	$global_settings = unserialize(db_result(db_query($sql,$field_name)));
	$allowed_values = $global_settings['allowed_values'];
	$allowed_values_explode = explode("\n",$allowed_values);
	$allowed_values = array();
	foreach ($allowed_values_explode as $allowed_value) {
		$allowed_value_explode = explode("|",$allowed_value);
		$allowed_values[$allowed_value_explode[0]] = trim($allowed_value_explode[1]);
	}
	return $allowed_values;
}

EXAMPLE USAGE:

$field_name = "field_dg_hole_tee";
$tees = tf_cck_integer_field_allowed_values_list_as_array($field_name);
drupal_set_message(print_r($tees,true));

OUTPUT:

Drupal 7

Thanks to Laurens from Rotterdam in The Netherlands for this Drupal 7 snippet!

 

function get_list_integer_allowed_values_as_object() {
   $data               = array();
   $result             = db_query("SELECT field_name , data FROM {field_config} WHERE type='list_integer'");
   foreach ($result as $record) {
     $tempvar  = unserialize($record->data);
     $data[$record->field_name] = $tempvar['settings']['allowed_values'];
   }
 return $data;
}

It will return something like this:

array(15) {

 ["field_objecttype"]=>

 array(7) {

   [1]=>

   string(9) "Penthouse"

   [2]=>

   string(9) "Apartment"

   [3]=>

   string(6) "Studio"

 }

 ["field_furnished"]=>

 array(4) {

   [0]=>

   string(3) "Yes"

   [1]=>

   string(2) "No"

 }

}

Add new comment