Slugs / URL parts

New in version 12.0

The main purpose of this type is to define parts of a URL path to generate and resolve URLs.

Example: A basic slug field

This example limits the length of the slug to 50 characters. It takes only the field input_1 into account for generating the slug.

EXT:styleguide/Configuration/TCA/tx_styleguide_elements_slugs.php
[
    'columns' => [
        'slug_2' => [
            'label' => 'slug_2',
            'config' => [
                'type' => 'slug',
                'size' => 50,
                'generatorOptions' => [
                    'fields' => [
                        'input_1',
                    ],
                    'fieldSeparator' => '/',
                    'prefixParentPageSlug' => true,
                ],
                'fallbackCharacter' => '-',
                'eval' => 'uniqueInSite',
                'default' => '',
            ],
        ],
    ],
]
Copied!

Example: A slug field with prefix hook

This example uses a custom slug prefix hook via config['appearance']['prefix'] to adapt the displayed prefix. It takes the two fields input_1 and input_2 into account for generating the slug.

EXT:styleguide/Configuration/TCA/tx_styleguide_elements_slugs.php
[
    'columns' => [
        'slug_1' => [
            'label' => 'slug_1',
            'description' => 'field description',
            'config' => [
                'type' => 'slug',
                'generatorOptions' => [
                    'fields' => [
                        'input_1',
                        'input_2',
                    ],
                    'fieldSeparator' => '/',
                    'prefixParentPageSlug' => true,
                    'replacements' => [
                        '/' => '',
                    ],
                ],
                'appearance' => [
                    'prefix' => 'TYPO3\\CMS\\Styleguide\\UserFunctions\\FormEngine\\SlugPrefix->getPrefix',
                ],
                'fallbackCharacter' => '-',
                'eval' => 'uniqueInSite',
                'default' => '',
            ],
        ],
    ],
]
Copied!

Properties of the TCA column type slug

Name Type Scope
array Display
userFunction Display
string (list of keywords) Proc. / Display
string Proc. / Display
array Proc. / Display
array Proc. / Display
string Proc. / Display
boolean Proc. / Display
array Proc. / Display
array Proc. / Display
boolean Proc. / Display

appearance

appearance
Type
array
Path
$GLOBALS['TCA'][$table]['columns'][$field]['config']
Scope
Display

Properties that only apply to how the field is displayed in the backend.

prefix

prefix
Type
userFunction
Path
$GLOBALS['TCA'][$table]['columns'][$field]['config']['appearance']
Scope
Display

Provides a string that is displayed in front of the input field. The URL of the site is set by default if nothing has been defined.

Assign a user function. It receives two arguments:

  • The first argument is the parameters array containing the site object, the language id, the current table and the current row.
  • The second argument is the reference object TcaSlug.

The user function should return the string which is then used for display purposes.

Example: Set a slug field prefix
EXT:styleguide/Configuration/TCA/tx_styleguide_elements_slugs.php
[
    'columns' => [
        'slug_1' => [
            'label' => 'slug_1',
            'description' => 'field description',
            'config' => [
                'type' => 'slug',
                'generatorOptions' => [
                    'fields' => [
                        'input_1',
                        'input_2',
                    ],
                    'fieldSeparator' => '/',
                    'prefixParentPageSlug' => true,
                    'replacements' => [
                        '/' => '',
                    ],
                ],
                'appearance' => [
                    'prefix' => 'TYPO3\\CMS\\Styleguide\\UserFunctions\\FormEngine\\SlugPrefix->getPrefix',
                ],
                'fallbackCharacter' => '-',
                'eval' => 'uniqueInSite',
                'default' => '',
            ],
        ],
    ],
]
Copied!

The user function can be implemented like this:

<?php

declare(strict_types=1);

namespace TYPO3\CMS\Styleguide\UserFunctions\FormEngine;

/**
 * This file is part of the TYPO3 CMS project.
 *
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
 *
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
 *
 * The TYPO3 project - inspiring people to share!
 */

/**
 * A user function to compare two fields
 */
use TYPO3\CMS\Backend\Form\FormDataProvider\TcaSlug;

class SlugPrefix
{
    public function getPrefix(array $parameters, TcaSlug $reference): string
    {
        return 'custom slug prefix';
    }
}
Copied!

