/** * Update attribute option sorting * * @param AttributeInterface $attribute * @param array $sorting */ public function updateSorting(AttributeInterface $attribute, array $sorting = []) { foreach ($attribute->getOptions() as $option) { if (isset($sorting[$option->getId()])) { $option->setSortOrder($sorting[$option->getId()]); } else { $option->setSortOrder(0); } } $this->optionSaver->saveAll($attribute->getOptions()->toArray()); }
/** * @param AttributeRepositoryInterface $attributeRepository * @param AttributeOptionRepositoryInterface $attributeOptionRepository * @param string $attributeCode * @param FileLoader $loader * @param string $mappingFile */ public function __construct(AttributeRepositoryInterface $attributeRepository, AttributeOptionRepositoryInterface $attributeOptionRepository, $attributeCode, FileLoader $loader = null, $mappingFile = null) { $this->attributeRepository = $attributeRepository; $this->attributeOptionRepository = $attributeOptionRepository; $this->mapping = []; $this->attribute = $this->attributeRepository->findOneByIdentifier($attributeCode); if ($this->attribute) { $this->attributeOptions = $this->attribute->getOptions(); } else { $this->attributeOptions = new ArrayCollection(); } if ($mappingFile !== null && $loader !== null) { foreach ($loader->load($mappingFile) as $rawValue => $optionCode) { $this->mapTo($rawValue, $optionCode); } } }
/** * {@inheritdoc} */ protected function normalizeOptions(AttributeInterface $attribute) { $options = $attribute->getOptions(); if ($options->isEmpty()) { $options = ''; } else { $data = []; foreach ($options as $option) { $item = []; foreach ($option->getOptionValues() as $value) { $label = str_replace('{locale}', $value->getLocale(), self::LOCALIZABLE_PATTERN); $label = str_replace('{value}', $value->getValue(), $label); $item[] = $label; } $data[] = 'Code:' . $option->getCode() . self::ITEM_SEPARATOR . implode(self::ITEM_SEPARATOR, $item); } $options = implode(self::GROUP_SEPARATOR, $data); } return $options; }
/** * Normalize options * * @param AttributeInterface $attribute * * @return array */ protected function normalizeOptions(AttributeInterface $attribute) { $data = []; $options = $attribute->getOptions(); foreach ($options as $option) { $data[$option->getCode()] = []; foreach ($option->getOptionValues() as $value) { $data[$option->getCode()][$value->getLocale()] = $value->getValue(); } } return $data; }