public function ServicesEntityResourceControllerClean::create in Services Entity API 7.2
Implements ServicesResourceControllerInterface::create().
Overrides ServicesEntityResourceController::create
File
- plugins/
services_entity_resource_clean.inc, line 34
Class
- ServicesEntityResourceControllerClean
- This class is designed to create a very clean API that integrates with the services and entity modules. We want to strip all "drupalisms" out of the API. For example, there should be no [LANGUAGE_NONE][0][value] or field_ in the API.
Code
public function create($entity_type, array $values) {
$wrapper = $this
->createWrapperFromValues($entity_type, $values);
// Check write access on each property.
foreach (array_keys($values) as $name) {
if (!$this
->propertyAccess($wrapper, $name, 'create')) {
services_error(t("Not authorized to set property '@p'", array(
'@p' => $name,
)), 403);
}
}
// Make sure that bundle information is present on entities that have
// bundles. We have to do this after creating the wrapper, because the
// name of the bundle key may differ from that of the corresponding
// metadata property (e.g. for taxonomy terms, the bundle key is
// 'vocabulary_machine_name', while the property is 'vocabulary').
if ($bundle_key = $wrapper
->entityKey('bundle')) {
$entity = $wrapper
->value();
if (empty($entity->{$bundle_key})) {
$entity_info = $wrapper
->entityInfo();
if (isset($entity_info['bundles']) && count($entity_info['bundles']) === 1) {
// If the entity supports only a single bundle, then use that as a
// default. This allows creation of such entities if (as with ECK)
// they still use a bundle key.
$entity->{$bundle_key} = reset($entity_info['bundles']);
}
else {
services_error('Missing bundle: ' . $bundle_key, 406);
}
}
}
$properties = $wrapper
->getPropertyInfo();
$diff = array_diff_key($values, $properties);
if (!empty($diff)) {
services_error('Unknown data properties: ' . implode(' ', array_keys($diff)) . '.', 406);
}
$wrapper
->save();
return $this
->get_data($wrapper, '*');
}