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);
?>More like this
- Delete a content type programmatically in Drupal 7
- Deleting the last content type containing a field deletes that field also
- Once saved, the machine name of a content type should not automatically change when the display name is changed
- Select multiple users to delete sorted by registration date and with one profile field (Drupal 4.7)
- WYSIWYG module gotcha: after upgrading or changing editors, delete profiles to resume


Comments
Post new comment