---
title: "Code snippet generation"
manual: "How to Document"
version: "main"
permalink: "https://docs.typo3.org/permalink/h2document:codesnippet-generation"
source: "Maintainers/Codesnippets.rst"
modified: "2026-09-14T09:38:54+00:00"
---

# Code snippet generation

## Add a code snippet for a new class

The API section of a manual page (for example a PSR-14 event page) is
usually not written by hand: it is generated from the class's own PHP
doc-comments and method signatures by [t3docs-codesnippets](https://github.com/TYPO3-Documentation/t3docs-codesnippets), and then
included into the page with:

```rst
..  include:: /CodeSnippets/Events/Core/SomeEvent.rst.txt
```

To make the generator pick up a new class, register it in the manual's
`Documentation/CodeSnippets/Config/` tree. For events this is one PHP
file per category, for example
`Documentation/CodeSnippets/Config/Api/Events/EventsCore.php` for
[`typo3/cms-core`](https://packagist.org/packages/typo3/cms-core), returning an array of entries:

**Documentation/CodeSnippets/Config/Api/Events/EventsCore.php**

```php
[
    'action' => 'createPhpClassDocs',
    'class' => \TYPO3\CMS\Core\Routing\Event\AfterPageUriGeneratedEvent::class,
    'targetFileName' => 'CodeSnippets/Events/Core/AfterPageUriGeneratedEvent.rst.txt',
    'withCode' => false,
],
```

> [!WARNING]
> **Attention**
>
> `targetFileName` is flat per top-level category
> (`CodeSnippets/Events/Core/...`), regardless of any subfolder the
> corresponding manual page itself lives in (for example
> `ApiOverview/Events/Events/Core/Routing/...`). Match the existing
> files in the same category rather than mirroring the page's own path.

If there is no config file for the class's category yet, create one
following the pattern above and add it to that tree's `All.php`
aggregator so it is picked up.

After adding the entry, run the class through the generator (see
[Regenerate existing code snippets](https://docs.typo3.org/permalink/h2document:codesnippet-regeneration) below)
to create the actual `.rst.txt` file. If you cannot run the generator
locally, you can write the file by hand in the same format so the page
renders correctly in the meantime - the next real generator run will
overwrite it with equivalent content, so this is not destructive.

## Regenerate existing code snippets

To regenerate the existing code snippets in a manual or extension clone
the repository of that manual.

```bash
git clone git@github.com:TYPO3-Documentation/TYPO3CMS-Reference-CoreApi.git
```

Use composer to install all dependencies,
including dev-dependencies. In official manuals we have a make command for
your convenience.

**Make**

```bash
make install
```

**composer**

```bash
# in repositories with composer.lock in version control
composer install

# in repositories where the composer.lock is not in version control
# to prevent outdated packages due to a local composer.lock from earlier install-runs
composer update
```

Then regenerate the code snippets. Official manuals should have a make command
for that.

**Make**

```bash
make codesnippets
```

**TYPO3 console command**

```bash
# Paths depends on where the vendor binaries are installed
# And where the configuration file codesnippets.php is located

vendor/bin/typo3 codesnippet:create Documentation/
```
