Created
July 31, 2013 01:00
-
-
Save johnlaine1/6118469 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* This file will demonstrate a method to export fields to code. | |
* You can use this to easily create fields using the UI, export to code | |
* and then use in a custom module. Upon installation of the module | |
* your fields and instances will already be set up. | |
*/ | |
// Create the fields you want using the Drupal UI. | |
// On the same site, go to example.com/devel/php | |
// Paste the following code into the "PHP code to execute" textbox. | |
// Set the first 3 variables and then click execute. | |
//============================================================================== | |
// Set the 3 variables below. | |
$entity_type = 'node'; | |
$field_name = 'body'; | |
$bundle_name = 'article'; | |
$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 = "\$fields['" . $field_name . "'] = " . drupal_var_export($info_config) . ";\n"; | |
$output .= "\$instances['" . $field_name . "'] = " . drupal_var_export($info_instance) . ";"; | |
drupal_set_message("<textarea rows=30 style=\"width: 100%;\">" . $output . '</textarea>'); | |
//============================================================================== | |
// You will get 2 arrays, something like this, but a lot longer. | |
$fields['field_some_field'] = array( | |
'properties of the field' | |
); | |
$instances['field_some_field'] = array( | |
'properties of the instance' | |
); | |
// Add the following to your .install file. | |
// Replace all instances of mymodule with actual module name. | |
// Paste the code from the devel ouput into the _mymodule_field_data and | |
// _mymodule_instance_data, as noted in the respective functions below. | |
// You can do this for as many fields as you like, just put all the $fields | |
// arrays in the _mymodule_field_data function and all the $instances in the | |
// _mymodule_instance_data function. | |
//============================================================================== | |
/** | |
* Implements hook_install(). | |
* Perform module setup tasks. | |
*/ | |
function mymodule_install() { | |
// Create all the fields we are adding to our entity type. | |
// http://api.drupal.org/api/function/field_create_field/7 | |
foreach (_mymodule_field_data() as $field) { | |
field_create_field($field); | |
} | |
// Create all the instances for our fields. | |
// http://api.drupal.org/api/function/field_create_instance/7 | |
foreach (_mymodule_instance_data() as $instance) { | |
field_create_instance($instance); | |
} | |
} | |
// Create the array of information about the fields we want to create. | |
function _mymodule_field_data() { | |
$fields = array(); | |
// Paste $fields data from devel ouput here. | |
return $fields; | |
} | |
// Create the array of information about the instances we want to create. | |
function _mymodule_instance_data() { | |
$instances = array(); | |
// Paste $instances data from devel output here. | |
return $instances; | |
} | |
//============================================================================== | |
// And if you want to uninstall the fields, instances and all the data contained | |
// within, when the module is uninstalled, add the following code. I'm not sure | |
// what the best practice is on this one, since instances of a field created | |
// by a custom module could be used to created instances of the field on other | |
// entity types. If you uninstall this module you would lose all information. | |
// Currently I am only using this for development purposes and will probably not | |
// include it in production. | |
//============================================================================== | |
/** | |
* Implements hook_uninstall(). | |
* Remove any information that the module sets. | |
*/ | |
function mymodule_uninstall() { | |
foreach (_mymodule_field_data() as $field_name => $properties) { | |
field_delete_field($field_name); | |
} | |
field_purge_batch(1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment