Naderio
Naderio

Reputation: 1419

TYPO3 9.5 LTS - Automatic (re)generation of URL Segments?

I have deleted all Slugs in my DB in hope that they regenerates automatically - but they dont!

Is there any way to trigger the regeneration? When upgrading from TYPO3 < 9 the get initially generated - but how?

Thanks for helping :)

Upvotes: 4

Views: 7126

Answers (6)

JKB
JKB

Reputation: 510

This one populates all empty slugs for a given table (e.g. after importing records from foreign sources into a custom extension):

https://www.typo3tiger.de/blog/post/typo3-extension-slugs-automatisiert-generieren.html

Upvotes: 0

alloyatomiklight
alloyatomiklight

Reputation: 21

try this

public static function setPageSlug($uid) {
        $fieldConfig = $GLOBALS['TCA']['tablename']['columns']['slug_field_name']['config'];
        $slugHelper = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\DataHandling\SlugHelper::class, 'tablename', 'slug_field_name', $fieldConfig);

        $connection = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Database\ConnectionPool::class)->getConnectionForTable('tablename');
        $queryBuilder = $connection->createQueryBuilder();

        $queryBuilder->getRestrictions()->removeAll()->add(\TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction::class));
        $statement = $queryBuilder->select('*')->from('tablename')->where(
        $queryBuilder->expr()->eq('uid', $uid)
        )->execute();

        $record = $statement->fetch();

        $slug = $slugHelper->generate($record, $record['pid']);

        // Update
        $queryBuilder = $connection->createQueryBuilder();
        $queryBuilder->update('tablename')->where(
        $queryBuilder->expr()->eq('uid', $uid)
        )->set('slug_field_name', $slug)->execute();

        var_dump($slug);
        return $slug;

        }

tablename => name of the table with the slug field

slug_field_name => name of the corresponding slug field

Upvotes: 2

John Miller
John Miller

Reputation: 717

TYPO3 has an in-built mechanism for this operation.

Log in to the backend,

  1. Click on Upgrade menu under ADMIN TOOLS
  2. Click on Run Upgrade Wizard on Upgrade Wizard card
  3. Check for Introduce URL parts ("slugs") to all existing pages in the list and click on its wizard. If already executed, check for it under Wizards marked as done list and reactivate it by clicking its Mark undone button and remember to execute its wizard once it reappears in the wizard list at the top. If you encounter a message instead, it means you lack empty slug fields.

Upvotes: 1

Christian
Christian

Reputation: 56

You may also have a look a the "slug" extension which provides batch editing of slug fields. https://extensions.typo3.org/extension/slug/

Upvotes: 4

Mathias Brodala
Mathias Brodala

Reputation: 6460

You can go to Upgrade > Run Upgrade Wizard and mark the wizard Introduce URL parts ("slugs") to all existing pages as undone.

Afterwards you can run this wizard and have the slug field filled for all pages again. Notice that this wizard only processes pages with an empty slug field. If you want to have all existing pages processed execute an SQL query like this:

UPDATE `pages` SET `slug` = NULL;

Upvotes: 17

Naderio
Naderio

Reputation: 1419

The Answere by Mathias Brodala works exactly as I wanted.

I had to prepare the database before use the Wizard with: UPDATE `pages` SET `slug` = NULL where 1

This deletes all(!!!) Slugs so that they can be recreated by the Wizard.

The Wizard only shows up, when there are records with an empty slug in the table. All Slugs that are already set will be ignored by the Wizard. (i have tested it - nothing gets broken).

An easy and effective solution. Thanks again @Mathias Brodala

Upvotes: 1

Related Questions