Skip to content

Commit 9a9b113

Browse files
s2blinawolf
authored andcommitted
[DOCS] Update developing custom ViewHelper (#6406)
* [DOCS] Update developing custom ViewHelper Releases: main, 13.4 * Update Documentation/ApiOverview/Fluid/DevelopCustomViewhelper.rst Co-authored-by: Chris Müller <2566282+brotkrueml@users.noreply.github.com> * [TASK] Language checks --------- Co-authored-by: Chris Müller <2566282+brotkrueml@users.noreply.github.com> Co-authored-by: Sarah McCarthy <sarahmccarthy123@yahoo.com> (cherry picked from commit b01dacb)
1 parent 6abce51 commit 9a9b113

File tree

1 file changed

+39
-16
lines changed

1 file changed

+39
-16
lines changed

Documentation/ApiOverview/Fluid/DevelopCustomViewhelper.rst

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ user input.
144144
:php:`htmlspecialchars()` as we have
145145
:ref:`disabled escaping <fluid-viewhelper-custom-escaping-of-output>`.
146146

147-
When escapting is diabled, the `render()` method is responsible to prevent
147+
If escaping is disabled, the `render()` method is responsible for preventing
148148
:ref:`XSS attacks <prevent-cross-site-scripting>`.
149149

150150
Therefore all arguments **must** be sanitized before they are returned.
@@ -385,6 +385,13 @@ ViewHelpers can have one or more of the following methods for
385385
implementing the rendering. The following section will describe the differences
386386
between the implementations.
387387

388+
.. _render-method:
389+
390+
`render()` method
391+
-----------------
392+
393+
This method is usually implemented.
394+
388395
.. _compile-method:
389396

390397
`compile()`-Method
@@ -484,39 +491,52 @@ ViewHelper's `renderStatic()` method you can replace the code like this:
484491

485492
.. _fluid-custom-viewhelper-access:
486493

487-
How to access classes in the ViewHelper implementation
494+
How to access objects in the ViewHelper implementation
488495
======================================================
489496

490497
Custom ViewHelper implementations support
491498
`Dependency injection <https://docs.typo3.org/permalink/t3coreapi:dependency-injection>`_.
492499

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>`_.
500+
You can, for example, inject the :php-short:`\TYPO3\CMS\Core\Configuration\ExtensionConfiguration`
501+
service to get the configuration of an installed extension. Note that ViewHelpers
502+
should avoid getting additional data from the database because this
503+
violates the separation of data (model) and presentation (view).
495504

496505
Some objects depend on the current context and can be fetched from
497506
the rendering context:
498507

499-
.. note::
500-
This list is not complete, please help us with more examples.
501-
502508
.. _fluid-custom-viewhelper-access-request:
503509

504510
Accessing the current Request in a ViewHelper implementation
505511
------------------------------------------------------------
506512

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`:
513+
514+
The current
515+
:php-short:`\Psr\Http\Message\ServerRequestInterface` object can be fetched
516+
from the :php-short:`TYPO3\CMS\Fluid\Core\Rendering\RenderingContext` inside
517+
the `render()` method in a ViewHelper implementation,:
510518

511519
.. code-block:: php
512520
:caption: EXT:my_extension/Classes/ViewHelpers/SomeViewHelper.php
513521
514-
public function render()
522+
use Psr\Http\Message\ServerRequestInterface;
523+
524+
public function render(): string
515525
{
516-
$request = $this->renderingContext->getRequest();
526+
if ($this->renderingContext->hasAttribute(ServerRequestInterface::class)) {
527+
$request = $this->renderingContext->getAttribute(ServerRequestInterface::class);
528+
}
517529
return 'Hello World!';
518530
}
519531
532+
Note that the request may not be available in all Fluid template contexts, so
533+
use `hasAttribute()` first to check if it exists.
534+
535+
The request may contain other attributes that are relevant for your
536+
ViewHelper's use case. See
537+
`TYPO3 request attributes <https://docs.typo3.org/permalink/t3coreapi:request-attributes>`_
538+
for a list of available attributes.
539+
520540
.. _fluid-custom-viewhelper-access-contentObject:
521541

522542
Using stdWrap / fetching the current ContentObject in a ViewHelper implementation
@@ -530,11 +550,14 @@ from the :php-short:`\Psr\Http\Message\ServerRequestInterface`:
530550
531551
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
532552
533-
public function render()
553+
public function render(): string
534554
{
535-
$request = $this->renderingContext->getRequest();
536-
$cObj = $request->getAttribute('currentContentObject');
537-
return $cObj->stdWrap('Hello World', ['wrap' => '|!']);
555+
if ($this->renderingContext->hasAttribute(ServerRequestInterface::class)) {
556+
$request = $this->renderingContext->getAttribute(ServerRequestInterface::class);
557+
$cObj = $request->getAttribute('currentContentObject');
558+
return $cObj->stdWrap('Hello World', ['wrap' => '|!']);
559+
}
560+
return '';
538561
}
539562
540563
.. deprecated:: 13.4

0 commit comments

Comments
 (0)