Skip to content

Instantly share code, notes, and snippets.

@MatthieuScarset
Last active August 29, 2015 14:26
Show Gist options
  • Save MatthieuScarset/7cbdab8b99c84451c858 to your computer and use it in GitHub Desktop.
Save MatthieuScarset/7cbdab8b99c84451c858 to your computer and use it in GitHub Desktop.
Drupal - Export $field and $instance
// Export fields and their instances
// for Drupal module development
// @see http://steindom.com/articles/exporting-and-creating-field-definitions-drupal-7
$entity_type = 'node';
$field_name = 'field_gallery_user_lname';
$bundle_name = 'gallery';
$info_config = field_info_field($field_name);
$info_instance = field_info_instance($entity_type, $field_name, $bundle_name);
unset($info_config['id']);
unset($info_instance['id'], $info_instance['field_id']);
include_once DRUPAL_ROOT . '/includes/utility.inc';
$output = "\$field = array(" . drupal_var_export($info_config) . ");\n";
$output .= "\$instance = array(" . drupal_var_export($info_instance) . "):\n";
$output .= "field_create_field(\$field);\n";
$output .= "field_create_instance(\$instance);";
drupal_set_message("<textarea rows=30 style=\"width: 100%;\">" . $output . '</textarea>');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment