/** * Sets the options of the attribute * * @param string $class * @param AttributeInterface $attribute * @param array $optionsData */ protected function setOptions($class, AttributeInterface $attribute, array $optionsData) { $this->doctrineCache->setReference($attribute); $optionClass = $this->optionManager->getAttributeOptionClass(); foreach ($optionsData as $code => $optionData) { $optionData['attribute'] = $attribute->getCode(); if (!isset($optionData['code'])) { $optionData['code'] = $code; } $option = $this->transformNestedEntity($class, 'options', $optionClass, $optionData); $attribute->addOption($option); $this->doctrineCache->setReference($option); } }
/** * Ensure that there is an option for all activated locales * * @param Collection $optionValues * * @return Collection */ protected function ensureEmptyOptionValues(Collection $optionValues) { $activeLocales = $this->getActiveLocales(); $usedLocales = []; foreach ($optionValues as $optionValue) { $usedLocales[] = $optionValue->getLocale(); } foreach ($activeLocales as $activatedLocale) { if (!in_array($activatedLocale->getCode(), $usedLocales)) { $attributeOptionValue = $this->optionManager->createAttributeOptionValue(); $attributeOptionValue->setLocale($activatedLocale->getCode()); $attributeOptionValue->setValue(''); $optionValues->add($attributeOptionValue); } } return $optionValues; }
/** * Find an attribute option or throw a 404 * * @param integer $id The id of the attribute option * * @throws NotFoundHttpException * @return AttributeOptionInterface */ protected function findAttributeOptionOr404($id) { try { $result = $this->optionManager->getAttributeOption($id); } catch (EntityNotFoundException $e) { throw new NotFoundHttpException($e->getMessage()); } return $result; }
/** * 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()]; }