Create slugs in TYPO3 via PHP

Create slugs in TYPO3 via PHP

Since TYPO3 9, slugs and path segments have found their way into TYPO3. In contrast to Realurl or CoolURI, pages and data records are now assigned a unique URL in the frontend. So that the whole thing works properly, there are fields such as pages.slug or tx_news_domain_model_news.path_segment. Using the following example, we will show you how you can generate unique values using PHP.

If the generated slugs are not quite correct after a TYPO3 update and have to be recreated, this can be done easily using a command, for example. But there are also other applications for creating slugs, such as creating data records in the frontend, which then also require a valid entry in table.path_segment at the end.

CreateSlug.php as an example service class for generating a new and unique slug value. In the example, the table name is hidden behind 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(); } }

The associated DatabaseUtility.php file could look like this:

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

Last but not least a TcaUtility.php class:

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