Project

General

Profile

Task #91663

Updated by Sven Erens over 4 years ago

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"), ("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: 

 <pre><code class="php"> 
 <?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; 
     } 
 } 
 </code></pre> 

 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.

Back