Find all fields stored in the database (not in code).

Public

Features install/revert does not remove additional fields from content type already stored in the db. So sometimes you have to remove these fields manually.

Get raw version
php
  1. <?php
  2.  
  3. /**
  4.  * @file
  5.  * Drush command to find all abandoned fields.
  6.  *
  7.  * @see http://drupal.org/node/649298
  8.  */
  9.  
  10. /**
  11.  * Implements hook_drush_command().
  12.  */
  13. function abandoned_fields_drush_command() {
  14.  
  15. $items['abandoned-fields'] = array(
  16. 'description' => 'Find all abandoned fields',
  17. 'drupal dependencies' => array('features'),
  18. 'aliases' => array('af'),
  19. );
  20.  
  21. return $items;
  22. }
  23.  
  24. /**
  25.  * Drush command callback;
  26.  */
  27. function drush_abandoned_fields() {
  28. module_load_include('inc', 'features', 'features.export');
  29.  
  30. $commited_fields = array();
  31. foreach (features_get_default('field') as $id => $field) {
  32. if (!in_array($field['field_config']['field_name'], $commited_fields)) {
  33. $commited_fields[] = $field['field_config']['field_name'];
  34. }
  35. }
  36.  
  37. $rows[] = array('Field name', 'Location');
  38. $rows[] = array('-------------', '----------');
  39. foreach (field_info_fields() as $field_name => $field) {
  40. if (!in_array($field_name, $commited_fields)) {
  41. $bundle_strs = array();
  42. foreach ($field['bundles'] as $entity_type => $bundles) {
  43. $bundle_strs[] = $entity_type . ' (' . implode(',', $bundles) . ')';
  44. }
  45. $rows[]= array(
  46. $field_name,
  47. implode(' ', $bundle_strs),
  48. );
  49. }
  50. }
  51. drush_print_table($rows);
  52. }

Comments

Eric B's picture

This looks like it would solve my issue of fields in a feature that I removed from code but they are still showing on my Acquia site. Can you give further info on where this code would be place, and how to run it? I'm fairly new to Drupal and am not clear on how to use what you have written.

THanks