Skip to content

Commit 77f3d8c

Browse files
[TASK] How to access the request, cobject and DB in a ViewHelper (#5604)
* [TASK] How to access the request, cobject and DB in a ViewHelper Releases: main, 13.4 * [TASK] Language checks Releases: main --------- Co-authored-by: Sarah McCarthy <[email protected]>
1 parent 85102c9 commit 77f3d8c

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

Documentation/ApiOverview/Fluid/DevelopCustomViewhelper.rst

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,3 +481,65 @@ ViewHelper's `renderStatic()` method you can replace the code like this:
481481
by calling `$this->renderingContext->getViewHelperInvoker()->invoke()` instead.
482482

483483
See also :php:`TYPO3Fluid\Fluid\Core\ViewHelper\ViewHelperInvoker`.
484+
485+
.. _fluid-custom-viewhelper-access:
486+
487+
How to access classes in the ViewHelper implementation
488+
======================================================
489+
490+
Custom ViewHelper implementations support
491+
`Dependency injection <https://docs.typo3.org/permalink/t3coreapi:dependency-injection>`_.
492+
493+
You can, for example, inject the :php-short:`\TYPO3\CMS\Core\Database\ConnectionPool`
494+
to access the database by using the `database abstraction layer DBAL <https://docs.typo3.org/permalink/t3coreapi:doctrine-dbal>`_.
495+
496+
Some objects depend on the current context and can be fetched from
497+
the rendering context:
498+
499+
.. note::
500+
This list is not complete, please help us with more examples.
501+
502+
.. _fluid-custom-viewhelper-access-request:
503+
504+
Accessing the current Request in a ViewHelper implementation
505+
------------------------------------------------------------
506+
507+
You can use a `render()` method in the ViewHelper implementation to get the
508+
current :php-short:`\Psr\Http\Message\ServerRequestInterface` object
509+
from the :php-short:`TYPO3\CMS\Fluid\Core\Rendering\RenderingContext`:
510+
511+
.. code-block:: php
512+
:caption: EXT:my_extension/Classes/ViewHelpers/SomeViewHelper.php
513+
514+
public function render()
515+
{
516+
$request = $this->renderingContext->getRequest();
517+
return 'Hello World!';
518+
}
519+
520+
.. _fluid-custom-viewhelper-access-contentObject:
521+
522+
Using stdWrap / fetching the current ContentObject in a ViewHelper implementation
523+
---------------------------------------------------------------------------------
524+
525+
You can `access the ContentObjectRenderer <https://docs.typo3.org/permalink/t3coreapi:tsfe-contentobjectrenderer>`_
526+
from the :php-short:`\Psr\Http\Message\ServerRequestInterface`:
527+
528+
.. code-block:: php
529+
:caption: EXT:my_extension/Classes/ViewHelpers/SomeViewHelper.php
530+
531+
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
532+
533+
public function render()
534+
{
535+
$request = $this->renderingContext->getRequest();
536+
$cObj = $request->getAttribute('currentContentObject');
537+
return $cObj->stdWrap('Hello World', ['wrap' => '|!']);
538+
}
539+
540+
.. deprecated:: 13.4
541+
The class :php-short:`\TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController`
542+
and its global instance :php:`$GLOBALS['TSFE']`, which were formerly used to fetch the
543+
ContentObjectRenderer, have been marked as
544+
deprecated. The class will be removed in TYPO3 v14. See
545+
`TSFE <https://docs.typo3.org/permalink/t3coreapi:tsfe>`_ for migration steps.

0 commit comments

Comments
 (0)