Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/bundles.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

return [
//Twig\Extra\TwigExtraBundle\TwigExtraBundle::class => ['all' => true],
Presta\SitemapBundle\PrestaSitemapBundle::class => ['all' => true],
];
21 changes: 21 additions & 0 deletions config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions config/routes/presta_sitemap.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
presta_sitemap:
resource: "@PrestaSitemapBundle/Resources/config/routing.yml"
24 changes: 24 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
76 changes: 76 additions & 0 deletions src/Sitemaps/CategoryGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Enterprise License (PEL)
* 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
*/

namespace App\Sitemaps;

use Pimcore\Model\DataObject\Category;
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;

class CategoryGenerator extends AbstractElementGenerator
{
function __construct(array $filters = [], array $processors = [])
{
parent::__construct($filters, $processors);
}

public function populate(UrlContainerInterface $urlContainer, string $section = null)
{
if (null !== $section && $section !== 'category') {
// do not add entries if section doesn't match
return;
}

$section = 'category';

$list = new Category\Listing();

// 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, ['de' => '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);
}
}
}
84 changes: 84 additions & 0 deletions src/Sitemaps/NewsGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Enterprise License (PEL)
* 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
*/

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(protected LocaleServiceInterface $localeService, array $filters = [], array $processors = [])
{
parent::__construct($filters, $processors);
}

public function populate(UrlContainerInterface $urlContainer, string $section = null)
{
if (null !== $section && $section !== 'news') {
// do not add entries if section doesn't match
return;
}

$section = 'news';

$list = new News\Listing();
$list->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);
}
}
}
46 changes: 46 additions & 0 deletions src/Sitemaps/Processors/AbsoluteURLProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* 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 PCL
*/

namespace App\Sitemaps\Processors;

use Pimcore\Model\Element\ElementInterface;
use Pimcore\Sitemap\Element\GeneratorContextInterface;
use Pimcore\Sitemap\Element\ProcessorInterface;
use Pimcore\Sitemap\UrlGeneratorInterface;
use Presta\SitemapBundle\Sitemap\Url\Url;
use Presta\SitemapBundle\Sitemap\Url\UrlConcrete;

class AbsoluteURLProcessor implements ProcessorInterface
{
/**
* @var UrlGeneratorInterface
*/
private $urlGenerator;

public function __construct(UrlGeneratorInterface $urlGenerator)
{
$this->urlGenerator = $urlGenerator;
}

public function process(Url $url, ElementInterface $element, GeneratorContextInterface $context)
{
$path = $this->urlGenerator->generateUrl($url->getLoc());
$url = new UrlConcrete($path);

return $url;
}
}
78 changes: 78 additions & 0 deletions src/Sitemaps/ProductGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Enterprise License (PEL)
* 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
*/

namespace App\Sitemaps;

use Pimcore\Model\DataObject\Car;
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\Bundle\EcommerceFrameworkBundle\Factory;
use Pimcore\Bundle\EcommerceFrameworkBundle\IndexService\ProductList\ProductListInterface;


class ProductGenerator extends AbstractElementGenerator
{
public function populate(UrlContainerInterface $urlContainer, string $section = null)
{
if (null !== $section && $section !== 'cars') {
// do not add entries if section doesn't match
return;
}

$section = 'cars';

$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
// 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);
}
}
}