/**
  * Create and configure an attribute
  *
  * @param string|null $type
  *
  * @return AttributeInterface
  */
 public function createAttribute($type = null)
 {
     $attribute = $this->create();
     if (null !== $type && '' !== $type) {
         $attributeType = $this->registry->get($type);
         $attribute->setBackendType($attributeType->getBackendType());
         $attribute->setAttributeType($attributeType->getName());
         $attribute->setUnique($attributeType->isUnique());
     }
     return $attribute;
 }
 /**
  * Customize the attribute form
  *
  * @param FormInterface      $form
  * @param AttributeInterface $attribute
  */
 protected function customizeForm(FormInterface $form, AttributeInterface $attribute)
 {
     $attributeTypeClass = $this->attributeTypeRegistry->get($attribute->getAttributeType());
     $fields = $attributeTypeClass->buildAttributeFormTypes($this->factory, $attribute);
     foreach ($fields as $field) {
         $form->add($field);
     }
 }
 /**
  * Create attribute
  *
  * @param Request $request
  *
  * @Template("PimEnrichBundle:Attribute:form.html.twig")
  * @AclAncestor("pim_enrich_attribute_create")
  *
  * @return array
  */
 public function createAction(Request $request)
 {
     $attributeType = $request->get('attribute_type');
     $attributeTypes = $this->registry->getAliases();
     if (!$attributeType || !is_string($attributeType) || !in_array($attributeType, $attributeTypes)) {
         return new RedirectResponse($this->router->generate('pim_enrich_attribute_index'));
     }
     $attribute = $this->factory->createAttribute($attributeType);
     if ($this->attributeHandler->process($attribute)) {
         $this->request->getSession()->getFlashBag()->add('success', new Message('flash.attribute.created'));
         return new RedirectResponse($this->router->generate('pim_enrich_attribute_edit', ['id' => $attribute->getId()]));
     }
     return ['form' => $this->attributeForm->createView(), 'locales' => $this->localeRepository->getActivatedLocaleCodes(), 'disabledLocales' => $this->localeRepository->findBy(['activated' => false]), 'measures' => $this->measuresConfig, 'attributeType' => $attributeType];
 }
 /**
  * @param AttributeInterface $attribute
  * @param string|null        $data
  */
 protected function setType($attribute, $data)
 {
     if ('' === $data || null === $data) {
         throw new \InvalidArgumentException('attributeType must be filled.');
     }
     try {
         $attributeType = $this->registry->get($data);
         $attribute->setAttributeType($attributeType->getName());
         $attribute->setBackendType($attributeType->getBackendType());
         $attribute->setUnique($attributeType->isUnique());
     } catch (\LogicException $exception) {
         throw new \InvalidArgumentException(sprintf('AttributeType "%s" does not exist.', $data));
     }
 }
예제 #5
0
 /**
  * {@inheritdoc}
  */
 protected function addFieldAttributeType(FormBuilderInterface $builder)
 {
     $builder->add('attributeType', 'choice', ['choices' => $this->registry->getSortedAliases(), 'select2' => true, 'disabled' => false, 'read_only' => true]);
 }
 function let(AttributeTypeRegistry $registry, AddAttributeTypeRelatedFieldsSubscriber $subscriber, FormBuilderInterface $builder)
 {
     $registry->getAliases()->willReturn(['text', 'number', 'email']);
     $registry->getSortedAliases()->willReturn(['text' => 'text', 'number' => 'number', 'email' => 'email']);
     $this->beConstructedWith($registry, $subscriber, 'Pim\\Bundle\\CatalogBundle\\Entity\\AttributeTranslation', 'Pim\\Bundle\\CatalogBundle\\Entity\\Attribute', 'Pim\\Bundle\\CatalogBundle\\Entity\\AttributeGroup');
 }