/** * Create an option of an attribute * * @param Request $request * @param int $attributeId * * @return JsonResponse * * @AclAncestor("pim_enrich_attribute_edit") */ public function createAction(Request $request, $attributeId) { $attribute = $this->findAttributeOr404($attributeId); $attributeOption = $this->optionManager->createAttributeOption(); $attributeOption->setAttribute($attribute); //Should be replaced by a paramConverter $data = json_decode($request->getContent(), true); return $this->manageFormSubmission($attributeOption, $data); }
/** * Create a new option for a simple/multi-select attribute * * @param Request $request * @param int $id * @param string $dataLocale * * @Template("PimEnrichBundle:Attribute:form_options.html.twig") * @AclAncestor("pim_enrich_attribute_edit") * * @return Response */ public function createOptionAction(Request $request, $id, $dataLocale) { $attribute = $this->findAttributeOr404($id); if (!$request->isXmlHttpRequest() || !in_array($attribute->getAttributeType(), $this->choiceAttributeTypes)) { return $this->redirectToRoute('pim_enrich_attribute_edit', ['id' => $attribute->getId()]); } $option = $this->optionManager->createAttributeOption(); $optionValue = $this->optionManager->createAttributeOptionValue(); $optionValue->setLocale($dataLocale); $optionValue->setValue(''); $option->addOptionValue($optionValue); $attribute->addOption($option); $form = $this->createForm('pim_attribute_option_create', $option); if ($request->isMethod('POST')) { $form->submit($request); if ($form->isValid()) { $this->optionSaver->save($option); $response = ['status' => 1, 'option' => ['id' => $option->getId(), 'label' => $option->setLocale($dataLocale)->__toString()]]; return new Response(json_encode($response)); } } return ['attribute' => $attribute, 'form' => $form->createView()]; }