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 set up 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 (formerly known as $GLOBALS['TSFE']->loginUser
),
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.
The date time aspect, TYPO3\CMS\Core\Context\DateTimeAspect
, accepts following properties:
Property |
Call |
Result |
---|---|---|
|
|
unix timestamp as integer value |
|
|
timezone name, e.g. |
|
|
datetime as string in ISO 8601 format, e.g. |
|
|
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.
The language aspect, TYPO3\CMS\Core\Context\LanguageAspect
accepts following properties:
Property |
Call |
Result |
---|---|---|
|
|
the requested language of the current page as integer (uid) |
|
|
the language id of records to be fetched in translation scenarios as integer (uid) |
|
|
the fallback steps as array |
|
|
one of |
|
|
one of |
|
|
one of |
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 |
---|---|---|
|
|
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 |
---|---|---|
|
|
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.
The user aspect, TYPO3\CMS\Core\Context\UserAspect
, accepts following properties:
Property |
Call |
Result |
---|---|---|
|
|
uid of the currently logged in user, 0 if no user |
|
|
the username of the currently authenticated user. Empty string if no user. |
|
|
whether a user is logged in, as boolean. |
|
|
whether the user is admin, as boolean. Only useful for BEuser. |
|
|
the groups the user is a member of, as array |
|
|
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.
The visibility aspect, TYPO3\CMS\Core\Context\VisibilityAspect
, accepts following properties:
Property |
Call |
Result |
---|---|---|
|
|
whether hidden pages should be displayed, as boolean |
|
|
whether hidden content should be displayed, as boolean |
|
|
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
The workspace aspect, TYPO3\CMS\Core\Context\WorkspaceAspect
, accepts following properties:
Property |
Call |
Result |
---|---|---|
|
|
UID of the currently accessed workspace as integer |
|
|
whether the current workspace is live, or a custom offline WS, as boolean |
|
|
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');