You are here

public function EntityPost::processRequest in Services 8.4

Same name and namespace in other branches
  1. 9.0.x src/Plugin/ServiceDefinition/EntityPost.php \Drupal\services\Plugin\ServiceDefinition\EntityPost::processRequest()

Return value

\Drupal\Core\Entity\EntityInterface|array

Overrides ServiceDefinitionEntityRequestContentBase::processRequest

File

src/Plugin/ServiceDefinition/EntityPost.php, line 39

Class

EntityPost
Plugin annotation @ServiceDefinition( id = "entity_post", methods = { "POST" }, translatable = true, response_code = 201, deriver = "\Drupal\services\Plugin\Deriver\EntityPost" )

Namespace

Drupal\services\Plugin\ServiceDefinition

Code

public function processRequest(Request $request, RouteMatchInterface $route_match, SerializerInterface $serializer) {
  $entity = parent::processRequest($request, $route_match, $serializer);
  if ($entity) {
    try {
      $entity
        ->save();
      if ($entity
        ->id()) {
        $this
          ->messenger()
          ->addMessage($this
          ->t('Entity of type @type was created.', [
          '@type' => $entity
            ->getEntityType()
            ->id(),
        ]));
        return $entity
          ->toArray();
      }
    } catch (EntityStorageException $e) {
      throw new HttpException('500', $e
        ->getMessage());
    }
  }
  throw new HttpException('500', 'The entity could not be created.');
}