.. include:: /Includes.rst.txt .. _developer: ===================== Developer Information ===================== This chapter covers the technical aspects of extending and integrating with the Contexts extension. .. _developer-custom-context: Creating Custom Context Types ============================= You can create custom context types by implementing the context interface and registering your type. Step 1: Create the Context Class -------------------------------- Extend ``AbstractContext`` and implement the ``match()`` method: .. code-block:: php getConfValue('myField'); $actualValue = $this->getActualValue(); // Use invert() and storeInSession() for proper behavior return $this->invert($this->storeInSession( $configuredValue === $actualValue )); } private function getActualValue(): string { // Access the PSR-7 request via Container $request = Container::get()->getRequest(); if ($request === null) { return ''; } // Example: get a query parameter $params = $request->getQueryParams(); return $params['myParam'] ?? ''; } } .. tip:: The ``AbstractContext`` provides helper methods: - ``getConfValue($fieldName)`` - Get FlexForm configuration value - ``invert($match)`` - Apply inversion setting if enabled - ``storeInSession($match)`` - Cache result in user session - ``getMatchFromSession()`` - Retrieve cached match result Step 2: Register the Context Type --------------------------------- In your :file:`ext_localconf.php`: .. code-block:: php MyCustomContext::class, 'label' => 'LLL:EXT:my_extension/Resources/Private/Language/locallang.xlf:context.my_custom', ]; Step 3: Add TCA Configuration ----------------------------- In :file:`Configuration/TCA/Overrides/tx_contexts_contexts.php`: .. code-block:: php 'type, title, --palette--;;visibility, my_field', ]; \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addTCAcolumns( 'tx_contexts_contexts', [ 'my_field' => [ 'label' => 'My Field', 'config' => [ 'type' => 'input', ], ], ] ); .. _developer-events: PSR-14 Events ============= .. versionadded:: 4.0.0 PSR-14 events replace legacy SC_OPTIONS hooks. The extension dispatches several PSR-14 events that you can listen to. ContextMatchEvent ----------------- Dispatched when a context is being matched. .. code-block:: php getContext(); $matches = $event->getMatches(); // Modify matching behavior if ($this->shouldOverride($context)) { $event->setMatches(true); } } } .. _developer-api: Context API =========== Checking Contexts Programmatically ---------------------------------- The ``Container`` class provides access to all matched contexts. It uses a singleton pattern and extends ``ArrayObject`` for easy iteration. .. code-block:: php find('my-context-alias'); if ($context !== null) { // Context exists and is active (matched) } // Iterate over all active (matched) contexts foreach (Container::get() as $uid => $context) { echo $context->getAlias() . ' is active'; } Using the ContextMatcher API ---------------------------- For simple matching checks, use the ``ContextMatcher`` API which caches results: .. code-block:: php matches('mobile')) { // Mobile context is active } TypoScript Conditions --------------------- Use the ``contextMatch()`` function in TypoScript conditions: .. code-block:: typoscript # Show content only when mobile context is active [contextMatch('mobile')] page.10.wrap =
Mobile context is active
Mobile context is not active