Slugs in TYPO3 per PHP erzeugen

Slugs in TYPO3 per PHP erzeugen

Seit TYPO3 9 haben Slugs und Path Segments Einzug in TYPO3 gehalten. Im Gegensatz zu Realurl oder CoolURI bekommen Seiten und Datensätze nun eindeutige URL im Frontend zugewiesen. Damit das Ganze sauber funktioniert, gibt es Felder wie pages.slug oder tx_news_domain_model_news.path_segment. Wie ihr eindeutige Werte per PHP erzeugen könnt, zeigen wir euch am nachfolgenden Beispiel.

Wenn die erzeugten Slugs nach einem TYPO3-Update nicht ganz stimmen und neu erzeugt werden müssen, lässt sich dies beispielsweise einfach per Command nachholen. Es gibt aber auch andere Anwendungsfälle für das Erzeugen von Slugs wie beispielsweise das Erstellen von Datensätzen im Frontend, die dann am Ende auch einen gültigen Eintrag in table.path_segment benötigen.

CreateSlug.php als Beispiel-Service-Klasse zum Erzeugen eines neuen und eindeutigen Slug Wertes. Der Tabellenname verbirgt sich in dem Beispiel hinter Event::TABLE_NAME.

<?php declare(strict_types = 1); namespace UniKn\UknCalendarize\Service; use TYPO3\CMS\Core\DataHandling\Model\RecordState; use TYPO3\CMS\Core\DataHandling\Model\RecordStateFactory; use TYPO3\CMS\Core\DataHandling\SlugHelper; use TYPO3\CMS\Core\Exception\SiteNotFoundException; use TYPO3\CMS\Core\Utility\GeneralUtility; use UniKn\UknCalendarize\Domain\Model\Event; use UniKn\UknCalendarize\Utility\DatabaseUtility; use UniKn\UknCalendarize\Utility\TcaUtility; /** * Class CreateSlug */ class CreateSlug { const SLUG_FIELDNAME = 'slug'; /** * @param array $row tx_extension_domain_model_anything.* * @param string $tableName tx_extension_domain_model_anything * @return void * @throws SiteNotFoundException */ public function recreateSlug(array $row, string $tableName): void { $slugHelper = GeneralUtility::makeInstance( SlugHelper::class, $tableName, self::SLUG_FIELDNAME, TcaUtility::getTcaOfField(self::SLUG_FIELDNAME, $tableName)['config'] ); $slug = $slugHelper->generate($row, $row['pid']); $slugValue = $slugHelper->buildSlugForUniqueInSite($slug, $this->getRecordState($row, $tableName)); $this->persistSlug($tableName, $slugValue); } /** * @param array $row tx_extension_domain_model_anything.* * @param string $tableName tx_extension_domain_model_anything * @return RecordState */ protected function getRecordState(array $row, string $tableName): RecordState { return GeneralUtility::makeInstance(RecordStateFactory::class, $tableName) ->fromArray($row); } /** * @param array $row tx_extension_domain_model_anything.* * @param string $tableName tx_extension_domain_model_anything * @param string $slugValue * @return void */ protected function persistSlug(array $row, string $tableName, string $slugValue) { $queryBuilder = DatabaseUtility::getQueryBuilderForTable($tableName); $queryBuilder ->update($tableName) ->where('uid=' . (int)$row['uid']) ->set(self::SLUG_FIELDNAME, $slugValue) ->execute(); } }

Die dazugehörige DatabaseUtility.php Datei könnte so aussehen:

<?php declare(strict_types=1); namespace UniKn\UknCalendarize\Utility; use Doctrine\DBAL\Driver\Exception as ExceptionDbalDriver; use Doctrine\DBAL\Exception as ExceptionDbal; use TYPO3\CMS\Core\Database\Connection; use TYPO3\CMS\Core\Database\ConnectionPool; use TYPO3\CMS\Core\Database\Query\QueryBuilder; use TYPO3\CMS\Core\Utility\GeneralUtility; /** * Class DatabaseUtility */ class DatabaseUtility { /** * Cache existing fields * * @var array */ protected static array $fieldsExisting = []; /** * @param string $fieldName * @param string $tableName * @return bool * @throws ExceptionDbal * @throws ExceptionDbalDriver */ public static function isFieldExistingInTable(string $fieldName, string $tableName): bool { $found = false; if (isset(self::$fieldsExisting[$tableName][$fieldName]) === false) { $connection = self::getConnectionForTable($tableName); $queryResult = $connection->executeQuery('describe ' . $tableName . ';')->fetchAllAssociative(); foreach ($queryResult as $fieldProperties) { if ($fieldProperties['Field'] === $fieldName) { $found = true; break; } } self::$fieldsExisting[$tableName][$fieldName] = $found; } else { $found = self::$fieldsExisting[$tableName][$fieldName]; } return $found; } /** * @param string $tableName * @param bool $removeRestrictions * @return QueryBuilder */ public static function getQueryBuilderForTable(string $tableName, bool $removeRestrictions = false): QueryBuilder { /** @var QueryBuilder $queryBuilder */ $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($tableName); if ($removeRestrictions === true) { $queryBuilder->getRestrictions()->removeAll(); } return $queryBuilder; } /** * @param string $tableName * @return Connection */ public static function getConnectionForTable(string $tableName): Connection { return GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($tableName); } }

TcaUtility.php

<?php declare(strict_types = 1); namespace UniKn\UknCalendarize\Utility; /** * Class TcaUtility */ class TcaUtility { /** * @param string $fieldName * @param string $tableName * @return array */ public static function getTcaOfField(string $fieldName, string $tableName): array { if (empty($GLOBALS['TCA'][$tableName]['columns'][$fieldName])) { throw new \LogicException('No TCA to field ' . $fieldName . ' and table ' . $tableName, 1570026984); } return (array)$GLOBALS['TCA'][$tableName]['columns'][$fieldName]; } }

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...

Zum Beitrag

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.

Zum Beitrag

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.

Zum Beitrag

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...

Zum Beitrag

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.

Zum Beitrag

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...

Zum Beitrag