eval

eval
Type
string (list of keywords)
Path
$GLOBALS['TCA'][$table]['columns'][$field]['config']
Scope
Proc. / Display

Configuration of field evaluation.

Keywords:

unique
Evaluate if a record is unique in the whole TYPO3 installation (specific to a language). This option is recommended as it allows to show any record stored inside other sites. The only downside is that it is not possible to have the same slug on multiple sites.
uniqueInSite

Requires the field to be unique for the current site and language. This allows for multiple records of the same table to have the same slug as long as these records are separated by their sites. Consequently records of a foreign site are not accessible with uniqueInsite since slugs are looked up respecting the current site.

uniqueInPid
Requires the field to be unique for the current PID among other records on the same page.

No other eval setting is checked for. It is possible to set different eval options, however it is recommended not to do so.

Example: eval = uniqueInSite
EXT:styleguide/Configuration/TCA/tx_styleguide_elements_slugs.php
[
    'columns' => [
        'slug_2' => [
            'label' => 'slug_2',
            'config' => [
                'type' => 'slug',
                'size' => 50,
                'generatorOptions' => [
                    'fields' => [
                        'input_1',
                    ],
                    'fieldSeparator' => '/',
                    'prefixParentPageSlug' => true,
                ],
                'fallbackCharacter' => '-',
                'eval' => 'uniqueInSite',
                'default' => '',
            ],
        ],
    ],
]
Copied!

fallbackCharacter

fallbackCharacter
Type
string
Path
$GLOBALS['TCA'][$table]['columns'][$field]['config']
Scope
Proc. / Display
Default
-

Character that represents the separator of slug sections, that contain the fieldSeparator.

Example: fallbackCharacter and fieldSeparator set
EXT:styleguide/Configuration/TCA/tx_styleguide_elements_slugs.php
[
    'columns' => [
        'slug_2' => [
            'label' => 'slug_2',
            'config' => [
                'type' => 'slug',
                'size' => 50,
                'generatorOptions' => [
                    'fields' => [
                        'input_1',
                    ],
                    'fieldSeparator' => '/',
                    'prefixParentPageSlug' => true,
                ],
                'fallbackCharacter' => '-',
                'eval' => 'uniqueInSite',
                'default' => '',
            ],
        ],
    ],
]
Copied!

generatorOptions

generatorOptions
Type
array
Path
$GLOBALS['TCA'][$table]['columns'][$field]['config']['']
Scope
Proc. / Display

Holds information about the record fields to be used for slug generation:

fields

fields
Type
array
Path
$GLOBALS['TCA'][$table]['columns'][$field]['config']['generatorOptions']['fields']
Scope
Proc. / Display

Insert several field names (of type string) that will be considered during slug construction.

Can also be used as nested array to combine multiple fields [['nav_title', 'title'], 'other_field'].

Examples:

Configuration value Values of an example page record Resulting slug
[['nav_title', 'title']] ['title' => 'Products', 'nav_title' => ''] /products
[['title', 'subtitle']] ['title' => 'Products', 'subtitle' => 'Product subtitle'] /products
['title', 'subtitle'] or [['title'], ['subtitle']] ['title' => 'Products', 'subtitle' => 'Product subtitle'] /products/product-subtitle
['nav_title', 'title'], 'subtitle' ['title' => 'Products', 'nav_title' => 'Best products', 'subtitle' => 'Product subtitle'] /best-products/product-subtitle
['seo_title', 'title'], ['nav_title', 'subtitle'] ['title' => 'Products', 'nav_title' => 'Best products', 'subtitle' => 'Product subtitle', 'seo_title' => 'SEO product title'] /seo-product-title/best-products

fieldSeparator

fieldSeparator
Type
string
Path
$GLOBALS['TCA'][$table]['columns'][$field]['config']['generatorOptions']['fieldSeparator']
Scope
Proc. / Display
Default
"/"

This value will divide the slug parts. If a section value contains this very value, it will be replaced by the value given in fallbackCharacter.

prefixParentPageSlug

prefixParentPageSlug
Type
boolean
Path
$GLOBALS['TCA'][$table]['columns'][$field]['config']['generatorOptions']['prefixParentPageSlug']
Default
true
Scope
Proc. / Display

