-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
generate sitemap.xml per siteAccess && inject the multilingual and mu…
…ltinational site annotations tag.
- Loading branch information
1 parent
17637d6
commit bb3f0d4
Showing
11 changed files
with
528 additions
and
77 deletions.
There are no files selected for viewing
321 changes: 256 additions & 65 deletions
321
components/SEOBundle/bundle/Controller/SitemapController.php
Large diffs are not rendered by default.
Oops, something went wrong.
173 changes: 173 additions & 0 deletions
173
components/SEOBundle/bundle/Core/Helper/SiteMapHelper.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Novactive\Bundle\eZSEOBundle\Core\Helper; | ||
|
||
use Exception; | ||
use Ibexa\Contracts\Core\SiteAccess\ConfigResolverInterface; | ||
use Ibexa\Core\MVC\Symfony\Locale\LocaleConverter; | ||
use Ibexa\Core\MVC\Symfony\Routing\Generator\RouteReferenceGenerator; | ||
use Ibexa\Core\MVC\Symfony\Routing\UrlAliasRouter; | ||
use Ibexa\Core\MVC\Symfony\SiteAccess\SiteAccessServiceInterface; | ||
use Ibexa\Migration\Log\LoggerAwareTrait; | ||
use Psr\Log\LoggerInterface; | ||
use Psr\Log\NullLogger; | ||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | ||
use Symfony\Component\Routing\RouterInterface; | ||
use Throwable; | ||
|
||
class SiteMapHelper | ||
{ | ||
use LoggerAwareTrait; | ||
|
||
/** | ||
* @var ConfigResolverInterface | ||
*/ | ||
private $configResolver; | ||
|
||
/** | ||
* @var RouteReferenceGenerator | ||
*/ | ||
private $routeReferenceGenerator; | ||
|
||
/** | ||
* @var RouterInterface | ||
*/ | ||
private $router; | ||
|
||
/** | ||
* @var LocaleConverter | ||
*/ | ||
private $localeConverter; | ||
|
||
/** | ||
* @var SiteAccessServiceInterface | ||
*/ | ||
private $siteAccessService; | ||
|
||
public function __construct( | ||
ConfigResolverInterface $configResolver, | ||
SiteAccessServiceInterface $siteAccessService, | ||
RouteReferenceGenerator $routeReferenceGenerator, | ||
RouterInterface $router, | ||
LocaleConverter $localeConverter, | ||
?LoggerInterface $logger = null | ||
) { | ||
$this->configResolver = $configResolver; | ||
$this->siteAccessService = $siteAccessService; | ||
$this->routeReferenceGenerator = $routeReferenceGenerator; | ||
$this->router = $router; | ||
$this->localeConverter = $localeConverter; | ||
$this->logger = $logger ?? new NullLogger(); | ||
} | ||
|
||
public function generateLocationUrl( | ||
int $locationId, | ||
string $siteAccess = null | ||
): ?string { | ||
try { | ||
$routeParams['locationId'] = $locationId; | ||
$routeReference = $this->routeReferenceGenerator->generate( | ||
UrlAliasRouter::URL_ALIAS_ROUTE_NAME, | ||
$routeParams | ||
); | ||
if ($siteAccess) { | ||
$routeReference->set('siteaccess', $siteAccess); | ||
} | ||
$url = $this->router->generate( | ||
UrlAliasRouter::URL_ALIAS_ROUTE_NAME, | ||
$routeReference->getParams(), | ||
UrlGeneratorInterface::ABSOLUTE_URL | ||
); | ||
} catch (Throwable $exception) { | ||
$this->logger->error('NovaeZSEO: ' . $exception->getMessage()); | ||
$url = null; | ||
} | ||
|
||
return $url; | ||
} | ||
public function generateRouteUrl( | ||
string $routeName, | ||
string $siteAccess = null, | ||
array $parameters = [] | ||
): ?string { | ||
try { | ||
$url = $this->router->generate( | ||
$routeName, | ||
[...['siteaccess' => $siteAccess], ...$parameters], | ||
UrlGeneratorInterface::ABSOLUTE_URL | ||
); | ||
} catch (Throwable $exception) { | ||
$this->logger->error('NovaeZSEO: ' . $exception->getMessage()); | ||
$url = null; | ||
} | ||
|
||
return $url; | ||
} | ||
public function getSiteAccessesLocationIdLanguages(): array | ||
{ | ||
|
||
static $rootLocationLanguages = null; | ||
|
||
if (is_array($rootLocationLanguages)) { | ||
return $rootLocationLanguages; | ||
} | ||
|
||
$rootLocationLanguages = []; | ||
$siteAccesses = $this->configResolver->getParameter('translation_siteaccesses'); | ||
foreach ($siteAccesses as $siteAccess) { | ||
$rootLocationLanguages[$siteAccess] = [ | ||
'rootLocationId' => $this->getSiteAccessRootLocationId($siteAccess), | ||
'mainLanguage' => $this->getSiteAccessMainLanguage($siteAccess), | ||
'languages' => $this->getSiteAccessLanguages($siteAccess) | ||
]; | ||
} | ||
|
||
return $rootLocationLanguages; | ||
} | ||
|
||
public function getCurrentSiteAccess(): ?string | ||
{ | ||
return $this->siteAccessService->getCurrent()?->name; | ||
} | ||
|
||
public function getCurrentSiteAccessRootLocationId(): ?int | ||
{ | ||
return $this->getSiteAccessRootLocationId($this->getCurrentSiteAccess()); | ||
} | ||
public function getCurrentSiteAccessMainLanguage(): ?string | ||
{ | ||
return $this->getSiteAccessMainLanguage($this->getCurrentSiteAccess()); | ||
} | ||
|
||
public function getSiteAccessRootLocationId(string $siteAccess): ?int | ||
{ | ||
return $this->configResolver->getParameter('content.tree_root.location_id', null, $siteAccess); | ||
} | ||
|
||
public function getSiteAccessLanguages(string $siteAccess): array | ||
{ | ||
return (array) $this->configResolver->getParameter('languages', null, $siteAccess); | ||
} | ||
|
||
public function getSiteAccessMainLanguage(string $siteAccess): string | ||
{ | ||
$languages = $this->configResolver->getParameter('languages', null, $siteAccess); | ||
return array_shift($languages); | ||
} | ||
|
||
public function getHrefLang(string $languageCode): string | ||
{ | ||
return str_replace( | ||
'_', | ||
'-', | ||
($this->localeConverter->convertToPOSIX($languageCode) ?? '') | ||
); | ||
} | ||
|
||
public function logException(Exception $exception): void | ||
{ | ||
$this->logger?->error('NovaeZSEO: ' . $exception->getMessage()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
components/SEOBundle/bundle/Resources/config/ibexa_locale_conversion.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#list from https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes | ||
locale_conversion: | ||
bre-BT: br_BT | ||
rum-RO: ro_RO | ||
mlt-MT: mt_MT | ||
gle-IE: ga_IE | ||
est-EE: et_EE | ||
slv-SI: sl_SI | ||
sla-MK: sl_MK | ||
lav-LV: lv_LV | ||
lit-LT: lt_LT | ||
geo-GE: ka_GE | ||
per-IR: fa_IR | ||
bul-BG: bg_BG | ||
bel-BY: be_BY |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters