src/Sitemaps/NewsGenerator.php line 56

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\Localization\LocaleServiceInterface;
  16. use Pimcore\Model\DataObject\News;
  17. use Pimcore\Model\Document;
  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 NewsGenerator extends AbstractElementGenerator
  24. {
  25.     public function __construct(protected LocaleServiceInterface $localeService, array $filters = [], array $processors = [])
  26.     {
  27.         parent::__construct($filters$processors);
  28.     }
  29.     public function populate(UrlContainerInterface $urlContainerstring $section null)
  30.     {
  31.         if (null !== $section && $section !== 'news') {
  32.             // do not add entries if section doesn't match
  33.             return;
  34.         }
  35.         $section 'news';
  36.         $list = new News\Listing();
  37.         $list->setOrderKey('date');
  38.         $list->setOrder('DESC');
  39.         // the context contains metadata for filters/processors
  40.         // it contains at least the url container, but you can add additional data
  41.         // with the params parameter
  42.         $context = new GeneratorContext($urlContainer$section, ['foo' => 'bar']);
  43.         //change locale as per multilingual setup
  44.         $this->localeService->setLocale('en');
  45.         /** @var News $newsArticle */
  46.         foreach ($list as $newsArticle) {
  47.             // only add element if it is not filtered
  48.             if (!$this->canBeAdded($newsArticle$context)) {
  49.                 continue;
  50.             }
  51.             // use a link generator to generate an URL to the article
  52.             // you need to make sure the link generator generates an absolute url
  53.             $link $newsArticle->getClass()->getLinkGenerator()->generate($newsArticle, [
  54.                 'referenceType' => UrlGeneratorInterface::ABSOLUTE_URL,
  55.                 'document' => Document::getByPath('/en/News')
  56.             ]);
  57.             // create an entry for the sitemap
  58.             $url = new UrlConcrete($link);
  59.             // run url through processors
  60.             $url $this->process($url$newsArticle$context);
  61.             // processors can return null to exclude the url
  62.             if (null === $url) {
  63.                 continue;
  64.             }
  65.             // add the url to the container
  66.             $urlContainer->addUrl($url$section);
  67.         }
  68.     }
  69. }