Skip to content

Commit 56cab14

Browse files
committed
[DOCS] Document writing an extension
and extending the templates
1 parent d6a1f39 commit 56cab14

File tree

6 files changed

+187
-1
lines changed

6 files changed

+187
-1
lines changed

docs/extension/_YourExtension.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace T3Docs\Typo3DocsTheme\DependencyInjection;
6+
7+
use Symfony\Component\Config\FileLocator;
8+
use Symfony\Component\DependencyInjection\ContainerBuilder;
9+
use Symfony\Component\DependencyInjection\Extension\Extension;
10+
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
11+
12+
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
13+
use function dirname;
14+
15+
class Typo3DocsThemeExtension extends Extension implements PrependExtensionInterface
16+
{
17+
/** @param mixed[] $configs */
18+
public function load(array $configs, ContainerBuilder $container): void
19+
{
20+
}
21+
22+
public function prepend(ContainerBuilder $container): void
23+
{
24+
$loader = new PhpFileLoader(
25+
$container,
26+
new FileLocator(dirname(__DIR__, 2) . '/resources/config'),
27+
);
28+
$loader->load('your-extension.php');
29+
$container->prependExtensionConfig('guides', [
30+
'themes' => [
31+
'typo3docs' => [
32+
'extends' => 'bootstrap',
33+
'templates' => [dirname(__DIR__, 2) . '/resources/template'],
34+
],
35+
],
36+
]);
37+
}
38+
}

docs/extension/_composer.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "t3docs/typo3-docs-theme",
3+
"description": "A theme for use in the phpdocumentor",
4+
"type": "library",
5+
"license": "MIT",
6+
"homepage": "https://docs.typo3.org",
7+
"autoload": {
8+
"psr-4": {
9+
"T3Docs\\Typo3DocsTheme\\": "src/"
10+
}
11+
},
12+
"require": {
13+
"phpdocumentor/guides-theme-bootstrap": "dev-main"
14+
}
15+
}

docs/extension/_your-extension.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
6+
use YourVendor\YourExtension\Directives\SomeDirective;
7+
8+
return static function (ContainerConfigurator $container): void {
9+
$container->services()
10+
->defaults()
11+
->autowire()
12+
->autoconfigure()
13+
->set(SomeDirective::class)
14+
->tag('phpdoc.guides.directive');
15+
};

docs/extension/index.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ which you invoke the CLI.
1414

1515
It should look like this::
1616

17-
.. code-block:: xml
17+
.. code-block:: xml
18+
:caption: your_project/guides.xml
1819
1920
<?xml version="1.0" encoding="UTF-8" ?>
2021
<guides>
@@ -30,4 +31,6 @@ Some ways to extend the guides:
3031

3132
.. toctree::
3233

34+
structure
35+
templates
3336
text-roles

docs/extension/structure.rst

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
.. include:: /include.rst.txt
2+
3+
.. _extension_structure:
4+
5+
=================
6+
General Structure
7+
=================
8+
9+
You can extend the guides with your own Composer-based Symfony extension.
10+
11+
.. _extension_composer:
12+
13+
composer.json
14+
=============
15+
16+
Each Composer package must have a file `composer.json`. See an example here:
17+
18+
.. literalinclude:: _composer.json
19+
:language: json
20+
:caption: your-extension/composer.json
21+
:lineos:
22+
23+
The PHP sources can be found in the directory `src` then as is stated in line 8
24+
in the `composer.json`.
25+
26+
.. _extension_symfony:
27+
28+
Create an extension
29+
===================
30+
31+
For the PHP package to be an extension you need a class
32+
extending `\Symfony\Component\DependencyInjection\Extension\Extension` by
33+
implementing the interface
34+
`Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface` we
35+
can also add our ow configurations to our extension:
36+
37+
.. literalinclude:: _YourExtension.php
38+
:language: php
39+
:caption: your-extension/DependencyInjection/YourExtension.php
40+
:lineos:
41+
42+
Lines 24 to 28 load a :ref:`Dependency Injection configuration <extension_di_configuration>`
43+
file. Lines 29 to 36 configure the directory overriding the default templates.
44+
Read chapter :ref:`extending_templates` to learn more about this.
45+
46+
.. note::
47+
This is a Symfony extension, not a TYPO3 extension. See also the
48+
`Symfony documentation about Extensions <https://symfony.com/doc/current/bundles/extension.html>`__.
49+
50+
.. _extension_di_configuration:
51+
52+
Dependency Injection configuration
53+
===================================
54+
55+
.. literalinclude:: _your-extension.php
56+
:language: php
57+
:caption: your-extension/resources/config/your-extension.php
58+
:lineos:
59+
60+
This is the place to register custom classes such as directives, text roles,
61+
custom compiler passes etc. You can also read about configuration files here:
62+
https://symfony.com/doc/current/bundles/configuration.html

docs/extension/templates.rst

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
.. include:: /include.rst.txt
2+
3+
.. _extending_templates:
4+
5+
===================
6+
Extending Templates
7+
===================
8+
9+
Register the templates overrides in your extensions
10+
:ref:`Extension class <extension_symfony>`:
11+
12+
.. literalinclude:: _YourExtension.php
13+
:language: php
14+
:caption: your-extension/DependencyInjection/YourExtension.php
15+
:lineos:
16+
:emphasize-lines: 29-35
17+
18+
It is recommended to always extend an existing template so that the base
19+
templates twig files can be used as fallback for non-defined specific template
20+
files.
21+
22+
In order to extend the default bootstrap theme, require the according base
23+
extension in your extension's `composer.json`:
24+
25+
.. code-block:: json
26+
:caption: your-extension/composer.json
27+
28+
{
29+
"name": "t3docs/typo3-docs-theme",
30+
"...": "...",
31+
"require": {
32+
"phpdocumentor/guides-theme-bootstrap": "dev-main"
33+
}
34+
}
35+
36+
And then set `'extends' => 'bootstrap'` (line 32 in the first code-snippet).
37+
38+
To extend the base template (plain HTML) require `phpdocumentor/guides` in your
39+
`composer.json`, and omit the key `extends`:
40+
41+
42+
.. code-block:: php
43+
:caption: your-extension/DependencyInjection/YourExtension.php
44+
45+
$container->prependExtensionConfig('guides', [
46+
'themes' => ['mytheme' => dirname(__DIR__, 3) . '/resources/template'],
47+
]);
48+
49+
You can now copy any twig file from the extended extensions and change it with
50+
the full power of `Twig <https://twig.symfony.com/>`__.
51+
52+
Have a look at the `custom theme for TYPO3 Documentation <https://github.com/TYPO3-Documentation/typo3-docs-theme>`__
53+
for an example on how to create a theme which also features custom directives.

0 commit comments

Comments
 (0)