Context API and aspects
Introduction
The Context API encapsulates various information for data retrieval (for example, inside the database) and analysis of current permissions and caching information.
The context is set up at the very beginning of each TYPO3 entry point, keeping track of, for example, the current time, if a user is logged in and which workspace is currently accessed.
The
\TYPO3\ object can be retrieved via
dependency injection:
<?php
declare(strict_types=1);
namespace MyVendor\MyExtension\Controller;
use TYPO3\CMS\Core\Context\Context;
final class MyController
{
public function __construct(
private readonly Context $context,
) {}
}
This information is separated in so-called "aspects", each being responsible for a certain area.
Aspects
Date time aspect
Contains time, date and timezone information for the current request.
The date time aspect,
\TYPO3\, accepts
the following properties:
timestamp
-
- Call
$this->context->getProperty From Aspect ('date', 'timestamp');
Returns the Unix timestamp as an integer value.
timezone
-
- Call
$this->context->getProperty From Aspect ('date', 'timezone');
Returns the timezone name, for example, "Germany/Berlin".
iso
-
- Call
$this->context->getProperty From Aspect ('date', 'iso');
Returns the datetime as string in ISO 8601 format, for example, "2004-02-12T15:19:21+00:00".
full
-
- Call
$this->context->getProperty From Aspect ('date', 'full');
Returns the complete \DateTimeImmutable object.
Example
<?php
declare(strict_types=1);
namespace MyVendor\MyExtension\Controller;
use TYPO3\CMS\Core\Context\Context;
final class MyController
{
public function __construct(
private readonly Context $context,
) {}
public function doSomething(): void
{
$currentTimestamp = $this->context->getPropertyFromAspect(
'date',
'timestamp',
);
// ... do something with $currentTimestamp
}
}
Language aspect
Contains information about language settings for the current request, including fallback and overlay logic.
The language aspect,
\TYPO3\ accepts the
following properties:
id
-
- Call
$this->context->getProperty From Aspect ('language', 'id');
Returns the requested language of the current page as integer (uid).
contentId
-
- Call
$this->context->getProperty From Aspect ('language', 'content Id');
Returns the language ID of records to be fetched in translation scenarios as integer (uid).
fallbackChain
-
- Call
$this->context->getProperty From Aspect ('language', 'fallback Chain');
Returns the fallback steps as array.
overlayType
-
- Call
$this->context->getProperty From Aspect ('language', 'overlay Type');
Returns one of
LanguageAspect:: OVERLAYS_ OFF LanguageAspect:: OVERLAYS_ MIXED LanguageorAspect:: OVERLAYS_ ON Language(default)Aspect:: OVERLAYS_ ON_ WITH_ FLOATING
See Overlay types for more details.
legacyLanguageMode
-
- Call
$this->context->getProperty From Aspect ('language', 'legacy Language Mode');
Returns one of
strictignoreorcontent_.fallback
This property is kept for compatibility reasons. Do not use, if not really necessary, the option will be removed rather sooner than later.
legacyOverlayType
-
- Call
$this->context->getProperty From Aspect ('language', 'legacy Overlay Type');
Returns one of
hideNon Translated 0or1.
This property is kept for compatibility reasons. Do not 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
Language. 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.Aspect->get Content Id () 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.
Example
<?php
declare(strict_types=1);
namespace MyVendor\MyExtension\Controller;
use TYPO3\CMS\Core\Context\Context;
final class MyController
{
public function __construct(
private readonly Context $context,
) {}
public function doSomething(): void
{
$fallbackChain = $this->context->getPropertyFromAspect(
'language',
'fallbackChain',
);
// ... do something with $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\, contains
the following property:
isPreview
-
- Call
$this->context->getProperty From Aspect ('frontend. preview', 'is Preview');
Returns, whether the frontend is currently in preview mode.
User aspect
Contains information about authenticated users in the current request. The aspect can be used for frontend and backend users.
The user aspect,
\TYPO3\, accepts the
following properties:
id
-
- Call
$this->context->getorProperty From Aspect ('frontend. user', 'id'); $this->context->getProperty From Aspect ('backend. user', 'id');
Returns the uid of the currently logged in user,
0if no user is logged in.
username
-
- Call
$this->context->getorProperty From Aspect ('frontend. user', 'username'); $this->context->getProperty From Aspect ('backend. user', 'username');
Returns the username of the currently authenticated user. Empty string, if no user is logged in.
isLoggedIn
-
- Call
$this->context->getorProperty From Aspect ('frontend. user', 'is Logged In'); $this->context->getProperty From Aspect ('backend. user', 'is Logged In');
Returns, whether a user is logged in, as boolean.
isAdmin
-
- Call
$this->context->getProperty From Aspect ('backend. user', 'is Admin');
Returns, whether the user is an administrator, as boolean. It is only useful for backend users.
groupIds
-
- Call
$this->context->getorProperty From Aspect ('frontend. user', 'group Ids'); $this->context->getProperty From Aspect ('backend. user', 'group Ids');
Returns the groups the user is a member of, as array.
groupNames
-
- Call
$this->context->getorProperty From Aspect ('frontend. user', 'group Names'); $this->context->getProperty From Aspect ('backend. user', 'group Names');
Returns the names of all groups the user belongs to, as array.
Example
<?php
declare(strict_types=1);
namespace MyVendor\MyExtension\Controller;
use TYPO3\CMS\Core\Context\Context;
final class MyController
{
public function __construct(
private readonly Context $context,
) {}
public function doSomething(): void
{
$userIsLoggedIn = $this->context->getPropertyFromAspect(
'frontend.user',
'isLoggedIn',
);
// ... do something with $userIsLoggedIn
}
}
Visibility aspect
The aspect contains whether to show hidden pages, records (content) or even deleted records.
The visibility aspect,
\TYPO3\, accepts
the following properties:
includeDeletedRecords
-
- Call
$this->context->getProperty From Aspect ('visibility', 'include Deleted Records');
Returns, whether deleted records should be displayed, as boolean.
Example
<?php
declare(strict_types=1);
namespace MyVendor\MyExtension\Controller;
use TYPO3\CMS\Core\Context\Context;
final class MyController
{
public function __construct(
private readonly Context $context,
) {}
public function doSomething(): void
{
$showHiddenPages = $this->context->getPropertyFromAspect(
'visibility',
'includeHiddenPages',
);
// ... do something with $showHiddenPages
}
}
Workspace aspect
The aspect contains information about the currently accessed workspace.
The workspace aspect,
\TYPO3\, accepts
the following properties:
id
-
- Call
$this->context->getProperty From Aspect ('workspace', 'id');
Returns the UID of the currently accessed workspace, as integer.
isLive
-
- Call
$this->context->getProperty From Aspect ('workspace', 'is Live');
Returns whether the current workspace is live, or a custom offline workspace, as boolean.
isOffline
-
- Call
$this->context->getProperty From Aspect ('workspace', 'is Offline');
Returns, whether the current workspace is offline, as boolean.
Example
<?php
declare(strict_types=1);
namespace MyVendor\MyExtension\Controller;
use TYPO3\CMS\Core\Context\Context;
final class MyController
{
public function __construct(
private readonly Context $context,
) {}
public function doSomething(): void
{
$showHiddenPages = $this->context->getPropertyFromAspect(
'workspace',
'id',
);
// ... do something with $showHiddenPages
}
}