Skip to content

Instantly share code, notes, and snippets.

@bfodeke
Last active May 11, 2017 14:30
Show Gist options
  • Save bfodeke/d7ca9dbe91a18d7e9e919b548517af27 to your computer and use it in GitHub Desktop.
Save bfodeke/d7ca9dbe91a18d7e9e919b548517af27 to your computer and use it in GitHub Desktop.
After creating a field in the UI, get the export code to put in custom module.
<?php
// https://steindom.com/articles/exporting-and-creating-field-definitions-drupal-7
$entity_type = 'node';
$field_name = 'field_name';
$bundle_name = 'content_type';
$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_create_field(\n" . drupal_var_export($info_config) . "\n);\n\n";
$output .= "field_create_instance(\n" . drupal_var_export($info_instance) . "\n);";
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