Use categories in your TYPO3 extension

with the TYPO3 categories API

  • 6 LTS
  • 7 LTS
  • 8 LTS
  • 9 LTS
  • 10-dev

Category API

Since version 6.0 TYPO3 offers an API to make the use of categories in own extensions possible with little effort.

To activate the categories for a model of your own extension or an existing extension, the following code must be added to the corresponding file in the directory Configuration/TCA/Overrides/ of the extension. Please always put this code in the overrides and not in the TCA/file.

 

\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::makeCategorizable(
    'my_ext',
    'tx_myext_domain_model_example',
    'categories' // this parameter is optional! default value is categories
);

 

 

In the first parameter we pass the extension key. Second, the table name of the model. The following additional parameters can be added as an option: Third parameter: Field name in the model table for categories. Fourth parameter: Optional configuration. Fifth parameter: Overwrite and delete old configuration.

After adding the code, a Database Compare must be performed using the Install Tool or alternatively the extension must be deactivated and reactivated using the Extension Manager to add the new field.

Category API extbase frontend usage

The next step is the frontend usage. We´ll an extbase based AbstractEntity as example domain model.

 

<?php

namespace Kronovanet\MyExt\Domain\Model;

use TYPO3\CMS\Extbase\DomainObject\AbstractEntity;
use TYPO3\CMS\Extbase\Persistence\ObjectStorage;

class Example extends AbstractEntity
{
    /**
     * @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\CMS\Extbase\Domain\Model\Category>
     */
    protected $categories;

    public function __construct()
    {
        $this->categories = new ObjectStorage();
    }

    /**
     * @return ObjectStorage
     */
    public function getCategories(): ObjectStorage
    {
        return $this->categories;
    }
    
    /**
     * @param ObjectStorage $categories
     */
    public function setCategories(ObjectStorage $categories)
    {
        $this->categories = $categories;
    }   
}

 

Now we are ready to use those categories for everything we want.

Fluid template example (GitHub)

Use categories inside a FlexForm

If you want to define an output in your plugin using categories via the settings, you can also use categories as settings in a FlexForm.

Example:

Notice: You can define the settings name as you want. You aren´t forced to name it "categories".

 

<settings.categories>
 <label>LLL:EXT:mediapool/Resources/Private/Language/locallang.xlf:flex_forms.mediapool.categories</label>
  <config>
    <type>select</type>
    <renderType>selectTree</renderType>
    <foreign_table>sys_category</foreign_table>
    <foreign_table_where> AND sys_category.sys_language_uid IN (-1, 0) ORDER BY sys_category.sorting ASC</foreign_table_where>
    <size>10</size>
    <treeConfig>
      <parentField>parent</parentField>
      <appearance>
        <expandAll>true</expandAll>
        <showHeader>true</showHeader>
      </appearance>
    </treeConfig>
  </config>
</settings.categories>

 

Then you can use the user input inside your controller with $this->settings['categories'] or in your fluid template {settings.categories}.