Hi Drupies... What a great community, what a great tool...

I am new in Drupal. I am building a module that measures time on specific tasks. I need to get a list of all fields in a specific content type. Any ideas where to start?

Thanks
Patrick

Comments

nevets’s picture

You can use something like

		$fields = content_types($type);

		if ( empty($fields) || empty($fields['fields']) ) {
			return;
		}
		
		foreach ( $fields['fields'] as $field ) {
		  ...
		}
		
parallax’s picture

thanks nevet, that was exactly what i was looking for!!!

pjsz’s picture

The previous content_fields($type) didn't work for me. It returned all the cck fields. In drupal 6, i've found to get content_fields for just one content type you need to do something like:

$type = 'mytype';
$fields = content_fields();
$type_fields = array(); 
foreach ($fields as $field_name => $field_data)
{
  if ($field_data['type_name'] == $type)
  { 
    $type_fields[$field_name] = $field_data;
  } 
}

// something like that. Or you could unset $fields   you didn't want.
Vlad Vinnikov’s picture

content_fields() is now (Drupal 7) field_info_fields();
http://api.drupal.org/api/drupal/modules--field--field.info.inc/function...

tooFATforBUTTer’s picture

I have several fields that are shared across content types and those fields can't be found using this method. For whatever reason,

content_fields()

returns a string of only the 1st content type I associated the field(s) with instead of an array of all the types it's associated with. I'm using CCK 6.x-2.9.

My original need to collect all this data was so that I could dynamically build a query that pulled all the fields' info together. These fields are for nodes that users grade other content with. They are basically "score card" nodes, so the values in these fields need added to other node values. Views sounded like it was going to be too heavy for my needs because I would still have to manually add the values together and calculate averages.

At any rate, I started with (and most likely will go with) manually collecting the fields through the 'node_field_instance_table'.

<?php
/**
*  Don't prepend with 'content_type_', just list the core database name of the content type
*  as shown on (/admin/content/types/list)
*/
$content_type = 'my_content_type';

$sql_field_type_collector = "
    SELECT `field_name`
    FROM `content_node_field_instance` nfi
    WHERE nfi.`type_name` = '%s'
  ";
  
  $sql_field_type_collector_result = db_query($sql_field_type_collector, $content_type);
  while ($field = db_fetch_array($sql_field_type_collector_result)){
    $fields[] = $field['field_name'];
  };
  array_pop($fields);
?>

You could make a foreach loop that goes through $field_names and runs content_fields(); on each one:

<?php
  foreach ($fields as $field){
    $type_fields[$field] = content_fields($field);
  }
?>
alexmoreno’s picture

this worked perfectly, thank you pjsz

glass.dimly’s picture

For D6, one of the above comments only captures the single hosting content type for the field, not all instances field on a content type. So this code does not work to list all content types on which a field can be found, only the "root" field.

On D6, the following will give you a list of all content types, with fields listed as child arrays:

dpm(content_types());