/   /   /  Sulu Symfony CMS : get nodes collection by tag name

Note:

For more extensions and themes visit our store

Sulu Symfony CMS : get nodes collection by tag name


Sulu cms framework is an open-source content management platform based on the Symfony PHP framework. Use it as a CMS to develop and manage enterprise multi-sites or as a development environment to create reliable and high-performant web-apps.
The official doc is here

That is our first tutorial in this cms , we used and observe that is easy and practice but need more large community .

Ok this tutorial cover how getting collection of nodes based on tag name .

- Some parameters used inside our code :

 

  1.  
  2.         $dataSource = null;
  3.         $includeSubFolders = null;
  4.         $languageCode = 'en';
  5.         $tagOperator = 'or';
  6.         $sortBy = 'date';
  7.         $sortMethod = 'asc';
  8.         $exclude = null;
  9.         $webspaceKey = 'ibnab';
  10.         $limitResult = 1;
  11.         $tagNames = 'sulu';
  12.         $resolvedTags = [];
  13.  

- Get service tag manager :

  1.  
  2.         $tagManager = $this->get('sulu_tag.tag_manager');
  3.         if (isset($tagNames)) {
  4.             $tags = explode(',', $tagNames);
  5.             foreach ($tags as $tag) {
  6.                 $resolvedTag = $tagManager->findByName($tag);
  7.                 if ($resolvedTag) {
  8.                     $resolvedTags[] = $resolvedTag->getId();
  9.                 }
  10.             }
  11.         }
  12.  

- Some code about sorted columns :

  1.  
  2.         $sortColumns = [];
  3.         if (isset($sortBy)) {
  4.             $columns = explode(',', $sortBy);
  5.             foreach ($columns as $column) {
  6.                 if ($column) {
  7.                     $sortColumns[] = $column;
  8.                 }
  9.             }
  10.         }
  11.  

- Create global  configuration used in our repository selection :

  1.  
  2.         $filterConfig = [
  3.             'dataSource' => $dataSource,
  4.             'includeSubFolders' => $includeSubFolders,
  5.             'limitResult' => $limitResult,
  6.             'tags' => $resolvedTags,
  7.             'tag' => $tagOperator,
  8.             'sortBy' => $sortColumns,
  9.             'sortMethod' => $sortMethod,
  10.         ];
  11.  

- New get service and call the method  getFilteredNodes :

  1.  
  2.         /** @var NodeRepository $repository */
  3.         $repository = $this->get('sulu_content.node_repository');
  4.         $contents = $repository->getFilteredNodes(
  5.             $filterConfig,
  6.             $languageCode,
  7.             $webspaceKey,
  8.             true,
  9.             true,
  10.             $exclude !== null ? [$exclude] : []
  11.         );
  12.  
 
   
- The collections nodes is inside :
  1.  
  2.         $contents =  $contents['_embedded']['nodes'];
  3.  

- You can pass to twig :

  1.  
  2.         $response = $this->renderStructure(
  3.             $structure,
  4.             ['contents' => $content],
  5.             $preview,
  6.             $partial
  7.         );
  8.         return $response;
  9.  

- Now dumping inside twig :

  1.  
  2. {{ dump(contents) }}
  3.  

that is all

Comments

make your store more efficient

Solving problems. With open source technology. Professional results. That’s what makes Ibnab your best choice

IBNAB is a company made of a group of professionals whose work is providing secure open source solutions. Our company strives for reaching magnificent results with each experience and provides professional open source solutions that cover every part of the business process.