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

TYPO3: Finding unused files in fileadmin

Do you want to delete unused or orphaned files in fileadmin or another storage location? Unfortunately, there's no direct core functionality for this. But a small command in your site package can...

Go to news

TYPO3: Editors with individual user_upload folders

Perhaps you're familiar with this client requirement? Editors should be able to add videos using the "Add media by URL" button. But the files shouldn't be located in fileadmin/user_upload/, but rather...

Go to news

TYPO3: Finding pages in mixed mode

In TYPO3, Mixed Mode refers to translated pages that contain content only partially related to the corresponding content in the main language. This is indicated in the backend by an error message. But...

Go to news

Extbase Extensions: Think extensibility with data, site and language

Today, I have a small request for the TYPO3 extension authors out there: Make sure your extensions are extensible. This will also promote the distribution of the corresponding plugins.

Go to news

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