findMatchingRedirect returning NULL for existing entries.

Created on 19 March 2024, 9 months ago

Problem/Motivation

I am trying to use this to programmatically create redirects that don't exist, so I execute the following code:

<?php
use Drupal\redirect\Entity\Redirect as redirectEntity;
use Drupal\redirect\RedirectRepository;

//... other stuff

$allLimitingInterfaces = $limitingInterfacesQuery->execute()->fetchAll();

//add redirects for all old style queries
$redirectRepository = \Drupal::service('redirect.repository');
foreach($allLimitingInterfaces as $LI){
  if(is_null($redirectRepository->findMatchingRedirect('/tgif/limiting', ['interface' => $LI->SearchQueryString],Language::LANGCODE_NOT_SPECIFIED)))
  {
    $redirect = redirectEntity::create();
    $redirect->setSource('tgif/limiting', ['interface' => $LI->SearchQueryString]);
    $redirect->setRedirect(Url::fromRoute('msul_tgif_star_xml_client.limiting_interface',['interfaceName'=>$LI->SearchQueryString])->toString());
    $redirect->setLanguage = Language::LANGCODE_NOT_SPECIFIED;
    $redirect->setStatusCode('301');
    $redirect->save();
  }
} 

The first time through, everything seems to work, the redirects are created and they work; but on subsequent executions, the findMatchingRedirect returns null even for all the redirects that were created in the previous run, thus causing an error when it tries to recreate an existing redirect.

Looking in the DB, it appears that the hash generated when the entity is created when does not match the hash generated within the findMatchingRedirect method, so the query in /src/RedirectRepository.php line 81 is returning null.

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

πŸ› Bug report
Status

Active

Version

1.9

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States kwfinken Lansing, MI

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024