Slugs / URL parts
The main purpose of this type is to define parts of a URL path to generate and resolve URLs. The according database field is generated automatically.
table of contents
Example: A basic slug field
This example limits the length of the slug to 50 characters. It takes only the
field input_
into account for generating the slug.
[
'columns' => [
'slug_2' => [
'label' => 'slug_2',
'config' => [
'type' => 'slug',
'size' => 50,
'generatorOptions' => [
'fields' => [
'input_1',
],
'fieldSeparator' => '/',
'prefixParentPageSlug' => true,
],
'fallbackCharacter' => '-',
'eval' => 'uniqueInSite',
'default' => '',
],
],
],
]
Example: A slug field with prefix hook
This example uses a custom slug prefix hook via
config
to adapt the displayed prefix. It takes
the two fields input_
and input_
into account for generating
the slug.
[
'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' => '',
],
],
],
]
Properties of the TCA column type slug
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
-
- 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
Tca
.Slug
The user function should return the string which is then used for display purposes.
[
'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' => '',
],
],
],
]
The user function can be implemented like this:
<?php
declare(strict_types=1);
/*
* 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!
*/
namespace TYPO3\CMS\Styleguide\UserFunctions\FormEngine;
/**
* A user function to compare two fields
*
* @internal
*/
use TYPO3\CMS\Backend\Form\FormDataProvider\TcaSlug;
final class SlugPrefix
{
public function getPrefix(array $parameters, TcaSlug $reference): string
{
return 'custom slug prefix';
}
}
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
unique
since slugs are looked up respecting the current site.Insite Warning
Be aware that using this option makes it impossible to show records stored inside other sites. If this is required,
unique
should be used instead. - 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.
[
'columns' => [
'slug_2' => [
'label' => 'slug_2',
'config' => [
'type' => 'slug',
'size' => 50,
'generatorOptions' => [
'fields' => [
'input_1',
],
'fieldSeparator' => '/',
'prefixParentPageSlug' => true,
],
'fallbackCharacter' => '-',
'eval' => 'uniqueInSite',
'default' => '',
],
],
],
]
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.
[
'columns' => [
'slug_2' => [
'label' => 'slug_2',
'config' => [
'type' => 'slug',
'size' => 50,
'generatorOptions' => [
'fields' => [
'input_1',
],
'fieldSeparator' => '/',
'prefixParentPageSlug' => true,
],
'fallbackCharacter' => '-',
'eval' => 'uniqueInSite',
'default' => '',
],
],
],
]
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
-
- 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'] Info
Inserting multiple fields in a simple array would result in an concatenated slug.
Nested array values would result in "take
nav_
if not empty, otherwise take value fromtitle title
".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
-
- 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
-
- 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.
Note
This option is exclusively for page records. It won't have an effect on any other records.
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
-
- 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:
$GLOBALS['TCA'][$tableName]['columns'][$fieldName]['config']['generatorOptions']['postModifiers'][] = MyClass::class . '->method';
Copied!Consider
$table
andName = 'pages' $field
insideName = 'slug' EXT:
:my_ extension/ 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.
[
'columns' => [
'slug_4' => [
'label' => 'slug_4',
'config' => [
'type' => 'slug',
'generatorOptions' => [
'fields' => [
'input_1',
'input_2',
],
'prefixParentPageSlug' => false,
],
'fallbackCharacter' => '-',
'eval' => 'uniqueInSite',
'default' => '',
],
],
],
]
[
'columns' => [
'slug_5' => [
'label' => 'slug_5',
'config' => [
'type' => 'slug',
'generatorOptions' => [
'fields' => [
[
'input_1',
'input_2',
],
],
'prefixParentPageSlug' => false,
],
'fallbackCharacter' => '-',
'eval' => 'uniqueInSite',
'default' => '',
],
],
],
]
This will change the provided slug 'Some Job in city1/city2 (f/m)' to 'some-job-in-city1-city2'.
[
'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',
],
],
],
]
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 thepages.
field this defaults toslug true
and cannot be changed.)