.. include:: /Includes.rst.txt .. _context-api: ======================= Context API and aspects ======================= 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 :php:`\TYPO3\CMS\Core\Context\Context` object can be retrieved via :ref:`dependency injection `: .. literalinclude:: _MyController.php :caption: EXT:my_extension/Classes/Controller/MyController.php This information is separated in so-called ":ref:`aspects `", each being responsible for a certain area. .. _context-api-aspects: Aspects ======= .. contents:: :depth: 1 :local: .. _context-api-aspects-datetime: Date time aspect ---------------- Contains time, date and timezone information for the current request. .. _context-api-aspects-datetime-properties: The date time aspect, :php:`\TYPO3\CMS\Core\Context\DateTimeAspect`, accepts the following properties: .. confval:: timestamp :name: datetime-aspect-timestamp :Call: :php:`$this->context->getPropertyFromAspect('date', 'timestamp');` Returns the Unix timestamp as an integer value. .. _datetime-aspect-timezone: timezone ~~~~~~~~ .. confval:: timezone :name: datetime-aspect-timezone :Call: :php:`$this->context->getPropertyFromAspect('date', 'timezone');` Returns the timezone name, for example, "Germany/Berlin". .. _datetime-aspect-iso: iso ~~~ .. confval:: iso :name: datetime-aspect-iso :Call: :php:`$this->context->getPropertyFromAspect('date', 'iso');` Returns the datetime as string in `ISO 8601 `__ format, for example, "2004-02-12T15:19:21+00:00". .. _datetime-aspect-full: full ~~~~ .. confval:: full :name: datetime-aspect-full :Call: :php:`$this->context->getPropertyFromAspect('date', 'full');` Returns the complete `\DateTimeImmutable `__ object. .. _datetime-aspect-accesstime: accessTime ~~~~~~~~~~ .. confval:: accessTime :name: datetime-aspect-accesstime :Call: :php:`$this->context->getPropertyFromAspect('date', 'accessTime');` :changed: 15.0 breaking-110414-1786380424 Returns the Unix timestamp floored to the full minute as an integer value. TYPO3 compares the `starttime` and `endtime` of records with this value and calculates the cache lifetime of a page from it. The method :php:`\TYPO3\CMS\Core\Context\DateTimeAspect::getTimestampWithMinutePrecision()` returns the same value. .. versionchanged:: 15.0 :changelog: breaking-110414-1786380424 The global variables `$GLOBALS['ACCESS_TIME']` and `$GLOBALS['SIM_ACCESS_TIME']` were removed. Use this property instead. .. _context-api-aspects-datetime-example: Example: Read and simulate the time of the date aspect ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. literalinclude:: _MyControllerUsingDateAspect.php :caption: EXT:my_extension/Classes/Controller/MyController.php To simulate another point in time, for example in a preview, set a new date aspect. Records are then evaluated against this time: .. literalinclude:: _SimulateDateAspect.php :caption: EXT:my_extension/Classes/Preview/SimulateDate.php .. _context-api-aspects-language: Language aspect --------------- Contains information about language settings for the current :ref:`request `, including fallback and overlay logic. .. _context-api-aspects-language-properties: The language aspect, :php:`\TYPO3\CMS\Core\Context\LanguageAspect` accepts the following properties: .. confval:: id :name: language-aspect-id :Call: :php:`$this->context->getPropertyFromAspect('language', 'id');` Returns the requested language of the current page as integer (uid). .. _language-aspect-contentid: contentId ~~~~~~~~~ .. confval:: contentId :name: language-aspect-contentId :Call: :php:`$this->context->getPropertyFromAspect('language', 'contentId');` Returns the language ID of records to be fetched in translation scenarios as integer (uid). .. _language-aspect-fallbackchain: fallbackChain ~~~~~~~~~~~~~ .. confval:: fallbackChain :name: language-aspect-fallbackChain :Call: :php:`$this->context->getPropertyFromAspect('language', 'fallbackChain');` Returns the fallback steps as array. .. _language-aspect-overlaytype: overlayType ~~~~~~~~~~~ .. confval:: overlayType :name: language-aspect-overlayType :Call: :php:`$this->context->getPropertyFromAspect('language', 'overlayType');` Returns one of * :php:`LanguageAspect::OVERLAYS_OFF` * :php:`LanguageAspect::OVERLAYS_MIXED` * :php:`LanguageAspect::OVERLAYS_ON` or * :php:`LanguageAspect::OVERLAYS_ON_WITH_FLOATING` (default) See :ref:`Overlay types ` for more details. .. _language-aspect-legacylanguagemode: legacyLanguageMode ~~~~~~~~~~~~~~~~~~ .. confval:: legacyLanguageMode :name: language-aspect-legacyLanguageMode :Call: :php:`$this->context->getPropertyFromAspect('language', 'legacyLanguageMode');` Returns one of * `strict` * `ignore` or * `content_fallback`. This property is kept for compatibility reasons. Do not use, if not really necessary, the option will be removed rather sooner than later. .. _language-aspect-legacyoverlaytype: legacyOverlayType ~~~~~~~~~~~~~~~~~ .. confval:: legacyOverlayType :name: language-aspect-legacyOverlayType :Call: :php:`$this->context->getPropertyFromAspect('language', 'legacyOverlayType');` Returns one of * `hideNonTranslated` * `0` or * `1`. This property is kept for compatibility reasons. Do not use, if not really necessary, the option will be removed rather sooner than later. .. _context-api-aspects-language-overlay-types: Overlay types ~~~~~~~~~~~~~ :php:`LanguageAspect::OVERLAYS_OFF` Fetch only records from the selected language as given by :php:`LanguageAspect->getContentId()`. 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. :php:`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. :php:`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. :php:`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. .. _context-api-aspects-language-example: Example: Read the fallback chain of the language aspect ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. literalinclude:: _MyControllerUsingLanguageAspect.php :caption: EXT:my_extension/Classes/Controller/MyController.php .. _context-api-aspects-language-pagerepository: Read pages in another language with `PageRepository` ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. versionadded:: 15.0 :changelog: feature-110453-1786523400 Before, you had to clone the context, set the language aspect on the clone, and create the `PageRepository` with `makeInstance()`. The :php-short:`\TYPO3\CMS\Core\Domain\Repository\PageRepository` resolves records for the language and the workspace of its context. To read pages in another language, inject the `PageRepository` and call :php-short:`\TYPO3\CMS\Core\Domain\Repository\PageRepository::withLanguageAspect()`. The method returns a new instance with the given language aspect and keeps all other aspects of the context. The injected instance stays unchanged. The following class returns the title of a page in the language of a site: .. literalinclude:: _TranslatedPageTitle.php :caption: EXT:my_extension/Classes/Service/TranslatedPageTitle.php Pass `new LanguageAspect()` to read records without any language overlay. To replace the whole context, call :php-short:`\TYPO3\CMS\Core\Domain\Repository\PageRepository::withContext()`. .. _context-api-aspects-preview: 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). .. _context-api-aspects-preview-properties: The preview aspect, :php:`\TYPO3\CMS\Frontend\Aspect\PreviewAspect`, contains the following property: .. confval:: isPreview :name: Preview-aspect-isPreview :Call: :php:`$this->context->getPropertyFromAspect('frontend.preview', 'isPreview');` Returns, whether the frontend is currently in preview mode. .. _context-api-aspects-user: User aspect ----------- Contains information about authenticated users in the current :ref:`request `. The aspect can be used for frontend and backend users. .. _context-api-aspects-user-properties: The user aspect, :php:`\TYPO3\CMS\Core\Context\UserAspect`, accepts the following properties: .. confval:: id :name: user-aspect-id :Call: :php:`$this->context->getPropertyFromAspect('frontend.user', 'id');` or :php:`$this->context->getPropertyFromAspect('backend.user', 'id');` Returns the uid of the currently logged in user, `0` if no user is logged in. .. _user-aspect-username: username ~~~~~~~~ .. confval:: username :name: user-aspect-username :Call: :php:`$this->context->getPropertyFromAspect('frontend.user', 'username');` or :php:`$this->context->getPropertyFromAspect('backend.user', 'username');` Returns the username of the currently authenticated user. Empty string, if no user is logged in. .. _user-aspect-isloggedin: isLoggedIn ~~~~~~~~~~ .. confval:: isLoggedIn :name: user-aspect-isLoggedIn :Call: :php:`$this->context->getPropertyFromAspect('frontend.user', 'isLoggedIn');` or :php:`$this->context->getPropertyFromAspect('backend.user', 'isLoggedIn');` Returns, whether a user is logged in, as boolean. .. _user-aspect-isadmin: isAdmin ~~~~~~~ .. confval:: isAdmin :name: user-aspect-isAdmin :Call: :php:`$this->context->getPropertyFromAspect('backend.user', 'isAdmin');` Returns, whether the user is an administrator, as boolean. It is only useful for backend users. .. _user-aspect-groupids: groupIds ~~~~~~~~ .. confval:: groupIds :name: user-aspect-groupIds :Call: :php:`$this->context->getPropertyFromAspect('frontend.user', 'groupIds');` or :php:`$this->context->getPropertyFromAspect('backend.user', 'groupIds');` Returns the groups the user is a member of, as array. .. _user-aspect-groupnames: groupNames ~~~~~~~~~~ .. confval:: groupNames :name: user-aspect-groupNames :Call: :php:`$this->context->getPropertyFromAspect('frontend.user', 'groupNames');` or :php:`$this->context->getPropertyFromAspect('backend.user', 'groupNames');` Returns the names of all groups the user belongs to, as array. .. _context-api-aspects-user-example: Example: Check whether a frontend user is logged in ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. literalinclude:: _MyControllerUsingUserAspect.php :caption: EXT:my_extension/Classes/Controller/MyController.php .. _context-api-aspects-visibility: Visibility aspect ----------------- The aspect contains whether to show hidden pages, records (content) or even deleted records. .. _context-api-aspects-visibility-properties: The visibility aspect, :php:`\TYPO3\CMS\Core\Context\VisibilityAspect`, accepts the following properties: .. confval:: includeHiddenPages :name: visibility-aspect-includeHiddenPages :Call: :php:`$this->context->getPropertyFromAspect('visibility', 'includeHiddenPages');` Returns, whether hidden pages should be displayed, as boolean. .. _visibility-aspect-includehiddencontent: includeHiddenContent ~~~~~~~~~~~~~~~~~~~~ .. confval:: includeHiddenContent :name: visibility-aspect-includeHiddenContent :Call: :php:`$this->context->getPropertyFromAspect('visibility', 'includeHiddenContent');` Returns, whether hidden content should be displayed, as boolean. .. _visibility-aspect-includedeletedrecords: includeDeletedRecords ~~~~~~~~~~~~~~~~~~~~~ .. confval:: includeDeletedRecords :name: visibility-aspect-includeDeletedRecords :Call: :php:`$this->context->getPropertyFromAspect('visibility', 'includeDeletedRecords');` Returns, whether deleted records should be displayed, as boolean. .. _context-api-aspects-visibility-example: Example: Check whether hidden pages are included ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. literalinclude:: _MyControllerUsingVisibilityAspect.php :caption: EXT:my_extension/Classes/Controller/MyController.php .. _context-api-aspects-workspace: Workspace aspect ---------------- The aspect contains information about the currently accessed :ref:`workspace `. .. _context-api-aspects-workspace-properties: The workspace aspect, :php:`\TYPO3\CMS\Core\Context\WorkspaceAspect`, accepts the following properties: .. confval:: id :name: workspace-aspect-id :Call: :php:`$this->context->getPropertyFromAspect('workspace', 'id');` Returns the UID of the currently accessed workspace, as integer. .. _workspace-aspect-islive: isLive ~~~~~~ .. confval:: isLive :name: workspace-aspect-isLive :Call: :php:`$this->context->getPropertyFromAspect('workspace', 'isLive');` Returns whether the current workspace is live, or a custom offline workspace, as boolean. .. _workspace-aspect-isoffline: isOffline ~~~~~~~~~ .. confval:: isOffline :name: workspace-aspect-isOffline :Call: :php:`$this->context->getPropertyFromAspect('workspace', 'isOffline');` Returns, whether the current workspace is offline, as boolean. .. _context-api-aspects-workspace-example: Example: Read the ID of the current workspace ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. literalinclude:: _MyControllerUsingWorkspaceAspect.php :caption: EXT:my_extension/Classes/Controller/MyController.php