Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto create redirects like the core slug behavior? #28

Open
eckonator opened this issue Oct 20, 2022 · 3 comments
Open

Auto create redirects like the core slug behavior? #28

eckonator opened this issue Oct 20, 2022 · 3 comments

Comments

@eckonator
Copy link

If I update many slugs with this extension, it is important from an SEO point of view that redirects (old slug / new slug) are automatically created analogous to the core behavior.

Can this feature be implemented? I have developed a solution myself, but I am not quite happy with it yet, because the domain for the redirects is not set.

Here is my patch file.

diff --git a/Classes/Utility/SlugUtility.php b/Classes/Utility/SlugUtility.php
index 3099542..df48cef 100644
--- a/Classes/Utility/SlugUtility.php
+++ b/Classes/Utility/SlugUtility.php
@@ -23,7 +23,7 @@ class SlugUtility
     protected $hasToBeUniqueInPid;
     protected $hasToBeUniqueInTable;
     protected $fieldNamesToShow;
-  
+
     /**
      * Instantiate the form protection before a simulated user is initialized.
      *
@@ -77,7 +77,7 @@ class SlugUtility
             $slug=$record[$this->slugFieldName];
         } else {
             $slug = $this->slugHelper->generate($record, $pid);
-        
+
             $state = RecordStateFactory::forName($this->table)->fromArray($record, $pid, $recordId);
             if ($this->hasToBeUniqueInSite && !$this->slugHelper->isUniqueInSite($slug, $state)) {
                 $slug = $this->slugHelper->buildSlugForUniqueInSite($slug, $state);
@@ -133,7 +133,7 @@ class SlugUtility
             /*
              */
         }
-    
+
         foreach ($this->fieldNamesToShow as $fieldName) {
             if ($entry['slugFieldValues'] &&  $record[$fieldName]) {
                 $entry['slugFieldValues'] .= ', ';
@@ -144,6 +144,10 @@ class SlugUtility
         return $entry;
     }
 
+    /**
+     * @throws NoSuchCacheException
+     * @throws Exception
+     */
     public function updateEntry($entry)
     {
         $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($this->table);
@@ -157,12 +161,45 @@ class SlugUtility
             )
             ->set($this->slugFieldName, $entry['newSlug'])
             ->execute();
+
+
+        $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('sys_redirect');
+        $queryBuilder = $connection->createQueryBuilder();
+        $queryBuilder->select('uid')
+            ->from('sys_redirect')
+            ->where(
+                $queryBuilder->expr()->eq(
+                    'source_path',
+                    $queryBuilder->createNamedParameter($entry['slug'])
+                )
+            );
+        $data = $queryBuilder->execute();
+        $foundRedirectUid = $data->fetchFirstColumn()[0];
+        if(is_null($foundRedirectUid)) {
+            $queryBuilder
+                ->insert('sys_redirect')
+                ->values( [
+                    'pid' => 0,
+                    'source_path' => $entry['slug'],
+                    'target' => $entry['newSlug'],
+                    'target_statuscode' => 301
+                ])->execute();
+        } else {
+            $queryBuilder->update('sys_redirect')
+                ->where(
+                    $queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter((int)$foundRedirectUid))
+                )
+                ->set('target', $entry['newSlug'])
+                ->set('target_statuscode', 301)
+                ->execute();
+        }
+
         // Delete runtime Cache for rootline -> slug generate
         $runtimeCache = GeneralUtility::makeInstance(CacheManager::class)->getCache('cache_runtime');
         $runtimeCache->set('backendUtilityPageForRootLine', []);
         $runtimeCache->set('backendUtilityBeGetRootLine', []);
     }
-  
+
     protected function getLiveVersionPid($t3ver_oid)
     {
         $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('pages');
@ursbraem
Copy link

I was also looking for that

@ursbraem
Copy link

@eckonator Maybe it's better if you submit a PR?

@moe2k
Copy link

moe2k commented Oct 31, 2023

I also noticed the hard way that the extension doesnt auto-create the redirects.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants