src/Sitemaps/CategoryGenerator.php line 32

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Enterprise License (PEL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  *  @license    http://www.pimcore.org/license     GPLv3 and PEL
  13.  */
  14. namespace App\Sitemaps;
  15. use Pimcore\Model\DataObject\Category;
  16. use Pimcore\Sitemap\Element\AbstractElementGenerator;
  17. use Pimcore\Sitemap\Element\GeneratorContext;
  18. use Presta\SitemapBundle\Service\UrlContainerInterface;
  19. use Presta\SitemapBundle\Sitemap\Url\UrlConcrete;
  20. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  21. class CategoryGenerator extends AbstractElementGenerator
  22. {
  23.     public function __construct(array $filters = [], array $processors = [])
  24.     {
  25.         parent::__construct($filters$processors);
  26.     }
  27.     public function populate(UrlContainerInterface $urlContainerstring $section null)
  28.     {
  29.         if (null !== $section && $section !== 'category') {
  30.             // do not add entries if section doesn't match
  31.             return;
  32.         }
  33.         $section 'category';
  34.         $list = new Category\Listing();
  35.         // the context contains metadata for filters/processors
  36.         // it contains at least the url container, but you can add additional data
  37.         // with the params parameter
  38.         $context = new GeneratorContext($urlContainer$section, ['de' => 'de']);
  39.         /** @var Category $category */
  40.         foreach ($list as $category) {
  41.             // only add element if it is not filtered
  42.             if (!$this->canBeAdded($category$context)) {
  43.                 continue;
  44.             }
  45.             // use a link generator to generate an URL to the article
  46.             // you need to make sure the link generator generates an absolute url
  47.             $link $category->getClass()->getLinkGenerator()->generate($category, [
  48.                 'referenceType' => UrlGeneratorInterface::ABSOLUTE_URL
  49.             ], true);
  50.             // create an entry for the sitemap
  51.             $url = new UrlConcrete($link);
  52.             // run url through processors
  53.             $url $this->process($url$category$context);
  54.             // processors can return null to exclude the url
  55.             if (null === $url) {
  56.                 continue;
  57.             }
  58.             // add the url to the container
  59.             $urlContainer->addUrl($url$section);
  60.         }
  61.     }
  62. }