diff --git a/config/bundles.php b/config/bundles.php index 680b50a4..0f4f4adc 100644 --- a/config/bundles.php +++ b/config/bundles.php @@ -2,4 +2,5 @@ return [ //Twig\Extra\TwigExtraBundle\TwigExtraBundle::class => ['all' => true], + Presta\SitemapBundle\PrestaSitemapBundle::class => ['all' => true], ]; diff --git a/config/config.yaml b/config/config.yaml index 130b8191..72eab852 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -20,6 +20,27 @@ pimcore: encoder_factories: Pimcore\Model\DataObject\Customer: cmf.security.user_password_encoder_factory + sitemaps: + generators: + app_news: + enabled: true + priority: 50 + generator_id: App\Sitemaps\NewsGenerator + app_cars: + enabled: true + priority: 49 + generator_id: App\Sitemaps\ProductGenerator + app_category: + enabled: true + priority: 48 + generator_id: App\Sitemaps\CategoryGenerator + + # Pimcore ships a default document tree generator which is enabled by default + # but you can easily disable it here. + pimcore_documents: + enabled: true + + #### TRANSLATIONS # translations: # case_insensitive: true diff --git a/config/routes/presta_sitemap.yaml b/config/routes/presta_sitemap.yaml new file mode 100644 index 00000000..17c3a15c --- /dev/null +++ b/config/routes/presta_sitemap.yaml @@ -0,0 +1,2 @@ +presta_sitemap: + resource: "@PrestaSitemapBundle/Resources/config/routing.yml" \ No newline at end of file diff --git a/config/services.yaml b/config/services.yaml index edf5054a..bf09f032 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -8,6 +8,11 @@ parameters: # use echo \Defuse\Crypto\Key::createNewRandomKey()->saveToAsciiSafeString(); to generate secret for data encryption app_encryption_secret: "ThisTokenIsNotSoSecretChangeIt" + + #this is necessary for CLI commands to get the base url, eg. sitemap dump + router.request_context.host: demo.pimcore.fun + router.request_context.scheme: https + # customize the full path to external executables # normally they are auto-detected by `which program` or auto-discovered in the configured path in # System Settings -> General -> Additional $PATH variable @@ -75,7 +80,26 @@ services: App\Form\RegistrationFormHandler: ~ App\Form\CarSubmitFormType: ~ + # --------------------------------------------------------- + # Processors + # --------------------------------------------------------- + App\Sitemaps\Processors\AbsoluteURLProcessor: ~ + # --------------------------------------------------------- + # Sitemaps + # --------------------------------------------------------- + App\Sitemaps\NewsGenerator: + arguments: + $processors: + - '@App\Sitemaps\Processors\AbsoluteURLProcessor' + App\Sitemaps\ProductGenerator: + arguments: + $processors: + - '@App\Sitemaps\Processors\AbsoluteURLProcessor' + App\Sitemaps\CategoryGenerator: + arguments: + $processors: + - '@App\Sitemaps\Processors\AbsoluteURLProcessor' # --------------------------------------------------------- # Misc Services diff --git a/src/Sitemaps/CategoryGenerator.php b/src/Sitemaps/CategoryGenerator.php new file mode 100644 index 00000000..af4a5072 --- /dev/null +++ b/src/Sitemaps/CategoryGenerator.php @@ -0,0 +1,76 @@ + 'de']); + + /** @var Category $category */ + foreach ($list as $category) { + // only add element if it is not filtered + if (!$this->canBeAdded($category, $context)) { + continue; + } + + // use a link generator to generate an URL to the article + // you need to make sure the link generator generates an absolute url + $link = $category->getClass()->getLinkGenerator()->generate($category, [ + 'referenceType' => UrlGeneratorInterface::ABSOLUTE_URL + ], true); + + // create an entry for the sitemap + $url = new UrlConcrete($link); + + // run url through processors + $url = $this->process($url, $category, $context); + + // processors can return null to exclude the url + if (null === $url) { + continue; + } + + // add the url to the container + $urlContainer->addUrl($url, $section); + } + } +} diff --git a/src/Sitemaps/NewsGenerator.php b/src/Sitemaps/NewsGenerator.php new file mode 100644 index 00000000..8b420fce --- /dev/null +++ b/src/Sitemaps/NewsGenerator.php @@ -0,0 +1,84 @@ +setOrderKey('date'); + $list->setOrder('DESC'); + + // the context contains metadata for filters/processors + // it contains at least the url container, but you can add additional data + // with the params parameter + $context = new GeneratorContext($urlContainer, $section, ['foo' => 'bar']); + + //change locale as per multilingual setup + $this->localeService->setLocale('en'); + + /** @var News $newsArticle */ + foreach ($list as $newsArticle) { + // only add element if it is not filtered + if (!$this->canBeAdded($newsArticle, $context)) { + continue; + } + + // use a link generator to generate an URL to the article + // you need to make sure the link generator generates an absolute url + $link = $newsArticle->getClass()->getLinkGenerator()->generate($newsArticle, [ + 'referenceType' => UrlGeneratorInterface::ABSOLUTE_URL, + 'document' => Document::getByPath('/en/News') + ]); + + // create an entry for the sitemap + $url = new UrlConcrete($link); + + // run url through processors + $url = $this->process($url, $newsArticle, $context); + + // processors can return null to exclude the url + if (null === $url) { + continue; + } + + // add the url to the container + $urlContainer->addUrl($url, $section); + } + } +} diff --git a/src/Sitemaps/Processors/AbsoluteURLProcessor.php b/src/Sitemaps/Processors/AbsoluteURLProcessor.php new file mode 100644 index 00000000..7e01639a --- /dev/null +++ b/src/Sitemaps/Processors/AbsoluteURLProcessor.php @@ -0,0 +1,46 @@ +urlGenerator = $urlGenerator; + } + + public function process(Url $url, ElementInterface $element, GeneratorContextInterface $context) + { + $path = $this->urlGenerator->generateUrl($url->getLoc()); + $url = new UrlConcrete($path); + + return $url; + } +} diff --git a/src/Sitemaps/ProductGenerator.php b/src/Sitemaps/ProductGenerator.php new file mode 100644 index 00000000..9f488169 --- /dev/null +++ b/src/Sitemaps/ProductGenerator.php @@ -0,0 +1,78 @@ +getIndexService(); + $productListing = $indexService->getProductListForCurrentTenant(); + $productListing->addCondition("carClass IS NOT NULL", 'carClass'); + $productListing->setVariantMode(ProductListInterface::VARIANT_MODE_VARIANTS_ONLY); + $list = $productListing; + + // the context contains metadata for filters/processors + // it contains at least the url container, but you can add additional data + // with the params parameter + $context = new GeneratorContext($urlContainer, $section, ['foo' => 'bar']); + + /** @var Car $car */ + foreach ($list as $car) { + // only add element if it is not filtered + if (!$this->canBeAdded($car, $context)) { + continue; + } + + // use a link generator to generate an URL to the article + // you need to make sure the link generator generates an absolute url + $link = $car->getClass()->getLinkGenerator()->generate($car, [ + 'referenceType' => UrlGeneratorInterface::ABSOLUTE_URL + ]); + + // create an entry for the sitemap + $url = new UrlConcrete($link); + + // run url through processors + $url = $this->process($url, $car, $context); + + // processors can return null to exclude the url + if (null === $url) { + continue; + } + + // add the url to the container + $urlContainer->addUrl($url, $section); + } + } +}