View source
<?php
namespace HookUpdateDeployTools;
class Fields {
public static function deleteInstance($field_name, $bundle_name, $entity_type) {
Check::notEmpty('$field_name', $field_name);
Check::notEmpty('$bundle_name', $bundle_name);
Check::notEmpty('$entity_type', $entity_type);
$msg_vars = array(
'!field_name' => $field_name,
'!bundle_name' => $bundle_name,
'!entity_type' => $entity_type,
);
$instance = field_info_instance($entity_type, $field_name, $bundle_name);
if ($instance) {
$msg = 'The field instance for field:!field_name in bundle:!bundle_name in entity:!entity_type is being deleted.';
$return = Message::make($msg, $msg_vars, WATCHDOG_INFO, 1);
field_delete_instance($instance, TRUE);
field_purge_batch(10);
$instance = field_info_instance($entity_type, $field_name, $bundle_name);
if ($instance) {
$msg = 'The field instance for field:!field_name in bundle:!bundle_name in entity:!entity_type seems to still exist.';
$return .= Message::make($msg, $msg_vars, WATCHDOG_ERROR, 1);
throw new HudtException($msg, $msg_vars, WATCHDOG_ERROR, FALSE);
}
else {
$msg = 'The field instance for field:!field_name was successfully removed from bundle:!bundle_name in entity:!entity_type.';
$return .= Message::make($msg, $msg_vars, WATCHDOG_INFO, 1);
$active_fields = field_info_field_map();
if (!empty($active_fields[$field_name])) {
$msg = 'The field:!field_name is still in use in other entities, so the other instances were not deleted.';
$return .= Message::make($msg, $msg_vars, WATCHDOG_INFO, 1);
}
else {
$msg = 'The field:!field_name is not in use in other entities, so it was fully removed.';
$return .= Message::make($msg, $msg_vars, WATCHDOG_INFO, 1);
}
}
}
else {
$msg = 'The field instance for field:!field_name in bundle:!bundle_name in entity:!entity_type does not exist. Assuming this as already successful.';
$return = Message::make($msg, $msg_vars, WATCHDOG_INFO, 1);
}
return $return;
}
}