thrownewFieldException('Attempt to create a field instance which already exists.');
thrownewFieldException(t('Attempt to create a field instance %field_name,%bundle which already exists.',array('%field_name'=>$instance['field_name'],'%bundle'=>$instance['bundle'])));
}
_field_write_instance($instance);
...
...
@@ -688,3 +691,217 @@ function field_delete_instance($field_name, $bundle) {
/**
* @} End of "defgroup field_crud".
*/
/*
* @defgroup field_purge Field API bulk data deletion
* @{
* Clean up after Field API bulk deletion operations.
*
* Field API provides functions for deleting data attached to individual
* objects as well as deleting entire fields or field instances in a single
* operation.
*
* Deleting field data items for an object with field_attach_delete() involves
* three separate operations:
* - Invoking the Field Type API hook_field_delete() for each field on the
* object. The hook for each field type receives the object and the specific
* field being deleted. A file field module might use this hook to delete
* uploaded files from the filesystem.
* - Invoking the Field Storage API hook_field_storage_delete() to remove
* data from the primary field storage. The hook implementation receives the
* object being deleted and deletes data for all of the object's bundle's
* fields.
* - Invoking the global Field Attach API hook_field_attach_delete() for all
* modules that implement it. Each hook implementation receives the object
* being deleted and can operate on whichever subset of the object's bundle's
* fields it chooses to.
*
* These hooks are invoked immediately when field_attach_delete() is
* called. Similar operations are performed for field_attach_delete_revision().
*
* When a field, bundle, or field instance is deleted, it is not practical to
* invoke these hooks immediately on every affected object in a single page
* request; there could be thousands or millions of them. Instead, the
* appropriate field data items, instances, and/or fields are marked as deleted
* so that subsequent load or query operations will not return them. Later, a
* separate process cleans up, or "purges", the marked-as-deleted data by going
* through the three-step process described above and, finally, removing
* deleted field and instance records.
*
* Purging field data is made somewhat tricky by the fact that, while
* field_attach_delete() has a complete object to pass to the various deletion
* hooks, the Field API purge process only has the field data it has previously
* stored. It cannot reconstruct complete original objects to pass to the
* deletion hooks. It is even possible that the original object to which some
* Field API data was attached has been itself deleted before the field purge
* operation takes place.
*
* Field API resolves this problem by using "pseudo-objects" during purge
* operations. A pseudo-object contains only the information from the original
* object that Field API knows about: entity type, id, revision id, and
* bundle. It also contains the field data for whichever field instance is
* currently being purged. For example, suppose that the node type 'story' used
* to contain a field called 'subtitle' but the field was deleted. If node 37
* was a story with a subtitle, the pseudo-object passed to the purge hooks
* would look something like this:
*
* @code
* $obj = stdClass Object(
* [nid] => 37,
* [vid] => 37,
* [type] => 'story',
* [subtitle] => array(
* [0] => array(
* 'value' => 'subtitle text',
* ),
* ),
* );
* @endcode
*/
/**
* Purge some deleted Field API data, instances, or fields.
*
* This function will purge deleted field data on up to a specified maximum
* number of objects and then return. If a deleted field instance with no
* remaining data records is found, the instance itself will be purged.
* If a deleted field with no remaining field instances is found, the field
* itself will be purged.
*
* @param $batch_size
* The maximum number of field data records to purge before returning.
*/
functionfield_purge_batch($batch_size){
// Retrieve all deleted field instances. We cannot use field_info_instances()
// because that function does not return deleted instances.