Last active
August 27, 2019 14:44
-
-
Save Zillion01/5592408332ebf5c673c0e02005811aaf to your computer and use it in GitHub Desktop.
Generic PHP function for TYPO3 to generate routeEnhancer slugs for a table
This file contains 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
/** | |
* Generate slugs for a table | |
* | |
* Author: Jacco van der Post <[email protected]> | |
* | |
* @param string $table - The table to create the slugs in | |
* @param string $slugFieldName - The slug or path segment fieldname | |
* @param string $title - The field where the slug will be based on | |
* @param bool $forceOverwrite - Forces rewrite of all slug values | |
* | |
* @return void | |
*/ | |
private function generateSlugsForTable(string $table = null, string $slugFieldName = null, string $title = null, bool $forceOverwrite = false) { | |
if ($table && $slugFieldName && $title) { | |
$records = []; | |
$connectionPool = GeneralUtility::makeInstance(ConnectionPool::class); | |
$queryBuilder = $connectionPool->getQueryBuilderForTable($table); | |
if ($forceOverwrite) { | |
$queryBuilder | |
->update($table) | |
->set($slugFieldName, null) | |
->execute(); | |
$queryBuilder->resetQueryParts(); | |
$statement = $queryBuilder | |
->select('uid', $title) | |
->from($table) | |
->execute(); | |
} else { | |
// Select all records which have no slug value | |
$statement = $queryBuilder | |
->select('uid', $title) | |
->from($table) | |
->where( | |
$queryBuilder->expr()->isNull($slugFieldName)) | |
->orWhere ( | |
$queryBuilder->expr()->eq($slugFieldName, $queryBuilder->createNamedParameter('', \PDO::PARAM_STR)) | |
) | |
->execute(); | |
} | |
while ($row = $statement->fetch()) { | |
$records[] = $row; | |
} | |
// Get TYPO3 slughelper | |
$tcaConfiguration['generatorOptions']['fields'][] = $title; | |
$this->slugHelper = GeneralUtility::makeInstance(SlugHelper::class, $table, $slugFieldName, $tcaConfiguration); | |
foreach ($records as $record) { | |
// Make a slug | |
$trySlug = $this->slugHelper->sanitize(str_replace('/', '', $record[$title])); | |
$slug = $trySlug; | |
// Check if it's unique | |
$notUniqueSlug = true; | |
$slugCounter = 1; | |
while ($notUniqueSlug) { | |
$queryBuilder->resetQueryParts(); | |
$existingSlugs = $queryBuilder | |
->count('uid') | |
->from($table) | |
->where( | |
$queryBuilder->expr()->eq($slugFieldName, $queryBuilder->createNamedParameter($slug, \PDO::PARAM_STR)) | |
) | |
->execute() | |
->fetchColumn(0); | |
if ($existingSlugs > 0) { | |
$slug = $trySlug . '-' . $slugCounter; | |
$slugCounter++; | |
} else { | |
$notUniqueSlug = false; | |
// Store unique slug | |
$queryBuilder->resetQueryParts(); | |
$queryBuilder | |
->update($table) | |
->where( | |
$queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($record['uid'], \PDO::PARAM_INT)) | |
) | |
->set($slugFieldName, $slug) | |
->execute(); | |
} | |
}; | |
} | |
$this->logger->info(count($records) . ' slugs generated for table ' . $table); | |
} else { | |
$this->logger->error('Error on generating slugs.'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment