in Web & Tech

Deleting Drupal Fields programmatically implementing hook_update_N()

Say we’d like to get rid of a Drupal Field that has the machine name field_wein_texte and our module that’s supposed to manage this is called kx_deployment. Here’s how the kx_deployment.install needs to look like:

<?php
/**
* Implements hook_update_N().
* Managing Field Deletions programmatically
* Note: field_delete_fields() removes fields globally, to detach a field from an entity use field_delete_instance()
*/
function kx_deployment_update_7001() {
  $fields_to_delete = array(
    'field_wein_texte',
  );
  foreach ($fields_to_delete as $field_name) {
    field_delete_field($field_name);
    watchdog('kx_deployment', '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.
if(isset($fields_to_delete)) {
  field_purge_batch(count($fields_to_delete) + 5);
}
?>

Source: http://data.agaric.com/deleting-drupal-fields-programmatically-so-change-field-type-features

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

To respond on your own website, enter the URL of your response which should contain a link to this post's permalink URL. Your response will then appear (possibly after moderation) on this page. Want to update or remove your response? Update or delete your post and re-enter your post's URL again. (Learn More)