From df95ba20c5fec593a2abd77f686fa5ff5e73c376 Mon Sep 17 00:00:00 2001 From: Jia Jia Ji Date: Thu, 13 Jan 2022 19:40:58 +0100 Subject: [PATCH 1/6] WIP: initial commit for working example for sitemaps about news,category,cars. --- config/bundles.php | 1 + config/config.yaml | 21 +++++++ config/routes/presta_sitemap.yaml | 2 + config/services.yaml | 19 ++++++ src/Processors/AbsoluteURLProcessor.php | 49 ++++++++++++++++ src/Sitemaps/CategoryGenerator.php | 78 +++++++++++++++++++++++++ src/Sitemaps/NewsGenerator.php | 73 +++++++++++++++++++++++ src/Sitemaps/ProductGenerator.php | 71 ++++++++++++++++++++++ 8 files changed, 314 insertions(+) create mode 100644 config/routes/presta_sitemap.yaml create mode 100644 src/Processors/AbsoluteURLProcessor.php create mode 100644 src/Sitemaps/CategoryGenerator.php create mode 100644 src/Sitemaps/NewsGenerator.php create mode 100644 src/Sitemaps/ProductGenerator.php 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..817e9dc5 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -75,7 +75,26 @@ services: App\Form\RegistrationFormHandler: ~ App\Form\CarSubmitFormType: ~ + # --------------------------------------------------------- + # Processors + # --------------------------------------------------------- + App\Processors\AbsoluteURLProcessor: ~ + # --------------------------------------------------------- + # Sitemaps + # --------------------------------------------------------- + App\Sitemaps\NewsGenerator: + arguments: + $processors: + - '@Pimcore\Sitemap\Element\Processor\AbsoluteURLProcessor' + App\Sitemaps\ProductGenerator: + arguments: + $processors: + - '@Pimcore\Sitemap\Element\Processor\AbsoluteURLProcessor' + App\Sitemaps\CategoryGenerator: + arguments: + $processors: + - '@Pimcore\Sitemap\Element\Processor\AbsoluteURLProcessor' # --------------------------------------------------------- # Misc Services diff --git a/src/Processors/AbsoluteURLProcessor.php b/src/Processors/AbsoluteURLProcessor.php new file mode 100644 index 00000000..e5079200 --- /dev/null +++ b/src/Processors/AbsoluteURLProcessor.php @@ -0,0 +1,49 @@ +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/CategoryGenerator.php b/src/Sitemaps/CategoryGenerator.php new file mode 100644 index 00000000..1a6cb06d --- /dev/null +++ b/src/Sitemaps/CategoryGenerator.php @@ -0,0 +1,78 @@ + '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); + + $path = $this->urlGenerator->generateUrl($link); + + // create an entry for the sitemap + $url = new UrlConcrete($path); + + // 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..5434328d --- /dev/null +++ b/src/Sitemaps/NewsGenerator.php @@ -0,0 +1,73 @@ +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']); + + /** @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 + ]); + + // 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/ProductGenerator.php b/src/Sitemaps/ProductGenerator.php new file mode 100644 index 00000000..eb403cd9 --- /dev/null +++ b/src/Sitemaps/ProductGenerator.php @@ -0,0 +1,71 @@ + '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); + } + } +} From e4d20444fe19f2c066ea7596a0eb2247aa3045ca Mon Sep 17 00:00:00 2001 From: Jia Jia Ji Date: Fri, 14 Jan 2022 19:49:26 +0100 Subject: [PATCH 2/6] WIP: fix for category sitemap, loading data by e-commerce productlisting for cars sitemap --- src/Sitemaps/CategoryGenerator.php | 4 +--- src/Sitemaps/ProductGenerator.php | 13 ++++++++++--- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/Sitemaps/CategoryGenerator.php b/src/Sitemaps/CategoryGenerator.php index 1a6cb06d..af4a5072 100644 --- a/src/Sitemaps/CategoryGenerator.php +++ b/src/Sitemaps/CategoryGenerator.php @@ -58,10 +58,8 @@ public function populate(UrlContainerInterface $urlContainer, string $section = 'referenceType' => UrlGeneratorInterface::ABSOLUTE_URL ], true); - $path = $this->urlGenerator->generateUrl($link); - // create an entry for the sitemap - $url = new UrlConcrete($path); + $url = new UrlConcrete($link); // run url through processors $url = $this->process($url, $category, $context); diff --git a/src/Sitemaps/ProductGenerator.php b/src/Sitemaps/ProductGenerator.php index eb403cd9..9f488169 100644 --- a/src/Sitemaps/ProductGenerator.php +++ b/src/Sitemaps/ProductGenerator.php @@ -9,8 +9,8 @@ * Full copyright and license information is available in * LICENSE.md which is distributed with this source code. * - * @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org) - * @license http://www.pimcore.org/license GPLv3 and PEL + * @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org) + * @license http://www.pimcore.org/license GPLv3 and PEL */ namespace App\Sitemaps; @@ -21,6 +21,9 @@ use Presta\SitemapBundle\Service\UrlContainerInterface; use Presta\SitemapBundle\Sitemap\Url\UrlConcrete; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; +use Pimcore\Bundle\EcommerceFrameworkBundle\Factory; +use Pimcore\Bundle\EcommerceFrameworkBundle\IndexService\ProductList\ProductListInterface; + class ProductGenerator extends AbstractElementGenerator { @@ -33,7 +36,11 @@ public function populate(UrlContainerInterface $urlContainer, string $section = $section = 'cars'; - $list = new Car\Listing(); + $indexService = Factory::getInstance()->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 From dfd4be418547ac320b45a7e0864bfc9a27a72e54 Mon Sep 17 00:00:00 2001 From: Jia Jia Ji Date: Thu, 20 Jan 2022 15:54:46 +0100 Subject: [PATCH 3/6] fix wrong namespace and service path --- config/services.yaml | 6 +++--- src/Processors/AbsoluteURLProcessor.php | 5 +---- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/config/services.yaml b/config/services.yaml index 817e9dc5..f456a634 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -86,15 +86,15 @@ services: App\Sitemaps\NewsGenerator: arguments: $processors: - - '@Pimcore\Sitemap\Element\Processor\AbsoluteURLProcessor' + - '@App\Processors\AbsoluteURLProcessor' App\Sitemaps\ProductGenerator: arguments: $processors: - - '@Pimcore\Sitemap\Element\Processor\AbsoluteURLProcessor' + - '@App\Processors\AbsoluteURLProcessor' App\Sitemaps\CategoryGenerator: arguments: $processors: - - '@Pimcore\Sitemap\Element\Processor\AbsoluteURLProcessor' + - '@App\Processors\AbsoluteURLProcessor' # --------------------------------------------------------- # Misc Services diff --git a/src/Processors/AbsoluteURLProcessor.php b/src/Processors/AbsoluteURLProcessor.php index e5079200..68ceea0f 100644 --- a/src/Processors/AbsoluteURLProcessor.php +++ b/src/Processors/AbsoluteURLProcessor.php @@ -15,7 +15,7 @@ * @license http://www.pimcore.org/license GPLv3 and PCL */ -namespace Pimcore\Sitemap\Element\Processor; +namespace App\Processors; use Pimcore\Model\Element\ElementInterface; use Pimcore\Sitemap\Element\GeneratorContextInterface; @@ -24,9 +24,6 @@ use Presta\SitemapBundle\Sitemap\Url\Url; use Presta\SitemapBundle\Sitemap\Url\UrlConcrete; -/** - * Adds modification date from element modification date. - */ class AbsoluteURLProcessor implements ProcessorInterface { /** From d03ac99b884012c266d62a051759a8843aea52d6 Mon Sep 17 00:00:00 2001 From: Jia Jia Ji Date: Thu, 20 Jan 2022 17:57:35 +0100 Subject: [PATCH 4/6] wip: small refactor, moved processors under sitemaps sitemap dump by CLI works beside "news" section (only works if returning null on line 38 of documentresolver) --- config/services.yaml | 13 +++++++++---- .../Processors/AbsoluteURLProcessor.php | 2 +- src/Website/LinkGenerator/NewsLinkGenerator.php | 1 + 3 files changed, 11 insertions(+), 5 deletions(-) rename src/{ => Sitemaps}/Processors/AbsoluteURLProcessor.php (97%) diff --git a/config/services.yaml b/config/services.yaml index f456a634..07803360 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: localhost + router.request_context.scheme: http + # 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 @@ -78,7 +83,7 @@ services: # --------------------------------------------------------- # Processors # --------------------------------------------------------- - App\Processors\AbsoluteURLProcessor: ~ + App\Sitemaps\Processors\AbsoluteURLProcessor: ~ # --------------------------------------------------------- # Sitemaps @@ -86,15 +91,15 @@ services: App\Sitemaps\NewsGenerator: arguments: $processors: - - '@App\Processors\AbsoluteURLProcessor' + - '@App\Sitemaps\Processors\AbsoluteURLProcessor' App\Sitemaps\ProductGenerator: arguments: $processors: - - '@App\Processors\AbsoluteURLProcessor' + - '@App\Sitemaps\Processors\AbsoluteURLProcessor' App\Sitemaps\CategoryGenerator: arguments: $processors: - - '@App\Processors\AbsoluteURLProcessor' + - '@App\Sitemaps\Processors\AbsoluteURLProcessor' # --------------------------------------------------------- # Misc Services diff --git a/src/Processors/AbsoluteURLProcessor.php b/src/Sitemaps/Processors/AbsoluteURLProcessor.php similarity index 97% rename from src/Processors/AbsoluteURLProcessor.php rename to src/Sitemaps/Processors/AbsoluteURLProcessor.php index 68ceea0f..7e01639a 100644 --- a/src/Processors/AbsoluteURLProcessor.php +++ b/src/Sitemaps/Processors/AbsoluteURLProcessor.php @@ -15,7 +15,7 @@ * @license http://www.pimcore.org/license GPLv3 and PCL */ -namespace App\Processors; +namespace App\Sitemaps\Processors; use Pimcore\Model\Element\ElementInterface; use Pimcore\Sitemap\Element\GeneratorContextInterface; diff --git a/src/Website/LinkGenerator/NewsLinkGenerator.php b/src/Website/LinkGenerator/NewsLinkGenerator.php index 507fd733..06762af3 100644 --- a/src/Website/LinkGenerator/NewsLinkGenerator.php +++ b/src/Website/LinkGenerator/NewsLinkGenerator.php @@ -82,6 +82,7 @@ public function generate(Concrete $object, array $params = []): string if (isset($params['document']) && $params['document'] instanceof Document) { $document = $params['document']; } else { + # TODO: line 38 $request = $this->getCurrentRequest(); in DocumentResolver causes problems to Sitemap genearation $document = $this->documentResolver->getDocument($this->requestStack->getCurrentRequest()); } From f20980acfcfef877573885536175063a39713040 Mon Sep 17 00:00:00 2001 From: Jia Jia Ji Date: Fri, 21 Jan 2022 12:33:05 +0100 Subject: [PATCH 5/6] fixes cli dump locale problem minor tweaks and cleanup --- config/services.yaml | 4 ++-- src/Sitemaps/NewsGenerator.php | 11 ++++++++++- src/Website/LinkGenerator/NewsLinkGenerator.php | 1 - 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/config/services.yaml b/config/services.yaml index 07803360..bf09f032 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -10,8 +10,8 @@ parameters: #this is necessary for CLI commands to get the base url, eg. sitemap dump - router.request_context.host: localhost - router.request_context.scheme: http + 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 diff --git a/src/Sitemaps/NewsGenerator.php b/src/Sitemaps/NewsGenerator.php index 5434328d..e2bb05e3 100644 --- a/src/Sitemaps/NewsGenerator.php +++ b/src/Sitemaps/NewsGenerator.php @@ -16,14 +16,22 @@ namespace App\Sitemaps; use Pimcore\Model\DataObject\News; +use Pimcore\Model\Document; use Pimcore\Sitemap\Element\AbstractElementGenerator; use Pimcore\Sitemap\Element\GeneratorContext; use Presta\SitemapBundle\Service\UrlContainerInterface; use Presta\SitemapBundle\Sitemap\Url\UrlConcrete; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; +use Pimcore\Localization\LocaleServiceInterface; class NewsGenerator extends AbstractElementGenerator { + public function __construct(LocaleServiceInterface $localeService, array $filters = [], array $processors = []) + { + parent::__construct($filters, $processors); + $localeService->setLocale('en'); + } + public function populate(UrlContainerInterface $urlContainer, string $section = null) { if (null !== $section && $section !== 'news') { @@ -52,7 +60,8 @@ public function populate(UrlContainerInterface $urlContainer, string $section = // 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 + 'referenceType' => UrlGeneratorInterface::ABSOLUTE_URL, + 'document' => Document::getByPath('/en/News') ]); // create an entry for the sitemap diff --git a/src/Website/LinkGenerator/NewsLinkGenerator.php b/src/Website/LinkGenerator/NewsLinkGenerator.php index 06762af3..507fd733 100644 --- a/src/Website/LinkGenerator/NewsLinkGenerator.php +++ b/src/Website/LinkGenerator/NewsLinkGenerator.php @@ -82,7 +82,6 @@ public function generate(Concrete $object, array $params = []): string if (isset($params['document']) && $params['document'] instanceof Document) { $document = $params['document']; } else { - # TODO: line 38 $request = $this->getCurrentRequest(); in DocumentResolver causes problems to Sitemap genearation $document = $this->documentResolver->getDocument($this->requestStack->getCurrentRequest()); } From 3f67156c350e2d72bfc4a5f890be2be87423a6df Mon Sep 17 00:00:00 2001 From: dpahuja Date: Fri, 21 Jan 2022 13:21:44 +0100 Subject: [PATCH 6/6] Sitemap working example - locale refactoring --- src/Sitemaps/NewsGenerator.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Sitemaps/NewsGenerator.php b/src/Sitemaps/NewsGenerator.php index e2bb05e3..8b420fce 100644 --- a/src/Sitemaps/NewsGenerator.php +++ b/src/Sitemaps/NewsGenerator.php @@ -26,10 +26,9 @@ class NewsGenerator extends AbstractElementGenerator { - public function __construct(LocaleServiceInterface $localeService, array $filters = [], array $processors = []) + public function __construct(protected LocaleServiceInterface $localeService, array $filters = [], array $processors = []) { parent::__construct($filters, $processors); - $localeService->setLocale('en'); } public function populate(UrlContainerInterface $urlContainer, string $section = null) @@ -50,6 +49,9 @@ public function populate(UrlContainerInterface $urlContainer, string $section = // 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