For coders TYPO3 Tech Corner

[PHP] What can you do with the SiteConfiguration in TYPO3?

[PHP] What can you do with the SiteConfiguration in TYPO3?

With the help of the new SiteConfiguration in TYPO3 9 or newer, many nice things can be done. So you can simply generate a URL using a Page ID (with or without arguments), or you can simply get the RootPageId or language. Examples of how to get the SiteConfiguration and what you can do with it can be seen in the following example.

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(); } }

"Code faster, look at the time" - does this sound familiar to you?

How about time and respect for code quality? Working in a team? Automated tests?

Join us

SQL: Show all tables sorted by size in descending order

Lately I've been using the SQL command more often to find out which tables in the TYPO3 database are the largest. I've published the snippet once.

Go to news

TYPO3 12 with CKEditor 5: Styles in a single selection

If you set a link in the RTE in TYPO3, you may have to choose between different link classes, for example to create buttons in the frontend. What's new in TYPO3 12 is that you can select not just one...

Go to news

Null-Safe Operator in the TYPO3 area

With the introduction of PHP8, problems with undefined arrays or variables in general can arise in many places. Here are a few examples and simple solutions.

Go to news

Delete the first/last lines of a (SQL) file

There isn't much to say about the following commands. Sometimes it can be useful to delete the first (or last) X lines from a file. And if the file is too large to open with a conventional program, a...

Go to news

b13/container: Add and modify child elements in edit view

Unlike gridelements, you cannot manage the child elements in the B13 Container extension when you open the container in the editing view. I would be happy to show you how you can quickly install this...

Go to news

Menu comparison: Numbers, numbers, numbers

Go to news