Created
September 25, 2023 13:41
-
-
Save christophlehmann/6c63ebf08943fae5b3cb068c2f67fa36 to your computer and use it in GitHub Desktop.
TYPO3: Slug generation for Extbase Objects
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace My\Site\Utility; | |
use TYPO3\CMS\Backend\Utility\BackendUtility; | |
use TYPO3\CMS\Core\Database\ConnectionPool; | |
use TYPO3\CMS\Core\DataHandling\Model\RecordStateFactory; | |
use TYPO3\CMS\Core\DataHandling\SlugHelper; | |
use TYPO3\CMS\Core\Utility\GeneralUtility; | |
use TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface; | |
use TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapFactory; | |
/** | |
* Generate and persist slugs for Entities. Meaned to be used in Extbase context after persisting an object. | |
* Example: | |
* | |
* $this->eventRepository->add($event); | |
* $persistenceManager = GeneralUtility::makeInstance(PersistenceManager::class); | |
* $persistenceManager->persistAll(); | |
* SlugUtility::generateAndPersistSlugForEntity($event); | |
* | |
*/ | |
class SlugUtility | |
{ | |
public static function generateAndPersistSlugForEntity(DomainObjectInterface $record): string | |
{ | |
$tableName = GeneralUtility::makeInstance(DataMapFactory::class) | |
->buildDataMap(get_class($record))->getTableName(); | |
foreach ($GLOBALS['TCA'][$tableName]['columns'] as $columnName => $columnConfiguration) { | |
if ($columnConfiguration['config']['type'] === 'slug') { | |
$slugFieldName = $columnName; | |
break; | |
} | |
} | |
if (!isset($slugFieldName)) { | |
throw new \Exception('No field of type slug found', 1684954165); | |
} | |
$row = BackendUtility::getRecord($tableName, $record->getUid()); | |
$slugHelper = GeneralUtility::makeInstance( | |
SlugHelper::class, | |
$tableName, | |
'slug', | |
$GLOBALS['TCA'][$tableName]['columns']['slug']['config'] | |
); | |
$slug = $slugHelper->generate($row, $record->getPid()); | |
$state = RecordStateFactory::forName($tableName)->fromArray( | |
$row, | |
$record->getPid(), | |
$record->getUid() | |
); | |
$evalInfo = GeneralUtility::trimExplode( | |
',', | |
$GLOBALS['TCA'][$tableName]['columns'][$slugFieldName]['config']['eval'], | |
true | |
); | |
if (in_array('uniqueInSite', $evalInfo)) { | |
$slug = $slugHelper->buildSlugForUniqueInSite($slug, $state); | |
} else if (in_array('uniqueInPid', $evalInfo)) { | |
$slug = $slugHelper->buildSlugForUniqueInPid($slug, $state); | |
} else if (in_array('unique', $evalInfo)) { | |
$slug = $slugHelper->buildSlugForUniqueInTable($slug, $state); | |
} | |
$connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($tableName); | |
$connection->update($tableName, [$slugFieldName => $slug], ['uid' => $record->getUid()]); | |
return $slug; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment