SiteFactory.php:
<?php
declare(strict_types=1);
namespace Vendor\Extension\Factory;
use GuzzleHttp\Psr7\Uri;
use TYPO3\CMS\Core\Exception\SiteNotFoundException;
use TYPO3\CMS\Core\Http\Request;
use TYPO3\CMS\Core\Http\ServerRequest;
use TYPO3\CMS\Core\Routing\SiteMatcher;
use TYPO3\CMS\Core\Site\Entity\Site;
use TYPO3\CMS\Core\Site\SiteFinder;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
* Class SiteFactory
* with some examples how to get a site object anywhere
*/
class SiteFactory
{
/**
* Try to find a matching site from a given url (e.g. a deeplink)
*
* @param string $url
* @return Site
*/
public function getSiteFromUrl(string $url = 'https://domain.org/path/path/'): Site
{
$uri = GeneralUtility::makeInstance(Uri::class, $url);
$request = GeneralUtility::makeInstance(ServerRequest::class, $uri);
$matcher = GeneralUtility::makeInstance(SiteMatcher::class);
$routeResult = $matcher->matchRequest($request);
return $routeResult->getSite();
}
/**
* Get fitting site from a page identifier
*
* @param int $pageIdentifier
* @return Site
* @throws SiteNotFoundException
*/
public function getSiteFromPageIdentifier(int $pageIdentifier = 123): Site
{
$siteFinder = GeneralUtility::makeInstance(SiteFinder::class);
return $siteFinder->getSiteByPageId($pageIdentifier);
}
/**
* Get site from a root page identifier
*
* @param int $rootpageIdentifier
* @return Site
* @throws SiteNotFoundException
*/
public function getSiteByRootpageIdentifier(int $rootpageIdentifier = 123): Site
{
$siteFinder = GeneralUtility::makeInstance(SiteFinder::class);
return $siteFinder->getSiteByRootPageId($rootpageIdentifier);
}
/**
* Get first defined site configuration
*
* @return Site
*/
public function getFirstSite(): Site
{
$siteFinder = GeneralUtility::makeInstance(SiteFinder::class);
$sites = $siteFinder->getAllSites();
return array_values($sites)[0];
}
/**
* Get site from a request object
*
* @param Request $request
* @return Site
*/
public function getSiteFromRequest(Request $request): Site
{
return $request->getAttribute('site');
}
}
SiteHelper.php:
<?php
declare(strict_types=1);
namespace Vendor\Extension\Helper;
use TYPO3\CMS\Core\Site\Entity\Site;
use TYPO3\CMS\Core\Utility\ArrayUtility;
/**
* Class SiteHelper
* with some examples how to play with it
*/
class SiteHelper
{
/**
* Build a link to a page by given page identifier
*
* @param int $pageIdentifier
* @param Site $site
* @return string
*/
public function buildUrlToPage(int $pageIdentifier, Site $site): string
{
$uri = $site->getRouter()->generateUri($pageIdentifier);
return $uri->__toString();
}
/**
* Build a link to a page by given page identifier and any parameters (like typeNum)
*
* @param int $pageIdentifier
* @param array $arguments
* @param Site $site
* @return string
*/
public function buildUrlToPageWithArguments(int $pageIdentifier, array $arguments = ['type' => 98], Site $site): string
{
$uri = $site->getRouter()->generateUri($pageIdentifier, $arguments);
return $uri->__toString();
}
/**
* Get the root page identifier
*
* @param Site $site
* @return int
*/
public function getRootpageIdentifier(Site $site): int
{
return $site->getRootPageId();
}
/**
* Get any configuration from site object
*
* Example with your individual configuration in site yaml:
* rootPageId: 1
* base: <a href="https://domain.org" target="_blank" rel="noreferrer">domain.org</a>
* myProject:
* recordStorage: 123
*
* @param string $configurationPath
* @param Site $site
* @return string|int|array
*/
public function getConfigurationFromSite(Site $site, string $configurationPath = 'myProject.recordStorage')
{
$array = $site->getConfiguration();
return ArrayUtility::getValueByPath($array, $configurationPath, '.');
}
/**
* Get the default language identifier
*
* @param Site $site
* @return int
*/
public function getDefaultLanguageId(Site $site): int
{
return $site->getDefaultLanguage()->getLanguageId();
}
/**
* Get all language configurations (with all properties)
*
* @param Site $site
* @return array
*/
public function getAllLanguages(Site $site): array
{
return $site->getAllLanguages();
}
}