Task #91663
closedPersistedAliasMapper can only resolve with fixed field "uid"
0%
Description
I recently had an issue while migrating from RealURL to the new TYPO3 url handling. As RealURL has an option to resolve using any fieldname (setting "id_field"), the PersistedAliasMapper only supports the field "uid". In most cases this should be enough. In my case I had to create a new AliasMapper:
<?php
namespace VENDOR\Project\Routing\Aspect;
/**
* Class PersistedAliasMapper.
*/
class PersistedAliasMapper extends \TYPO3\CMS\Core\Routing\Aspect\PersistedAliasMapper
{
/**
* @var string
*/
protected $idFieldName;
public function __construct(array $settings)
{
$idFieldName = $settings['idFieldName'] ?? 'uid';
if (!is_string($idFieldName)) {
throw new \InvalidArgumentException(
'idFieldName must be string',
1592311617
);
}
$this->idFieldName = $idFieldName;
parent::__construct($settings);
}
public function resolve(string $value): ?string
{
$value = $this->routeValuePrefix . $this->purgeRouteValuePrefix($value);
$result = $this->findByRouteFieldValue($value);
if ($result[$this->languageParentFieldName] ?? null > 0) {
return (string)$result[$this->languageParentFieldName];
}
if (isset($result[$this->idFieldName])) {
return (string)$result[$this->idFieldName];
}
return null;
}
protected function buildPersistenceFieldNames(): array
{
return array_filter([
$this->idFieldName,
'pid',
$this->routeFieldName,
$this->languageFieldName,
$this->languageParentFieldName,
]);
}
protected function findByIdentifier(string $value): ?array
{
$queryBuilder = $this->createQueryBuilder();
$result = $queryBuilder
->select(...$this->persistenceFieldNames)
->where($queryBuilder->expr()->eq(
$this->idFieldName,
$queryBuilder->createNamedParameter($value, \PDO::PARAM_INT)
))
->execute()
->fetch();
return $result !== false ? $result : null;
}
}
As you can see I only changed the hard coded "uid"-field to a setting, which defaults to "uid". Maybe you want to apply this to the core.
Updated by S P over 4 years ago
I'll join this request. We have the same issue. We have a routing-enabled table that is not in TCA (its rows are fully autogenerated by custom PHP and not used in Extbase or whatever) and has neither the uid
nor pid
field.
I also extended the core aspect and overwrote all methods to fetch from the correct fields.
In our case the primary (id) field is also no int
, but a varchar
.
Updated by Oliver Hader about 2 years ago
- Sprint Focus set to On Location Sprint
Updated by Georg Ringer 4 months ago
- Status changed from New to Closed
hey!
thanks for the issue! I am closing it cause the PersistedAliasMapper is there to map from records which use TCA which is the default in TYPO3. If you do something on your own, please use a custom Mapper as you just did