Integration 

Integration with other extensions 

Models of other extensions (e.g. EXT:news) can be exposed in the API without changing their code:

  1. Create a model in your extension which extends the model of the other extension and add @T3api\ApiResource annotation to it.
  2. Map your model to the table of the original model with Extbase persistence configuration (Configuration/Extbase/Persistence/Classes.php). If your class should also be returned by the original repositories and relations, register it as subclasses / recordType as described in TYPO3 documentation.
  3. Configure serialization of properties of the original class with YAML metadata - annotations can not be added to classes you do not own.

Extbase creates related objects with the classes used in the original model (e.g. categories of a news are \GeorgRinger\News\Domain\Model\Category). Declare the type of such relation with your API resource class in YAML metadata - related objects are then serialized as that API resource, including its @id:

GeorgRinger\News\Domain\Model\News:
  properties:
    categories:
      type: 'TYPO3\CMS\Extbase\Persistence\ObjectStorage<SourceBroker\T3apinews\Domain\Model\Category>'
Copied!

News extension - Example integration 

Testing instance of t3api contains complete integration with EXT:news - extension t3apinews (see .ddev/test/files/src/t3apinews in t3api repository). It is a good starting point for your own integration:

  • Classes/Domain/Model/News.php, Category.php, Tag.php - API resources extending EXT:news models, with operations and filters.
  • Classes/Domain/Model/File.php - resource used for file upload.
  • Configuration/Extbase/Persistence/Classes.php - mapping of the models to EXT:news tables.
  • Resources/Private/Serializer/GeorgRinger.News/*.yml - YAML metadata with serialization groups for EXT:news models, registered in ext_localconf.php.
EXT:t3apinews/Configuration/Extbase/Persistence/Classes.php
use SourceBroker\T3apinews\Domain\Model\Category;
use SourceBroker\T3apinews\Domain\Model\News;
use SourceBroker\T3apinews\Domain\Model\Tag;

return [
    News::class => [
        'tableName' => 'tx_news_domain_model_news',
    ],
    Tag::class => [
        'tableName' => 'tx_news_domain_model_tag',
    ],
    Category::class => [
        'tableName' => 'sys_category',
    ],
];
Copied!

Inline output 

If you would like to include API response directly in TYPO3 HTML output (e.g. to avoid waiting for initial request to the API in your frontend application) you can use t3api:inline ViewHelper. It processes the request internally, without HTTP request, and returns the same JSON which would be returned by the endpoint.

Arguments:

  • route (string, required) - path of the endpoint, without API base path, e.g. news/news or news/news/1.
  • params (array) - query params, e.g. filters: {search: 'minima', order: {title: 'asc'}}.
  • itemsPerPage (int) - number of items per page (sent as itemsPerPage param).
  • page (int) - page number (sent as page param).
<html xmlns:t3api="http://typo3.org/ns/SourceBroker/T3api/ViewHelpers"
      data-namespace-typo3-fluid="true">

    <script type="application/json" id="initial-news">
        <f:format.raw><t3api:inline route="news/news" params="{istopnews: 'true'}" itemsPerPage="10" /></f:format.raw>
    </script>

</html>
Copied!