Delivering Extbase plugins in a headless page
A plugin is the one kind of content element whose fields do not describe its output. Its content element holds only the configuration an editor chose, such as a selection or a storage folder. The output comes from the plugin itself, and an Extbase plugin renders HTML by default.
In the JSON page type from Delivering a TYPO3 page as JSON, the plugin has to deliver JSON
instead, which the page template inserts as the data of its content
element. This page shows how the plugin is rendered for its content element,
how its action answers with JSON, and what changes when the action is not
cacheable.
A conference list plugin in the content area of a page delivers this:
{
"content": {
"main": [
{
"uid": 41,
"type": "myextension_conferencelist",
"header": "Upcoming conferences",
"headerLink": "",
"data": [
{
"uid": 12,
"title": "Community Conference Europe",
"link": "https://example.org/conferences/conference/12"
},
{
"uid": 15,
"title": "Community Conference Asia",
"link": "https://example.org/conferences/conference/15"
}
]
}
]
}
}
On this page
Rendering an Extbase plugin for its content element in JSON
The content element partial from Delivering content elements as JSON gets a section for the plugin's content type. The section renders the plugin with the f:cObject ViewHelper:
<f:section name="myextension_conferencelist">
<f:cObject
typoscriptObjectPath="tt_content.myextension_conferencelist.20"
data="{record}"
table="tt_content"
/>
</f:section>
Extension registers the TypoScript
tt_ for the plugin's content
type. It renders the content element with its frame HTML, and the plugin
itself in
20
. The section renders only
20
, so
the output is the plugin's and nothing else.
data=" and
table="tt_ render the plugin for
the content element at hand. The plugin reads its configuration from that
record just as it does in the HTML page: the settings the editor chose in its
FlexForm, and its storage folder from the field Record Storage Page.
One plugin can therefore deliver different selections on different pages,
without a line of configuration in the page type.
The section writes the output of the plugin as it is, without
f:. The plugin delivers JSON already, and encoding it again
would turn it into a string.
Letting an Extbase plugin answer with JSON templates
The conference list plugin also renders the conference list of HTML pages, and
an editor places it the same way on every page. The plugin needs the same
actions for both:
list and
show, configured as
usual:
<?php
use MyVendor\MyExtension\Controller\ConferenceController;
use TYPO3\CMS\Extbase\Utility\ExtensionUtility;
defined('TYPO3') or die();
ExtensionUtility::configurePlugin(
'MyExtension',
'ConferenceList',
[ConferenceController::class => ['list', 'show']],
);
What differs is the representation. In the JSON page type, the plugin renders
its actions with JSON templates instead of HTML templates. A TypoScript
condition sets the format
of the plugin to json for that page type only:
[request.getPageArguments().getPageType() == 1762214410]
plugin.tx_myextension_conferencelist.format = json
[end]
With the format json, Fluid looks for the template
Conference/ instead of Conference/.
The controller stays as it is. The JSON templates follow the same rules as
the templates of the page, see Writing JSON in a Fluid template:
{escaping off}
[
<f:for each="{conferences}" as="conference" iteration="iteration">
{
"uid": <f:format.json value="{conference.uid}" />,
"title": <f:format.json value="{conference.title}" />,
"link": <f:format.json value="{f:uri.action(action: 'show', arguments: {conference: conference})}" />
}<f:if condition="{iteration.isLast}"><f:else>,</f:else></f:if>
</f:for>
]
{escaping off}
{
"uid": <f:format.json value="{conference.uid}" />,
"title": <f:format.json value="{conference.title}" />,
"description": <f:format.json value="{conference.description}" />
}
The format applies to the plugin only within the condition, so the HTML pages keep rendering the HTML templates.
The same approach delivers a plugin of another extension as JSON. Its JSON templates go into a template path of the site package, added with a higher key than the paths of the extension, see Overriding templates of a third-party extension. The extension itself does not need to change.
The action may still return
html. Its content type does not
reach the visitor, because the
additional of the JSON
page type are applied last and set application/.
Letting an Extbase plugin answer with JsonView
A plugin that exists only for the headless frontend can render its data with
JsonView instead of templates. The action
shapes the data as an array and returns
json, as the
seats example
does. The output is inserted into the page JSON in the same way.
Such a plugin sets the content type application/ itself. On an HTML
page, that content type applies to the whole page, see
An action returning JSON breaks the page. Use JsonView for plugins that
are rendered in the JSON page type only, and JSON templates for plugins that
appear on HTML pages as well.
Cacheable and non-cacheable Extbase plugins in a JSON page
The output of a cacheable action is stored in the page cache together with the rest of the page JSON. A request for the same page receives it from the cache, without running the action.
A non-cacheable action is rendered as a
USER_INT object. TYPO3 caches
the page JSON with a placeholder where the plugin output belongs, and replaces
the placeholder on every request with the output of the action. This works in
the JSON page because the section writes the plugin output as it is. Passed
through
f:, the placeholder would be encoded as a string,
TYPO3 would no longer find it, and the JSON would contain the placeholder
instead of the plugin output.
A page with a non-cacheable plugin is sent with the header
Cache-, so neither the browser nor a proxy keeps a
copy of the page JSON. Values that change on every request, such as the seats
left in a conference, can therefore be cheaper to deliver from an endpoint of
their own, which the frontend requests separately. Loading Extbase data
with fetch() builds such an endpoint.
Links and detail pages of Extbase plugins in a JSON page
The links an Extbase plugin creates, with
f: or the
Uri, point to the page as HTML, not to the JSON page type. The
JSON page type sets
force, so they are absolute.
For the frontend, these are the URLs of its own pages. It shows the URL
https:// with its own host, and
requests the JSON of that page by appending the suffix:
https://. The PageType
decorator and the
route enhancer of the plugin
work together, so TYPO3 resolves the page, the plugin runs
show for conference 12, and the page JSON contains its output.
Next steps after delivering Extbase plugins as JSON
The page type now delivers complete pages, including the output of their plugins. Connecting a headless frontend to TYPO3 describes what the frontend has to take care of when it requests them: mapping URLs, requests from the browser of another origin, frontend users, caching, and previews for editors.