CoolCMS API

PageSlugTranslator extends AbstractTranslator
in package
implements SlugTranslatorInterface

Base class of route translators: keeps the list of route factories that use a translator.

Table of Contents

Interfaces

SlugTranslatorInterface

Properties

$idToSlugCache  : array<int, array<int, string>>
$localeResolver  : LocaleResolver
$pageManager  : PageManager
$slugToIdCache  : array<int, array<string, int>>
$warmedUp  : bool

Methods

__construct()  : mixed
addTargetFactory()  : void
Registers a route factory that uses this translator.
batchIdToSlug()  : array<int, string>
clearCache()  : void
Clears the cache.
getTargetFactories()  : array<string|int, string>
Returns the route factories that use this translator.
idToSlug()  : string|null
Converts an id to a slug for the given locale.
slugToId()  : int|null
Converts a slug to an id for the given locale.
warmupCache()  : void
Loads all pages with their translations in a single query.
warmupFromEntities()  : void
Fills the cache from already loaded entities (no DB query).\n *
warmupFromMaps()  : void
Fills the cache from the precomputed maps of {@see \App\Core\Routers\RouteTableSnapshot} and sets `warmedUp` (disables the lazy DB fallback below).
fromCacheWithLanguageFallback()  : TValue|null
Returns a value from a translation cache, falling back to the other languages.

Properties

$idToSlugCache

private array<int, array<int, string>> $idToSlugCache = []

Cache id->slug [languageId][pageId] = slug

$slugToIdCache

private array<int, array<string, int>> $slugToIdCache = []

Cache slug->id [languageId][slug] = pageId

Methods

addTargetFactory()

Registers a route factory that uses this translator.

public addTargetFactory(string $factoryClass) : void

Called from the factory constructor as addTargetFactory($this::class).

Parameters
$factoryClass : string

fully qualified factory class name

batchIdToSlug()

public batchIdToSlug(array<string|int, int> $ids, string $locale) : array<int, string>
Parameters
$ids : array<string|int, int>
$locale : string
Return values
array<int, string>

getTargetFactories()

Returns the route factories that use this translator.

public getTargetFactories() : array<string|int, string>
Return values
array<string|int, string> —

factory class names without namespace

idToSlug()

Converts an id to a slug for the given locale.

public idToSlug(int $id, string $locale) : string|null
Parameters
$id : int
$locale : string
Return values
string|null

slugToId()

Converts a slug to an id for the given locale.

public slugToId(string $slug, string $locale) : int|null
Parameters
$slug : string
$locale : string
Return values
int|null

warmupCache()

Loads all pages with their translations in a single query.

public warmupCache([array<string|int, int>|null $pageIds = null ]) : void
Parameters
$pageIds : array<string|int, int>|null = null

when null, loads all pages; otherwise only the given ones

warmupFromEntities()

Fills the cache from already loaded entities (no DB query).\n *

public warmupFromEntities(array<string|int, mixed> $pages) : void
Parameters
$pages : array<string|int, mixed>

Page entities with loaded translations

warmupFromMaps()

Fills the cache from the precomputed maps of {@see \App\Core\Routers\RouteTableSnapshot} and sets `warmedUp` (disables the lazy DB fallback below).

public warmupFromMaps(array<int, array<int, string>> $idToSlug, array<int, array<string, int>> $slugToId) : void

Both maps are passed explicitly; flipping one would lose the last-wins behavior for duplicate hrefs.

Parameters
$idToSlug : array<int, array<int, string>>

[languageId][pageId] = href

$slugToId : array<int, array<string, int>>

[languageId][href] = pageId

fromCacheWithLanguageFallback()

Returns a value from a translation cache, falling back to the other languages.

protected fromCacheWithLanguageFallback(array<string|int, mixed> $cache, int $languageId, TKey $key) : TValue|null

IMPORTANT: without the fallback a translator returns null when the translation for the language is missing, and the route cannot build the URL at all ("No route for ..."). One untranslated category then breaks the whole foreign-language page, not just its own link.

Untranslated content shows in the main language instead of disappearing: the URL carries the main-language slug under /en/, until someone adds the translation.

Parameters
$cache : array<string|int, mixed>
$languageId : int
$key : TKey
Tags
template
Return values
TValue|null
On this page

Search results