/** * @param FormEvent $event */ public function preSetData(FormEvent $event) { $data = $event->getData(); if (null === $data || null !== $data->getId()) { return; } $data->setSortOrder($this->repository->getMaxSortOrder() + 1); }
/** * {@inheritdoc} * * We need to save a default group because all attributes must have a group. * * For example, we want to remove the price attribute from the product information group. We must put it * in the default group so we make sure it is always saved */ public function write(array $objects) { $this->incrementCount($objects); if (null !== ($defaultGroup = $this->attributeGroupRepository->findDefaultAttributeGroup())) { $objects[] = $defaultGroup; } $this->bulkSaver->saveAll($objects); $this->bulkDetacher->detachAll($objects); }
/** * Remove an attribute from a group and link it to the default group * * @param AttributeGroupInterface $group * @param AttributeInterface $attribute * * @throws \LogicException */ public function removeAttribute(AttributeGroupInterface $group, AttributeInterface $attribute) { if (null === ($default = $this->repository->findDefaultAttributeGroup())) { throw new \LogicException('The default attribute group should exist.'); } $group->removeAttribute($attribute); $attribute->setGroup($default); $this->attributeSaver->saveAll([$attribute]); $this->groupSaver->save($group); }
/** * @param int $id * * @return AttributeGroupInterface */ protected function findAttributeGroupOr404($id) { $result = $this->attributeGroupRepo->find($id); if (null === $result) { throw new NotFoundHttpException('Attribute group not found'); } return $result; }
/** * Hide the group field with a default value = "Other" * * @param FormInterface $form Form * @param AttributeInterface $data */ protected function hideGroupElement(FormInterface $form, AttributeInterface $data) { if (null !== $data->getId()) { $group = $data->getGroup(); } else { $group = $this->groupRepository->findDefaultAttributeGroup(); } $formField = $form->get('group'); $options = $formField->getConfig()->getOptions(); $newOptions = ['data' => $group, 'class' => $options['class'], 'choices' => [$group], 'required' => true, 'multiple' => false, 'read_only' => true, 'attr' => ['class' => 'hide']]; $form->add('group', 'entity', $newOptions); }
/** * @param string $code * * @return AttributeGroupInterface|null */ protected function findAttributeGroup($code) { $attributeGroup = $this->attrGroupRepo->findOneByIdentifier($code); return $attributeGroup; }