We have a situation where in different environments we have to manually delete fields. Rather than manually doing that, we want to programmatically deploy the changes. The idea that is being is discussed is to use a custom module which calls the hook_field_delete_instance() hook.
The issue we are having is how to best pass in the content type machine name and the machine name for a single content type. We are understanding that when we reused the field on a separate content type, we are essentially creating an alias on the current content type that points to the original content type holding the field name.
Using the situation below, what is the best way to delete the two fields on the on the basic page only using the hook_field_delete_instance() on a custom module?
Situation:
We have two content types: article and basic page. We created several fields on the article content type that we ended up reusing on the basic page.
Article:
- field_art_original_publish_date
- field_art_country_tag
- field_art_image
Basic Page:
- field_art_image (reused from article content type)
- field_art_country_tag (reused from article content type)
Update:
So now that i am thinking further on this, i would like 1 hook update for 1 field deletion so that if a hook update fails then i know which fields need attention.
Here is what i have come up with using what @Clive has suggested.
function mymodule_update_7001(&$sandox) {
mymodule_delete_field('field_art_image', 'page' );
}
function mymodule_update_7002(&$sandox) {
mymodule_delete_field('field_art_country_tag', 'page');
}
function mymodule_delete_field($field_mn, $ct_mn) {
$entity_type = "node";
$field_instance = field_info_instances($entity_type, $field_mn, $ct_mn);
field_delete_instance($field_instance);
//setting the batch size to a large number resolves my prior issues
$batch_size = 20000;
field_purge_batch($batch_size);
}
field_purge_batch($batch_size)
,$batch_size
- The maximum number of field data records to purge before returning. So try to set big value for$batch_size
, field won't be removed while it has any data.