EntityPut.php in Services 9.0.x
File
src/Plugin/ServiceDefinition/EntityPut.php
View source
<?php
namespace Drupal\services\Plugin\ServiceDefinition;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\services\ServiceDefinitionEntityRequestContentBase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\Routing\Route;
class EntityPut extends ServiceDefinitionEntityRequestContentBase {
public function processRoute(Route $route) {
$route
->setRequirement('_entity_access', $this
->getDerivativeId() . '.update');
}
public function processRequest(Request $request, RouteMatchInterface $route_match, SerializerInterface $serializer) {
try {
$updated_entity = parent::processRequest($request, $route_match, $serializer);
$entity = $this
->getContextValue($this
->getDerivativeId());
if ($entity instanceof ContentEntityInterface) {
foreach ($updated_entity as $field_name => $field) {
$entity
->set($field_name, $field
->getValue());
}
}
else {
foreach ($updated_entity
->toArray() as $field_name => $field) {
$entity
->set($field_name, $field);
}
}
$entity
->save();
return $entity
->toArray();
} catch (\Exception $e) {
throw new HttpException(422, 'The supplied content body could not be serialized into an entity of the requested type.', $e);
}
}
}
Classes
Name |
Description |
EntityPut |
Plugin annotation
@ServiceDefinition(
id = "entity_put",
methods = {
"PUT"
},
translatable = true,
deriver = "\Drupal\services\Plugin\Deriver\EntityPut"
) |