The slugs of parent pages will be prefixed to the slug for the page itself. Disable it for shorter URLs, but take the higher chance of collision into consideration.

replacements

replacements
Type
array
Path
$GLOBALS['TCA'][$table]['columns'][$field]['config']['generatorOptions']['replacements']
Scope
Proc. / Display

It allows to replace strings of a slug part. Add one of more array items with the key being the string to replace and the value being the replacement string.

postModifiers

postModifiers
Type
array
Path
$GLOBALS['TCA'][$table]['columns'][$field]['config']['generatorOptions']['postModifiers']
Scope
Proc. / Display

The "slug" TCA type includes a possibility to hook into the generation of a slug via custom TCA generation options.

Hooks can be registered via:

EXT:myextension/Configuration/TCA/Overrides/table.php
$GLOBALS['TCA'][$tableName]['columns'][$fieldName]['config']['generatorOptions']['postModifiers'][]
    = MyClass::class . '->method';
Copied!

Consider $tableName = 'pages' and $fieldName = 'slug' inside EXT:my_extension/Configuration/TCA/Overrides/table.php:

EXT:myextension/Configuration/TCA/Overrides/table.php
$GLOBALS['TCA']['pages']['columns']['slug']['config']['generatorOptions']['postModifiers'][] =
    MyClass::class . '->modifySlug';
Copied!

The method then receives an parameter array with the following values

  [
      'slug', // ...  the slug to be used
      'workspaceId', // ...  the workspace ID, "0" if in live workspace
      'configuration', // ...  the configuration of the TCA field
      'record', // ...  the full record to be used
      'pid', // ...  the resolved parent page ID
      'prefix', // ...  the prefix that was added
      'tableName', // ...  the table of the slug field
      'fieldName', // ...  the field name of the slug field
];
Copied!

All hooks need to return the modified slug value.

Example: Generate a concatenated slug
EXT:styleguide/Configuration/TCA/tx_styleguide_elements_slugs.php
[
    'columns' => [
        'slug_4' => [
            'label' => 'slug_4',
            'config' => [
                'type' => 'slug',
                'generatorOptions' => [
                    'fields' => [
                        'input_1',
                        'input_2',
                    ],
                    'prefixParentPageSlug' => false,
                ],
                'fallbackCharacter' => '-',
                'eval' => 'uniqueInSite',
                'default' => '',
            ],
        ],
    ],
]
Copied!
Example: Generate a slug from one field with a backup field if first is empty
EXT:styleguide/Configuration/TCA/tx_styleguide_elements_slugs.php
[
    'columns' => [
        'slug_5' => [
            'label' => 'slug_5',
            'config' => [
                'type' => 'slug',
                'generatorOptions' => [
                    'fields' => [
                        [
                            'input_1',
                            'input_2',
                        ],
                    ],
                    'prefixParentPageSlug' => false,
                ],
                'fallbackCharacter' => '-',
                'eval' => 'uniqueInSite',
                'default' => '',
            ],
        ],
    ],
]
Copied!
Example: remove all strings "(f/m)" from job titles

This will change the provided slug 'Some Job in city1/city2 (f/m)' to 'some-job-in-city1-city2'.

EXT:styleguide/Configuration/TCA/tx_styleguide_elements_slugs.php
[
    'columns' => [
        'slug_3' => [
            'label' => 'slug_3',
            'description' => 'remove string (f/m)',
            'config' => [
                'type' => 'slug',
                'generatorOptions' => [
                    'fields' => [
                        'input_3',
                    ],
                    'replacements' => [
                        '(f/m)' => '',
                        '/' => '-',
                    ],
                ],
                'fallbackCharacter' => '-',
                'prependSlash' => true,
                'eval' => 'uniqueInPid',
            ],
        ],
    ],
]
Copied!

prependSlash

prependSlash
Type
boolean
Path
$GLOBALS['TCA'][$table]['columns'][$field]['config']
Scope
Proc. / Display

Defines whether a slug field should contain a prepending slash, for example for nested categories with speaking segments.

If not set, this defaults to false. (Exception: for the pages.slug field this defaults to true and cannot be changed.)