示例#1
0
 /**
  * {@inheritdoc}
  */
 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()) {
                 drupal_set_message($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.");
 }
示例#2
0
 /**
  * {@inheritdoc}
  */
 public function processRequest(Request $request, RouteMatchInterface $route_match, SerializerInterface $serializer)
 {
     try {
         $updated_entity = parent::processRequest($request, $route_match, $serializer);
         /** @var $entity \Drupal\Core\Entity\EntityInterface */
         $entity = $this->getContextValue($this->getDerivativeId());
         if ($entity instanceof ContentEntityInterface) {
             foreach ($updated_entity as $field_name => $field) {
                 $entity->set($field_name, $field->getValue());
             }
         } else {
             /** @var $updated_entity \Drupal\Core\Config\Entity\ConfigEntityInterface */
             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);
     }
 }