Skip to main content

Snippet that prints all fields, bundles and entity-types

Published on 2016-05-25

Recently we needed an overview of all fields in a Drupal 7 website. This snippet helps to print that to the screen (and printer). First install the Devel module. (don't do this on a production website). In your site go to /devel/php and than paste to following snippet.

# print drupal 7 fields to screen
$list_item_prefix = "- "; // You can change this if you like.

$field_info = field_info_instances(); // Ask d7 for all fields.

// Print list of entity-types first.
print "Entity types:" . PHP_EOL;
foreach ($field_info as $entity_type => $bundles) {
  print $list_item_prefix . $entity_type . PHP_EOL;
}
print PHP_EOL;

// Print fields per bundle.
print "Fields per bundle:" . PHP_EOL;
foreach ($field_info as $entity_type => $bundles) {
  print "---------------------------------------------------------------------" . PHP_EOL;
  print "entity type: " . $entity_type . PHP_EOL;
  print "---------------------------------------------------------------------" . PHP_EOL;
  print PHP_EOL;
  foreach($bundles as $bundle => $fields) {
    print "bundle: " . $entity_type . ":" . $bundle; print PHP_EOL;
    print "fields:" . PHP_EOL;
    foreach($fields as $field => $settings){
      print $list_item_prefix . $field;print PHP_EOL;
    }
    print PHP_EOL;
  }
  print PHP_EOL;
}

The result looks something like this:

---------------------------------------------------------------------
entity type: node
---------------------------------------------------------------------

bundle: node:article
fields:
- field_product_author
- field_product_categories
- field_product_description