public function RestWSEntityResourceController::create in RESTful Web Services 7
Same name and namespace in other branches
- 7.2 restws.entity.inc \RestWSEntityResourceController::create()
Create a new resource.
Parameters
array $values: Array of values for properties of the resource, keyed by property name. At least for all required properties values have to be given.
Return value
int|string The id of the newly created resource.
Overrides RestWSResourceControllerInterface::create
File
- ./
restws.entity.inc, line 114 - RESTful web services module integration for entities.
Class
- RestWSEntityResourceController
- Controller for entity-bases resources.
Code
public function create(array $values) {
// Make sure that bundle information is present on entities that have
// bundles.
$entity_info = entity_get_info($this->entityType);
if (isset($entity_info['bundle keys'])) {
foreach ($entity_info['bundle keys'] as $bundle_key) {
if (!array_key_exists($bundle_key, $values)) {
throw new RestWSException('Missing bundle: ' . $bundle_key, 406);
}
}
}
try {
$wrapper = entity_property_values_create_entity($this->entityType, $values);
// Get the ID and bundle property names.
$entity_keys = array_intersect_key($entity_info['entity keys'], array(
'id' => 1,
'bundle' => 1,
));
foreach (array_keys($values) as $name) {
// Don't check access on entity keys for new entities. Otherwise,
// property access checks will fail for, e.g., node type, which
// requires the 'administer nodes' permission to set.
// @see entity_metadata_node_entity_property_info().
if (!in_array($name, $entity_keys)) {
if (!$this
->checkPropertyAccess($wrapper, $name, $wrapper->{$name})) {
throw new RestWSException(t('Not authorized to set property @p', array(
'@p' => $name,
)), 403);
}
}
}
} catch (EntityMetadataWrapperException $e) {
throw new RestWSException($e
->getMessage(), 406);
}
$properties = $wrapper
->getPropertyInfo();
$diff = array_diff_key($values, $properties);
if (!empty($diff)) {
throw new RestWSException('Unknown data properties: ' . implode(' ', array_keys($diff)) . '.', 406);
}
$wrapper
->save();
return $wrapper
->getIdentifier();
}