Issue Task Closed

#91663PersistedAliasMapper can only resolve with fixed field "uid"

Sven Erens 06/17/20, 08:20 updated 08/03/24, 07:00

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.

Reviews 0

No change resolves or names this issue in its commit message, and no comment links one.

History 6

  1. Sven Erens changed description 06/17/20, 08:22

  2. S P commented 07/29/20, 08:57

    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.

  3. Oliver Hader changed Sprint Focus On Location Sprint 11/03/22, 14:02

  4. Benni Mack changed Sprint Focus On Location Sprint 08/02/23, 13:04

  5. Georg Ringer changed Sprint Focus Needs Decision 06/23/24, 14:08

  6. Georg Ringer commented and changed status New Closed 08/03/24, 07:00

    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