src/Controller/CRUDController.php
public function generateEntityCodeAction($id = null)
    {
        $request = $this->getRequest();

        $id = $request->get($this->admin->getIdParameter());
        if ($id) {
            $subject = $this->admin->getObject($id);
            if (!$subject) {
                $error = sprintf('unable to find the object with id : %s', $id);

                return new JsonResponse(['error' => $error]);
            }
            try {
                $this->admin->checkAccess('edit', $subject); // TODO: is it necessary ? (we are not editing the entity)
            } catch (Exception $exc) {
                $error = $exc->getMessage();

                return new JsonResponse(['error' => $error]);
            }
        } else {
            $subject = $this->admin->getNewInstance();
        }

        $this->admin->setSubject($subject);

        $form = $this->admin->getForm();
        $form->setData($subject);
        $form->submit($request->request->get($form->getName()));
        $entity = $form->getData();

        $field = $request->query->get('field', 'code');
        $registry = $this->get('blast_core.code_generators');
        $generator = $registry::getCodeGenerator(get_class($entity), $field);
        try {
            $code = $generator::generate($entity);

            return new JsonResponse(['code' => $code]);
        } catch (\Exception $exc) {
            $error = $this->get('translator')->trans($exc->getMessage());

            return new JsonResponse(['error' => $error, 'generator' => get_class($generator)]);
        }
    }
public function generateEntityCodeAction($id = null)
    {
        $request = $this->getRequest();

        $id = $request->get($this->admin->getIdParameter());
        if ($id) {
            $subject = $this->admin->getObject($id);
            if (!$subject) {
                $error = sprintf('unable to find the object with id : %s', $id);

                return new JsonResponse(['error' => $error]);
            }
            try {
                $this->admin->checkAccess('edit', $subject); // TODO: is it necessary ? (we are not editing the entity)
            } catch (Exception $exc) {
                $error = $exc->getMessage();

                return new JsonResponse(['error' => $error]);
            }
        } else {
            $subject = $this->admin->getNewInstance();
        }

        $this->admin->setSubject($subject);

        $form = $this->admin->getForm();
        $form->setData($subject);
        $form->submit($request->request->get($form->getName()));
        $entity = $form->getData();

        $field = $request->query->get('field', 'code');
        $registry = $this->get('blast_core.code_generators');
        $generator = $registry::getCodeGenerator(get_class($entity), $field);
        try {
            $code = $generator::generate($entity);

            return new JsonResponse(['code' => $code]);
        } catch (\Exception $exc) {
            $error = $this->get('translator')->trans($exc->getMessage());

            return new JsonResponse(['error' => $error, 'generator' => get_class($generator)]);
        }
    }
public function generateEntityCodeAction ( $ id = null ) { $ request = $ this -> getRequest ( ) ; $ id = $ request -> get ( $ this -> admin -> getIdParameter ( ) ) ; if ( $ id ) { $ subject = $ this -> admin -> getObject ( $ id ) ; if ( ! $ subject ) { $ error = sprintf ( 'unable to find the object with id : %s' , $ id ) ; return new JsonResponse ( [ 'error' => $ error ] ) ; } try { $ this -> admin -> checkAccess ( 'edit' , $ subject ) ; // TODO: is it necessary ? (we are not editing the entity) } catch ( Exception $ exc ) { $ error = $ exc -> getMessage ( ) ; return new JsonResponse ( [ 'error' => $ error ] ) ; } } else { $ subject = $ this -> admin -> getNewInstance ( ) ; } $ this -> admin -> setSubject ( $ subject ) ; $ form = $ this -> admin -> getForm ( ) ; $ form -> setData ( $ subject ) ; $ form -> submit ( $ request -> request -> get ( $ form -> getName ( ) ) ) ; $ entity = $ form -> getData ( ) ; $ field = $ request -> query -> get ( 'field' , 'code' ) ; $ registry = $ this -> get ( 'blast_core.code_generators' ) ; $ generator = $ registry :: getCodeGenerator ( get_class ( $ entity ) , $ field ) ; try { $ code = $ generator :: generate ( $ entity ) ; return new JsonResponse ( [ 'code' => $ code ] ) ; } catch ( \ Exception $ exc ) { $ error = $ this -> get ( 'translator' ) -> trans ( $ exc -> getMessage ( ) ) ; return new JsonResponse ( [ 'error' => $ error , 'generator' => get_class ( $ generator ) ] ) ; } }
Generate Entity Code action.

@param int|string|null $id

@return JsonResponse
Generate Entity Code action .