Attention

TYPO3 v10 has reached end-of-life as of April 30th 2023 and is no longer being maintained. Use the version switcher on the top left of this page to select documentation for a supported version of TYPO3.

Need more time before upgrading? You can purchase Extended Long Term Support (ELTS) for TYPO3 v10 here: TYPO3 ELTS.

Context API and Aspects

Introduction

Context API encapsulates various information for data retrieval (e.g. inside the database) and analysis of current permissions and caching information.

Previously, various information was distributed inside globally accessible objects ($TSFE or $GLOBALS['BE_USER']) like the current workspace ID or if a frontend or backend user is authenticated. Having a global object available was also dependent on the current request type (frontend or backend), instead of having one consistent place where all this data is located.

The context is instantiated at the very beginning of each TYPO3 entry point, keeping track of the current time (formally known as $GLOBALS['EXEC_TIME'], if a user is logged in, and which workspace is currently accessed.

It can be retrieved anywhere via GeneralUtility::makeInstance():

use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Context\Context;

$context = GeneralUtility::makeInstance(Context::class);

This information is separated in so-called "Aspects", each being responsible for a certain area:

Date time aspect

Contains time, date and timezone information for the current request.

In comparison to known behaviour until TYPO3 v9, DateTimeAspect replaces for example $GLOBALS['SIM_EXEC_TIME'] and $GLOBALS['EXEC_TIME'].

The date time aspect, TYPO3\CMS\Core\Context\DateTimeAspect, accepts following properties:

Property

Call

Result

timestamp

$context->getPropertyFromAspect('date', 'timestamp');

unix timestamp as integer value

timezone

$context->getPropertyFromAspect('date', 'timezone');

timezone name, e.g. Germany/Berlin

iso

$context->getPropertyFromAspect('date', 'iso');

datetime as string in ISO 8601 format, e.g. 2004-02-12T15:19:21+00:00

full

$context->getPropertyFromAspect('date', 'full');

the complete DateTimeImmutable object

Example

use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Context\Context;

$context = GeneralUtility::makeInstance(Context::class);

// Reading the current data instead of $GLOBALS['EXEC_TIME']
$currentTimestamp = $context->getPropertyFromAspect('date', 'timestamp');

Language aspect

Contains information about language settings for the current request, including fallback and overlay logic.

In comparison to known behaviour until TYPO3 v9, LanguageAspect replaces various properties related to language Id, overlay and fallback logic, mostly within Frontend.

The language aspect, TYPO3\CMS\Core\Context\LanguageAspect accepts following properties:

Property

Call

Result

id

$context->getPropertyFromAspect('language', 'id');

the requested language of the current page as integer (uid)

contentId

$context->getPropertyFromAspect('language', 'contentId');

the language id of records to be fetched in translation scenarios as integer (uid)

fallbackChain

$context->getPropertyFromAspect('language', 'fallbackChain');

the fallback steps as array

overlayType

$context->getPropertyFromAspect('language', 'overlayType');

one of LanguageAspect::OVERLAYS_OFF, LanguageAspect::OVERLAYS_MIXED, LanguageAspect::OVERLAYS_ON, or LanguageAspect::OVERLAYS_ON_WITH_FLOATING (default)

legacyLanguageMode

$context->getPropertyFromAspect('language', 'legacyLanguageMode');

one of strict, ignore or content_fallback, kept for compatibility reasons. Don't use if not really necessary, the option will be removed rather sooner than later.

legacyOverlayType

$context->getPropertyFromAspect('language', 'legacyOverlayType');

one of hideNonTranslated, 0 or 1, kept for compatibility reasons. Don't use if not really necessary, the option will be removed rather sooner than later.

Overlay types:

  • LanguageAspect::OVERLAYS_OFF:

    Just fetch records from the selected language as given by $GLOBALS['TSFE']->sys_language_content. No overlay will happen, no fetching of the records from the default language. This boils down to "free mode" language handling. Records without a default language parent are included.

  • LanguageAspect::OVERLAYS_MIXED:

    Fetch records from the default language and overlay them with translations. If a record is not translated, the default language will be used.

  • LanguageAspect::OVERLAYS_ON:

    Fetch records from the default language and overlay them with translations. If a record is not translated, it will not be displayed.

  • LanguageAspect::OVERLAYS_ON_WITH_FLOATING:

    Fetch records from the default language and overlay them with translations. If a record is not translated, it will not be shown. Records without a default language parent are included.

Replaced calls:

  • $TSFE->sys_language_uid -> id

  • $TSFE->sys_language_content -> contentId

  • $TSFE->sys_language_mode -> fallbackChain

  • $TSFE->sys_language_mode -> legacyLanguageMode

  • $TSFE->sys_language_contentOL -> legacyOverlayType

Example

use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Context\Context;

$context = GeneralUtility::makeInstance(Context::class);

// Reading the current fallback chain instead $TSFE->sys_language_mode
$fallbackChain = $context->getPropertyFromAspect('language', 'fallbackChain');

Preview aspect

The preview aspect may be used to indicate that the frontend is in preview mode (for example in case a workspace is previewed or hidden pages or records should be shown).

The preview aspect, TYPO3\CMS\Frontend\Aspect\PreviewAspect, contains the following properties:

Property

Call

Result

isPreview

$context->getPropertyFromAspect('frontend.preview', 'isPreview');

whether the frontend is currently in preview mode

TypoScript aspect

The TypoScriptAspect can be used to manipulate/check whether TemplateRendering is forced.

The TypoScript aspect, TYPO3\CMS\Core\Context\TypoScriptAspect contains the following properties:

Property

Call

Result

forcedTemplateParsing

$context->getPropertyFromAspect('typoscript', 'forcedTemplateParsing');

whether TypoScript template parsing is forced

User aspect

Contains information about authenticated users in the current request. Can be used for frontend and backend users.

In comparison to known behaviour until TYPO3 v9, UserAspect replaces various calls and checks on $GLOBALS['BE_USER'] and $GLOBALS['TSFE']->fe_user options when only some information is needed.

The user aspect, TYPO3\CMS\Core\Context\UserAspect, accepts following properties:

Property

Call

Result

id

$context->getPropertyFromAspect('backend.user', 'id');

uid of the currently logged in user, 0 if no user

username

$context->getPropertyFromAspect('backend.user', 'username');

the username of the currently authenticated user. Empty string if no user.

isLoggedIn

$context->getPropertyFromAspect('frontend.user', 'isLoggedIn');

whether a user is logged in, as boolean.

isAdmin

$context->getPropertyFromAspect('backend.user', 'isAdmin');

whether the user is admin, as boolean. Only useful for BEuser.

groupIds

$context->getPropertyFromAspect('backend.user', 'groupIds');

the groups the user is a member of, as array

groupNames

$context->getPropertyFromAspect('frontend.user', 'groupNames');

the names of all groups the user belongs to, as array

Example

use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Context\Context;

$context = GeneralUtility::makeInstance(Context::class);

// Checking if a user is logged in
$userIsLoggedIn = $context->getPropertyFromAspect('frontend.user', 'isLoggedIn');

Visibility aspect

The aspect contains whether to show hidden pages, records (content) or even deleted records.

In comparison to known behaviour until TYPO3 v9, VisibilityAspect replaces for example $GLOBALS['TSFE']->showHiddenPages and $GLOBALS['TSFE']->showHiddenRecords.

The visibility aspect, TYPO3\CMS\Core\Context\VisibilityAspect, accepts following properties:

Property

Call

Result

includeHiddenPages

$context->getPropertyFromAspect('visibility', 'includeHiddenPages');

whether hidden pages should be displayed, as boolean

includeHiddenContent

$context->getPropertyFromAspect('visibility', 'includeHiddenContent');

whether hidden content should be displayed, as boolean

includeDeletedRecords

$context->getPropertyFromAspect('visibility', 'includeDeletedRecords');

whether deleted records should be displayed, as boolean.

Example

use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Context\Context;

$context = GeneralUtility::makeInstance(Context::class);

// Checking if hidden pages should be displayed
$showHiddenPages = $context->getPropertyFromAspect('visibility', 'includeHiddenPages');

Workspace aspect

The aspect contains information about the currently accessed workspace

In comparison to known behaviour until TYPO3 v9, WorkspaceAspect replaces e.g. $GLOBALS['BE_USER']->workspace.

The workspace aspect, TYPO3\CMS\Core\Context\WorkspaceAspect, accepts following properties:

Property

Call

Result

id

$context->getPropertyFromAspect('workspace', 'id');

UID of the currently accessed workspace as integer

isLive

$context->getPropertyFromAspect('workspace', 'isLive');

whether the current workspace is live, or a custom offline WS, as boolean

isOffline

$context->getPropertyFromAspect('workspace', 'isOffline');

whether the current workspace is offline, as boolean.

Example

use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Context\Context;

$context = GeneralUtility::makeInstance(Context::class);

// Retrieving the uid of currently accessed workspace
$workspaceId = $context->getPropertyFromAspect('workspace', 'id');