Use content element relations in TYPO3 extensions

Wouldn’t it be cool to enable editors to use standard or custom content elements in your extensions? The consequence is that you do not have to develop each and every functionality again. In this post I will show you, how you can use all available content elements of your installation.

A very prominent example is EXT:news. This is also the extension where I looked at, while developing a extension for a customer, as I needed a similar functionality. Thanks to Georg Ringer and open source, I am able to share some kind of howto about this topic. Here are two screenshots that show, how the content element relations will be shown on the backend:

typo3worx_ce-relations-example03

Multiple CE relations possible

typo3worx_ce-example-02

Inline editing of content elements

Prerequisites

1) A running TYPO3 instance

Hm, ok that sounds obvious, but for completeness I want to mention it here.

2) A minimal TYPO3 extension

In order to use this, you need at least a minimal TYPO3 extension, to which you can add this functionality. If you don’t have one and want to follow this howto codewise, please check the EXT:extension_builder and create an minimal one.

How to use content element relations in your TYPO3 extension

In the following sections I will show the various parts of you extension, which must be touched. If a mentioned file is not already there, you must create it.

Data structure: ext_tables.sql

First, we must tell TYPO3, where to store the content element relations. Therefore we need to extend the table ‚tt_content‘, add a column to the table of the extension containing the data and create an additional mm table. The necessary lines must be added in the file „ext_tables.sql“ of your extension.

TCA für content element relations

Then the content elements must be added to the TCA. Therefore the parts „interface“, „types“ and „columns“ must be touched. As I did not want to add the complete TCA here, you must replace the “[…]” with the parts of your code.

Domain – Model for tt_content

In order to use normal content elements, you must also provide a model in your extension for it. This happens in the file “EXT:myextension/Classes/Domain/Model/TtContent.php“. It provides all setter and getter methods for the table „tt_content“. As the complete source code would be too much for this post: there is a gist for it: https://gist.github.com/mschwemer/f0814450009204fc9a16d05957d77226

Repository for tt_content

Additional to the domain model the repository must be also provided. This is the purpose of the file: “EXT:myextension/Classes/Domain/Repository/TtContentRepository.php“. This is the code, you need:

Main Model of your extension

In the next step the content elements must be made reachable by your main domain model. Basically it is the normal stuff like defining the property, adding the functions add*, remove* and the getter. But what you in addition is the function “getContentElementIdList“. This return all the IDs which are associated with your record. The output is needed in the next snippet.

TypoScript

This is the TypoScript – Template, that defines the selection of the records, based on the table „tt_content“. It looks quite simple, but is an essential part reusing content elements. It is needed for the fluid template

Fluid – Template

The fluid template finally renders the content elements with in the plugin of your extension to the frontend.

Conclusion

I think this is a nice way to use and add normal content elements in a custom extension. This way you don’t have to re-invent the wheel again and again and blow up your model. Currently all available content elements are allowed. I can imagine, that in most cases a restriction to certain content elements or plugins would be helpful. I did not dig deeper into it, but would be probably worth another post. What do you think?

I want to thank Georg Ringer using this code in his extension “news” and sharing it with us all. It always a good inspiration for me!

Credits
The blogpost image was originally published on pixabay by Snufkin using the CC0 Public Domain License. I modified using Pablo by Buffer.

5 Comments

  1. Great article. Thanks for sharing. Thus with this enhancement I can have e.g. text / media CE in my own extension without much effort, right? For me one or two screenshots of the practical use would make it easier to understand.

    Reply
  2. Hi Markus,

    thank you very much for writing this topic down. One thing I would like to add to this is the mapping of the own TtContent model in the extension´s ext_typoscript_setup.txt.

    Without that I got the error that table tx_myextension_domain_model_ttcontent does not exist.

    Regards,

    Ralf

    Reply

Leave a Comment.