Same as getApiObject, but takes multiple category-entities.
public getApiObjects ( Sulu\Bundle\CategoryBundle\Entity\CategoryInterface[] $categories, string $locale ) : Sulu\Bundle\CategoryBundle\Entity\CategoryInterface | ||
$categories | Sulu\Bundle\CategoryBundle\Entity\CategoryInterface[] | |
$locale | string | |
return | Sulu\Bundle\CategoryBundle\Entity\CategoryInterface |
public function testGetApiObjects() { $entities = [new CategoryEntity(), null, new CategoryEntity(), new CategoryEntity(), null]; $wrappers = $this->categoryManager->getApiObjects($entities, 'en'); $this->assertTrue($wrappers[0] instanceof CategoryWrapper); $this->assertTrue($wrappers[2] instanceof CategoryWrapper); $this->assertTrue($wrappers[3] instanceof CategoryWrapper); $this->assertEquals(null, $wrappers[1]); $this->assertEquals(null, $wrappers[4]); }
/** * Returns an array of serialized categories. * If parentKey is set, only the children of the category which is assigned to the given key are returned. * * @param string $locale * @param string $parentKey key of parent category * * @return array */ public function getCategoriesFunction($locale, $parentKey = null) { return $this->memoizeCache->memoize(function ($locale, $parentKey = null) { $entities = $this->categoryManager->findChildrenByParentKey($parentKey); $categories = $this->categoryManager->getApiObjects($entities, $locale); $context = SerializationContext::create(); $context->setSerializeNull(true); return $this->serializer->serialize($categories, 'array', $context); }); }
/** * {@inheritdoc} */ public function read(NodeInterface $node, PropertyInterface $property, $webspaceKey, $languageCode, $segmentKey) { $data = []; $categoryIds = $node->getPropertyValueWithDefault($property->getName(), []); $categories = $this->categoryManager->findByIds($categoryIds); $categories = $this->categoryManager->getApiObjects($categories, $languageCode); foreach ($categories as $category) { $data[] = $category->toArray(); } $this->setData($data, $property); }
/** * {@inheritdoc} */ public function getContentData(PropertyInterface $property) { $ids = $property->getValue(); if (!is_array($ids) || empty($ids)) { return []; } $data = []; $categoryEntities = $this->categoryManager->findByIds($ids); $categoryApiEntities = $this->categoryManager->getApiObjects($categoryEntities, $property->getStructure()->getLanguageCode()); foreach ($categoryApiEntities as $category) { $data[] = $category->toArray(); } return $data; }