I want to delete field from my Content Type using hook_update and wondering how to do it in most correct way.
I know that my field is only on 1 Content Type (page).
Here is my current solution.
function site_deployment_update_7001() {
$fieldname = 'field_landing_page';
if ($field = field_info_instance('node', $fieldname, 'page')) {
field_delete_instance($field);
}
if ($field = field_info_field($fieldname)) {
field_delete_field($field);
}
}
However I'm a bit confused if I need to do field_delete_field after field_delete_instance. It's because on Drupal.org says that field_delete_instance doing field_delete_field as well (with condition: condition count($field['bundles']) == 0). I'm wondering about this condition, in what cases it is 0?
https://api.drupal.org/api/drupal/modules!field!field.crud.inc/function/field_delete_instance/7
function field_delete_instance($instance, $field_cleanup = TRUE) {
// Mark the field instance for deletion.
db_update('field_config_instance')->fields(array('deleted' => 1))->condition('field_name', $instance['field_name'])->condition('entity_type', $instance['entity_type'])->condition('bundle', $instance['bundle'])->execute();
// Clear the cache.
field_cache_clear();
// Mark instance data for deletion.
$field = field_info_field($instance['field_name']);
module_invoke($field['storage']['module'], 'field_storage_delete_instance', $instance);
// Let modules react to the deletion of the instance.
module_invoke_all('field_delete_instance', $instance);
// Delete the field itself if we just deleted its last instance.
if ($field_cleanup && count($field['bundles']) == 0) {
field_delete_field($field['field_name']);
}
}
Please advise what are the best approach to delete field. Thanks!