The TagManager is responsible for the centralized management of our tags.
/** * {@inheritdoc} */ public function write(NodeInterface $node, PropertyInterface $property, $userId, $webspaceKey, $languageCode, $segmentKey) { $tagIds = []; $tags = $property->getValue() === null ? [] : $property->getValue(); foreach ($tags as $tag) { $tagIds[] = $this->tagManager->findOrCreateByName($tag, $userId)->getId(); } $node->setProperty($property->getName(), $tagIds); }
/** * @return array */ public function getTagsFunction() { return $this->memoizeCache->memoize(function () { $tags = $this->tagManager->findAll(); $context = SerializationContext::create(); $context->setSerializeNull(true); $context->setGroups(['partialTag']); return $this->serializer->serialize($tags, 'array', $context); }); }
public function setUp() { $this->contentQuery = $this->getMockForAbstractClass('Sulu\\Component\\Content\\Query\\ContentQueryExecutorInterface'); $this->contentQueryBuilder = $this->getMockForAbstractClass('Sulu\\Component\\Content\\Query\\ContentQueryBuilderInterface'); $this->tagManager = $this->getMockForAbstractClass('Sulu\\Bundle\\TagBundle\\Tag\\TagManagerInterface', [], '', false, true, true, ['resolveTagIds', 'resolveTagNames']); $this->requestStack = $this->getMockBuilder('Symfony\\Component\\HttpFoundation\\RequestStack')->getMock(); $this->request = $this->getMockBuilder('Symfony\\Component\\HttpFoundation\\Request')->getMock(); $this->requestStack->expects($this->any())->method('getCurrentRequest')->will($this->returnValue($this->request)); $this->smartContent = new SmartContent($this->contentQuery, $this->contentQueryBuilder, $this->tagManager, $this->requestStack, 'SuluContentBundle:Template:content-types/smart_content.html.twig'); $this->tagManager->expects($this->any())->method('resolveTagIds')->will($this->returnValueMap([[[1, 2], ['Tag1', 'Tag2']]])); $this->tagManager->expects($this->any())->method('resolveTagName')->will($this->returnValueMap([[['Tag1', 'Tag2'], [1, 2]]])); }
/** * {@inheritdoc} */ public function getContentData(PropertyInterface $property) { // check memoize $hash = spl_object_hash($property); if (array_key_exists($hash, $this->cache)) { return $this->cache[$hash]; } /** @var PropertyParameter[] $params */ $params = array_merge($this->getDefaultParams($property), $property->getParams()); // prepare filters $filters = $property->getValue(); $filters['excluded'] = [$property->getStructure()->getUuid()]; // default value of tags/category is an empty array if (!array_key_exists('tags', $filters)) { $filters['tags'] = []; } if (!array_key_exists('categories', $filters)) { $filters['categories'] = []; } // extends selected filter with requested tags $filters['websiteTags'] = $this->tagRequestHandler->getTags($params['tags_parameter']->getValue()); $filters['websiteTagsOperator'] = $params['website_tags_operator']->getValue(); // extends selected filter with requested categories $filters['websiteCategories'] = $this->categoryRequestHandler->getCategories($params['categories_parameter']->getValue()); $filters['websiteCategoriesOperator'] = $params['website_categories_operator']->getValue(); // resolve tags to id if (!empty($filters['tags'])) { $filters['tags'] = $this->tagManager->resolveTagNames($filters['tags']); } // resolve website tags to id if (!empty($filters['websiteTags'])) { $filters['websiteTags'] = $this->tagManager->resolveTagNames($filters['websiteTags']); } // get provider $provider = $this->getProvider($property); $configuration = $provider->getConfiguration(); // prepare pagination, limitation and options $page = 1; $limit = array_key_exists('limitResult', $filters) && $configuration->hasLimit() ? $filters['limitResult'] : null; $options = ['webspaceKey' => $property->getStructure()->getWebspaceKey(), 'locale' => $property->getStructure()->getLanguageCode()]; if (isset($params['max_per_page']) && $configuration->hasPagination()) { // is paginated $page = $this->getCurrentPage($params['page_parameter']->getValue()); $pageSize = intval($params['max_per_page']->getValue()); // resolve paginated filters $data = $provider->resolveResourceItems($filters, $params, $options, !empty($limit) ? intval($limit) : null, $page, $pageSize); } else { $data = $provider->resolveResourceItems($filters, $params, $options, !empty($limit) ? intval($limit) : null); } // append view data $filters['page'] = $page; $filters['hasNextPage'] = $data->getHasNextPage(); $filters['referencedUuids'] = $data->getReferencedUuids(); $filters['paginated'] = $configuration->hasPagination(); $property->setValue($filters); // save result in cache return $this->cache[$hash] = $data->getItems(); }
public function setUp() { $this->contentDataProvider = $this->prophesize(DataProviderInterface::class); $this->contentDataProvider->getConfiguration()->willReturn($this->getProviderConfiguration()); $this->contentDataProvider->getDefaultPropertyParameter()->willReturn([]); $this->dataProviderPool = new DataProviderPool(); $this->dataProviderPool->add('content', $this->contentDataProvider->reveal()); $this->tagManager = $this->getMockForAbstractClass(TagManagerInterface::class, [], '', false, true, true, ['resolveTagIds', 'resolveTagNames']); $this->requestStack = $this->getMockBuilder(RequestStack::class)->getMock(); $this->request = $this->getMockBuilder(Request::class)->getMock(); $this->requestStack->expects($this->any())->method('getCurrentRequest')->will($this->returnValue($this->request)); $this->tagRequestHandler = $this->prophesize(TagRequestHandlerInterface::class); $this->tagRequestHandler->getTags('tags')->willReturn([]); $this->categoryRequestHandler = $this->prophesize(CategoryRequestHandlerInterface::class); $this->categoryRequestHandler->getCategories('categories')->willReturn([]); $this->smartContent = new SmartContent($this->dataProviderPool, $this->tagManager, $this->requestStack, $this->tagRequestHandler->reveal(), $this->categoryRequestHandler->reveal(), 'SuluContentBundle:Template:content-types/smart_content.html.twig'); $this->tagManager->expects($this->any())->method('resolveTagIds')->will($this->returnValueMap([[[1, 2], ['Tag1', 'Tag2']]])); $this->tagManager->expects($this->any())->method('resolveTagName')->will($this->returnValueMap([[['Tag1', 'Tag2'], [1, 2]]])); }
/** * {@inheritdoc} */ public function importData(NodeInterface $node, PropertyInterface $property, $value, $userId, $webspaceKey, $languageCode, $segmentKey = null) { $tagNames = []; $tagIds = json_decode($value); if (!empty($tagIds)) { $tagNames = $this->tagManager->resolveTagIds($tagIds); } $property->setValue($tagNames); $this->write($node, $property, $userId, $webspaceKey, $languageCode, $segmentKey); }
/** * {@inheritdoc} */ public function write(NodeInterface $node, PropertyInterface $property, $userId, $webspaceKey, $languageCode, $segmentKey) { $value = $property->getValue(); if ($value instanceof ArrayableInterface) { $value = $value->toArray(); } // if whole smart-content container is pushed if (isset($value['config'])) { $value = $value['config']; } if (!empty($value['tags'])) { $value['tags'] = $this->tagManager->resolveTagNames($value['tags']); } $node->setProperty($property->getName(), json_encode($value)); }
/** * Lazy loads the data based on the filter criteria from the config. * * @param array $excludeUuids * @param int $limit * @param int $offset * * @return StructureInterface[] */ public function getData($excludeUuids = [], $limit = null, $offset = null) { if ($this->data === null) { // resolve tagNames to ids for loading data $config = $this->getConfig(); if (!empty($config['tags'])) { $config['tags'] = $this->tagManager->resolveTagNames($config['tags']); } // determine limit if ($limit === null && isset($config['limitResult'])) { $limit = $config['limitResult']; } $this->data = $this->loadData($config, $excludeUuids, $limit, $offset); } return $this->data; }
/** * @param $value * @param $key */ protected function resolveTags(&$value, $key) { if (isset($value[$key])) { $ids = []; $names = []; foreach ($value[$key] as $tag) { if (is_numeric($tag)) { $ids[] = $tag; } else { $names[] = $tag; } } if (!empty($names)) { foreach ($this->tagManager->resolveTagNames($names) as $id) { $ids[] = $id; } } $value[$key] = $ids; } }
/** * Data can be set over by array. * * @param $media * @param $data * @param UserInterface $user * * @return Media */ protected function setDataToMedia(Media $media, $data, $user) { foreach ($data as $attribute => $value) { if ($value || $attribute === 'tags' && $value !== null || $attribute === 'size' && $value !== null || $attribute === 'description' && $value !== null || $attribute === 'copyright' && $value !== null) { switch ($attribute) { case 'size': $media->setSize($value); break; case 'title': $media->setTitle($value); break; case 'description': $media->setDescription($value); break; case 'copyright': $media->setCopyright($value); break; case 'version': $media->setVersion($value); break; case 'name': $media->setName($value); break; case 'url': $media->setUrl($value); break; case 'formats': $media->setFormats($value); break; case 'storageOptions': $media->setStorageOptions($value); break; case 'publishLanguages': $media->setPublishLanguages($value); break; case 'contentLanguages': $media->setContentLanguages($value); break; case 'tags': $media->removeTags(); if (count($value)) { foreach ($value as $tag) { $tagEntity = $this->tagManager->findOrCreateByName($tag, $user->getId()); $media->addTag($tagEntity); } } break; case 'properties': $media->setProperties($value); break; case 'changed': $media->setChanged($value); break; case 'created': break; case 'changer': if ($value instanceof UserInterface) { $media->setChanger($value); } break; case 'creator': if ($value instanceof UserInterface) { $media->setCreator($value); } break; case 'mimeType': $media->setMimeType($value); break; case 'collection': $collectionEntity = $this->getCollectionById($value); $media->setCollection($collectionEntity); // set parent break; case 'type': if (isset($value['id'])) { $type = $this->typeManager->get($value['id']); $media->setType($type); } break; } } } return $media; }
/** * {@inheritdoc} */ public function convert($value) { return $this->tagManager->resolveTagNames($value); }
public function testResolveTagIds() { $tagIds = [1, 2, 3, 99]; $tagNames = $this->tagManager->resolveTagIds($tagIds); $this->assertEquals(['Tag1', 'Tag2', 'Tag3'], $tagNames); }