예제 #1
0
 /**
  * Write a post, this method is used by both POST and PUT action methods.
  *
  * @param Request  $request Symfony request
  * @param int|null $id      A post identifier
  *
  * @return FormInterface
  */
 protected function handleWritePost($request, $id = null)
 {
     $post = $id ? $this->getPost($id) : null;
     $form = $this->formFactory->createNamed(null, 'sonata_news_api_form_post', $post, array('csrf_protection' => false));
     $form->bind($request);
     if ($form->isValid()) {
         $post = $form->getData();
         $post->setContent($this->formatterPool->transform($post->getContentFormatter(), $post->getRawContent()));
         $this->postManager->save($post);
         $view = \FOS\RestBundle\View\View::create($post);
         $serializationContext = SerializationContext::create();
         $serializationContext->setGroups(array('sonata_api_read'));
         $serializationContext->enableMaxDepthChecks();
         $view->setSerializationContext($serializationContext);
         return $view;
     }
     return $form;
 }