/**
  * {@inheritdoc}
  */
 public function getNewInstance()
 {
     $instance = parent::getNewInstance();
     if ($contextId = $this->getPersistentParameter('context')) {
         $context = $this->contextManager->find($contextId);
         if (!$context) {
             $context = $this->contextManager->create();
             $context->setEnabled(true);
             $context->setId($context);
             $context->setName($context);
             $this->contextManager->save($context);
         }
         $instance->setContext($context);
     }
     return $instance;
 }
Exemplo n.º 2
0
 /**
  * Write a context, this method is used by both POST and PUT action methods.
  *
  * @param Request  $request Symfony request
  * @param int|null $id      A context identifier
  *
  * @return FormInterface
  */
 protected function handleWriteContext($request, $id = null)
 {
     $context = $id ? $this->getContext($id) : null;
     $form = $this->formFactory->createNamed(null, 'sonata_classification_api_form_context', $context, array('csrf_protection' => false));
     FormHelper::removeFields($request->request->all(), $form);
     $form->handleRequest($request);
     if ($form->isValid()) {
         $context = $form->getData();
         $this->contextManager->save($context);
         $view = \FOS\RestBundle\View\View::create($context);
         $serializationContext = SerializationContext::create();
         $serializationContext->setGroups(array('sonata_api_read'));
         $serializationContext->enableMaxDepthChecks();
         $view->setSerializationContext($serializationContext);
         return $view;
     }
     return $form;
 }
Exemplo n.º 3
0
 /**
  * @param $contextCode
  *
  * @return ContextInterface
  */
 private function getContext($contextCode)
 {
     if (empty($contextCode)) {
         $contextCode = ContextInterface::DEFAULT_CONTEXT;
     }
     if ($contextCode instanceof ContextInterface) {
         return $contextCode;
     }
     $context = $this->contextManager->find($contextCode);
     if (!$context instanceof ContextInterface) {
         $context = $this->contextManager->create();
         $context->setId($contextCode);
         $context->setName($contextCode);
         $context->setEnabled(true);
         $this->contextManager->save($context);
     }
     return $context;
 }