src/Sitemaps/ProductGenerator.php line 50

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\Bundle\EcommerceFrameworkBundle\Factory;
  16. use Pimcore\Bundle\EcommerceFrameworkBundle\IndexService\ProductList\ProductListInterface;
  17. use Pimcore\Model\DataObject\Car;
  18. use Pimcore\Sitemap\Element\AbstractElementGenerator;
  19. use Pimcore\Sitemap\Element\GeneratorContext;
  20. use Presta\SitemapBundle\Service\UrlContainerInterface;
  21. use Presta\SitemapBundle\Sitemap\Url\UrlConcrete;
  22. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  23. class ProductGenerator extends AbstractElementGenerator
  24. {
  25.     public function populate(UrlContainerInterface $urlContainerstring $section null)
  26.     {
  27.         if (null !== $section && $section !== 'cars') {
  28.             // do not add entries if section doesn't match
  29.             return;
  30.         }
  31.         $section 'cars';
  32.         $indexService Factory::getInstance()->getIndexService();
  33.         $productListing $indexService->getProductListForCurrentTenant();
  34.         $productListing->addCondition('carClass IS NOT NULL''carClass');
  35.         $productListing->setVariantMode(ProductListInterface::VARIANT_MODE_VARIANTS_ONLY);
  36.         $list $productListing;
  37.         // the context contains metadata for filters/processors
  38.         // it contains at least the url container, but you can add additional data
  39.         // with the params parameter
  40.         $context = new GeneratorContext($urlContainer$section, ['foo' => 'bar']);
  41.         /** @var Car $car */
  42.         foreach ($list as $car) {
  43.             // only add element if it is not filtered
  44.             if (!$this->canBeAdded($car$context)) {
  45.                 continue;
  46.             }
  47.             // use a link generator to generate an URL to the article
  48.             // you need to make sure the link generator generates an absolute url
  49.             $link $car->getClass()->getLinkGenerator()->generate($car, [
  50.                 'referenceType' => UrlGeneratorInterface::ABSOLUTE_URL
  51.             ]);
  52.             // create an entry for the sitemap
  53.             $url = new UrlConcrete($link);
  54.             // run url through processors
  55.             $url $this->process($url$car$context);
  56.             // processors can return null to exclude the url
  57.             if (null === $url) {
  58.                 continue;
  59.             }
  60.             // add the url to the container
  61.             $urlContainer->addUrl($url$section);
  62.         }
  63.     }
  64. }