Hi all,

after a lot of searching I still don't know how I can get all fields for a specific content-type.
Any help would be appreciated.

eR

Comments

Jaypan’s picture

Gonna need to be more specific than that.

enochRoot’s picture

I'll try:
Every Content-type can have certain fields, right? The ones I can manage by clicking 'manage fields' right next to the content-type under admin/structure/types.
Now I just want to get that list of available fields for that content-type in code.

probably still not specific enough...

nevets’s picture

Sounds like you are looking for field_info_instances()

enochRoot’s picture

The description indeed looks like that's what I need. Can I use the content-type (node-type) as the $entity_type parameter?

thx,
eR

nevets’s picture

entity type would be 'node' and the bundle would be the machine name for the content type.

alexmoreno’s picture

in Drupal6 this does the trick:

http://drupal.org/node/1002634#comment-4575544

farfanfelipe’s picture

This is an example for Drupal 7

$options = array('target' => 'slave');
  
  $content_type = 'contests';

  $sql = "SELECT field_name
          FROM field_config_instance ci 
          WHERE ci.bundle = :content_type ";
      
  $result = db_query($sql, array(':content_type' => $content_type,), $options);
  $fields = array();
  foreach ($result->fetchAll() as $key => $field) {
    $fields[] = $field->field_name;
  }
alexmoreno’s picture

well, more elegant using the api:

$fields = content_types($type);

		if ( empty($fields) || empty($fields['fields']) ) {
			return;
		}

		foreach ( $fields['fields'] as $field ) {
		  ...
		}
skorzh’s picture

$fields = field_info_instances("node", "my_content_type");

glass.dimly’s picture

Some of that code is for D7. field_info_instances is only on D7.

Here's my D6 code:


/**
 * Get a list of content types with all fields on them for export to spreadsheet.
 */
function cc_migrate_info_get_fields_by_content_type() {
  foreach (content_types() as $content_type_name => $content_type) {
    print_r("\n\n{$content_type['name']} ({$content_type_name})\nFields:\n");
    foreach ($content_type['fields'] as $field_name => $field){
      print_r("\nMachine Name: {$field_name}\n");
      print_r("Type: {$field['type']}\n");
      print_r("Human Name: {$field['widget']['label']}\n");
      print_r("Description: {$field['widget']['description']}\n");
    }
  }
}

/**
 * Get a list of content types with descriptions
 */
function cc_migrate_info_get_content_types() {
  foreach (content_types() as $content_type_name => $content_type) {
    print_r("\n{$content_type['name']} ({$content_type_name})\n");
    $description = strip_tags($content_type['description']);
    print_r("Description: {$description}\n");
  }