User login

Deleting Drupal Fields Programmatically (So As to Change Field Type with Features)

In brief: The code at the bottom of this post deletes the named fields. If you are changing the field type for one or more fields, put it in an update hook for a feature module and run update.php before running features revert, your feature will happily rebuild the fields with their new settings. This destroys data.

FieldException: Cannot change an existing field's type. in field_update_field() (line 230 of /home/ben/code/sdl/web/modules/field/field.crud.inc).

Cannot change an existing field's type in field_update_field()
http://drupal.org/node/1201898

Not sure this would even help us:
Features install/revert does not remove additional fields from content type already stored in the db
http://drupal.org/node/649298

drush field-delete name_of_field
would work...

run drush from update.php
run drush commands in update hooks

Cannot do Drush commands from an update hook, though! (It's actually not a good idea at all)

http://drupal.foxinbox.org/drupal/code-snippits/remove-fieldsgroup-code

[gotcha] field_purge_batch() needs to run after fields are deleted, or they aren't really deleted. (You would think a function called field_delete_field() would sort of, well, do that- and it does delete the database tables - or rather, rename them just to keep the data around - but it does not remove the field definition from entity information.) [/gotcha]

<?php
/**
 * Implements hook_update_N().
 *
 * We need to delete our numeric fields before we can revert the feature and
 * replace them with text fields.
 *
 * Based on 6.x-era code from Fox,
 * <a href="http://drupal.foxinbox.org/drupal/code-snippits/remove-fieldsgroup-code
" title="http://drupal.foxinbox.org/drupal/code-snippits/remove-fieldsgroup-code
">http://drupal.foxinbox.org/drupal/code-snippits/remove-fieldsgroup-code
</a> */
function feature_projects_update_7001() {
 
$fields_to_delete = array(
   
'field_cost',
   
'field_construction_budget',
   
'field_construction_cost',
   
'field_total_budget',
  );
  foreach (
$fields_to_delete as $field_name) {
   
field_delete_field($field_name);
   
watchdog('feature_projects', 'Deleted the :field_name field from all content type instances.', array(':field_name' => $field_name));
  }
}

 

// The fields aren't really deleted until the purge function runs, ordinarily
  // during cron.  Count the number of fields we need to purge, and add five in
  // case a few other miscellaneous fields are in there somehow.
 
field_purge_batch(count($fields_to_delete) + 5);
?>
Searched words: 
drupal features change field type

Comments

batch size in field_purge_batch

Thanks for the writeup. Just one thing: $batch_size in field_purge_batch() is definded as: The maximum number of field data records to purge before returning.

In your example 4+5 is very low. You may have hundreds of entities with field data records for the specified field ... at least the comment is misleading.

Just a little errorjust

Just a little error

just include field_purge_batch(count($fields_to_delete) + 5) in the function

<?php
function feature_projects_update_7001() {
 
$fields_to_delete = array(
   
'field_cost',
   
'field_construction_budget',
   
'field_construction_cost',
   
'field_total_budget',
  );
  foreach (
$fields_to_delete as $field_name) {
   
field_delete_field($field_name);
   
watchdog('feature_projects', 'Deleted the :field_name field from all content type instances.', array(':field_name' => $field_name));
  }
 
// The fields aren't really deleted until the purge function runs, ordinarily
  // during cron.  Count the number of fields we need to purge, and add five in
  // case a few other miscellaneous fields are in there somehow.
 
field_purge_batch(count($fields_to_delete) + 5);
}
?>

;)

Your code on Dropbucket

Thanks for the tips, I put your code on Dropbucket :
http://dropbucket.org/node/668

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <blockquote> <small> <h2> <h3> <h4> <h5> <h6> <sub> <sup> <p> <br> <strike> <table> <tr> <td> <thead> <th> <tbody> <tt> <output>
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.