function farm_constraint_restws_request_alter in farmOS 7
Implements hook_restws_request_alter().
File
- modules/
farm/ farm_constraint/ farm_constraint.module, line 321 - Farm constraint module.
Code
function farm_constraint_restws_request_alter(array &$request) {
// Prevent deletion of entities that are referenced by other entities.
// This works by altering the "operation" in the request object that is built
// in restws_handle_request() before the access check is called. We replace
// the "delete" operation with "noop" so that the access check fails.
// This is a heavy-handed approach, and doesn't provide more nuanced options
// for dealing with referenced data, but it works to stop the deletion from
// occurring.
/**
* @todo
* This is a very hacky way to do this, and doesn't provide any feedback to
* the API user as to why their request fails.
*/
// If an operation, resource, and ID are not available, bail.
if (empty($request['op']) || empty($request['resource']) || empty($request['id'])) {
return;
}
// We only care about delete operations.
if ($request['op'] != 'delete') {
return;
}
// Get the entity type.
$type = $request['resource']
->resource();
// Get the entity ID.
$id = $request['id'];
// Get the entity.
$entity = $request['resource']
->read($id);
// If the entity doesn't exist it cannot have constraints.
if (empty($entity)) {
return;
}
// Get the entity bundle.
list($entity_id, $revision_id, $bundle) = entity_extract_ids($type, $entity);
// If constraints exist on the entity, change the operation to 'noop' so the
// access check in restws_handle_request() fails.
if (farm_constraint_exists($type, $bundle, $id)) {
$request['op'] = 'noop';
}
}