USER and USER_INT
This calls either a PHP function or a method in a class. This is very useful if you want to incorporate your own data processing or content.
Basically USER and USER_INT are user defined cObjects, because they call a function or method, which you control!
If you call a method in a class (which is of course instantiated as an
object), the internal variable $c
of that class is set with a
reference to the parent cObject. This offers you an API of functions,
which might be more or less relevant for you. See
Content
in the TYPO3 source code; access to typolink
or std
are only two of the gimmicks you get.
If you create this object as USER_
, it will be rendered non-cached,
outside the main page-rendering.
Properties
userFunc
-
- Type
- function name
The name of the function, which should be called. If you specify the name with a '->' in it, then it is interpreted as a call to a method in a class.
Three parameters are sent to the PHP function: First a
string $content
variable (which is empty for USER/USER_INT objects, but not when the user function is called from stdWrap functions .postUserFunc or .preUserFunc). The second parameter is an array ($configuration
) with the properties of this cObject, if any. As third parameter, the currentServer
is passed.Request Interface $request
Note
The $request
object should be used to access request related variables instead of directly accessing
the superglobal variables like $_
/ $_
/ $_
, or TYPO3’s API methods General
and General
.
(properties you define)
-
- Type
- (the data type you want)
Apart from the properties "userFunc" and "stdWrap", which are defined for all USER/USER_INT objects by default, you can add additional properties with any name and any data type to your USER/USER_INT object. These properties and their values will then be available in PHP; they will be passed to your function (in the second parameter). This allows you to process them further in any way you wish.
stdWrap
-
- Type
- stdWrap.
cache
-
- Type
- cache
See cache function description for details.
Examples
Attention
For the best result you should always, without exception, place your class files in an extension, define composer class loading for this extension and add this extension as a dependency of your project. Then, your classes will load without issues when you refer to them by their class name.
Example 1
This example shows how to include your own PHP script and how to use it from TypoScript. Use this TypoScript configuration:
page = PAGE
page.10 = USER_INT
page.10 {
userFunc = MyVendor\SitePackage\UserFunctions\ExampleTime->printTime
}
The file EXT:
might
amongst other things contain:
<?php
declare(strict_types=1);
namespace MyVendor\SitePackage\UserFunctions;
use Psr\Http\Message\ServerRequestInterface;
final class ExampleTime
{
/**
* Output the current time in red letters
*
* @param string Empty string (no content to process)
* @param array TypoScript configuration
* @param ServerRequestInterface $request
* @return string HTML output, showing the current server time.
*/
public function printTime(string $content, array $conf, ServerRequestInterface $request): string
{
return '<p style="color: red;">Dynamic time: ' . date('H:i:s') . '</p><br />';
}
}
Here page.
will give back what the PHP function print
returned. Since we did not use a USER
object, but a
USER_
object, this function is executed on every page hit.
Thus, in this example, the current time is displayed in red letters each time.
Example 2
Now let us have a look at another example:
We want to display all content element headers of a page in reversed order. For this we use the following TypoScript:
page = PAGE
page.typeNum = 0
page.30 = USER
page.30 {
userFunc = MyVendor\SitePackage\UserFunctions\ExampleListRecords->listContentRecordsOnPage
# reverseOrder is a boolean variable (see PHP code below)
reverseOrder = 1
}
The file EXT:
may contain amongst other things:
<?php
declare(strict_types=1);
namespace MyVendor\SitePackage\UserFunctions;
use Psr\Http\Message\ServerRequestInterface;
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
/**
* Example of a method in a PHP class to be called from TypoScript
*
* The class is defined as public as we use dependency injection in
* this example. If you do not need dependency injection, the
* "Autoconfigure" attribute should be omitted!
*/
#[Autoconfigure(public: true)]
final class ExampleListRecords
{
public function __construct(
private readonly ConnectionPool $connectionPool,
) {}
/**
* Reference to the parent (calling) cObject set from TypoScript
*/
private ContentObjectRenderer $cObj;
public function setContentObjectRenderer(ContentObjectRenderer $cObj): void
{
$this->cObj = $cObj;
}
/**
* List the headers of the content elements on the page
*
* @param string Empty string (no content to process)
* @param array TypoScript configuration
* @return string HTML output, showing content elements (in reverse order, if configured)
*/
public function listContentRecordsOnPage(string $content, array $conf, ServerRequestInterface $request): string
{
$connection = $this->connectionPool->getConnectionForTable('tt_content');
$result = $connection->select(
['header'],
'tt_content',
['pid' => $request->getAttribute('frontend.page.information')->getId()],
[],
['sorting' => $conf['reverseOrder'] ? 'DESC' : 'ASC'],
);
$output = [];
foreach ($result as $row) {
$output[] = $row['header'];
}
return implode('<br>', $output);
}
}
Since we need an instance of the Content
class, we are using
the set
method to get it and store it in the
c
class property for later use.
page.
will give back what the function list
of
the class YourClass returned. This example returns some debug output
at the beginning and then the headers of the content elements on the
page in reversed order. Note how we defined the property
"reverseOrder" for this USER object and how we used it in the PHP code.
Example 3
Another example can be found in the documentation of the stdWrap
property postUserFunc There you can also see how to work with
$c
, the reference to the parent (calling) cObject.
Example 4
PHP has a function gethostname
to "get the standard host name for
the local machine". You can make it available like this:
page.20 = USER_INT
page.20 {
userFunc = MyVendor\SitePackage\UserFunctions\Hostname->getHostname
}
Contents of EXT:
:
<?php
declare(strict_types=1);
namespace Vendor\SitePackage\UserFunctions;
final class Hostname
{
/**
* Return standard host name for the local machine
*
* @param string Empty string (no content to process)
* @param array TypoScript configuration
* @return string HTML result
*/
public function getHostname(string $content, array $conf): string
{
return gethostname() ?: '';
}
}