To use Extbase CategoryRepository
, to work with Categories
on our model we need to:
Add in ext_tables.sql
, in our model, the categories field categories int(11) unsigned DEFAULT '0' NOT NULL,
Make your model categorizable, by adding bellow snipet in ext_tables.php
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::makeCategorizable(
$_EXTKEY,
'tx_mymodels_domain_model_mymodel'
);
Create own Category
model and CategoryRepository
by extending extabase Category
and CategoryRepository
namespace MyVendor\MyModels\Domain\Model;
class Category extends \TYPO3\CMS\Extbase\Domain\Model\Category {}
namespace MyVendor\MyModels\Domain\Repository;
class CategoryRepository extends \TYPO3\CMS\Extbase\Domain\Repository\CategoryRepository {}
In your MyModelController
inject CategoryRepository
/**
* CategoryRepository
*
* @var \MyVendor\MyModels\Domain\Repository\CategoryRepository
* @inject
*/
protected $categoryRepository = null;
Add to your model categories
field with getters and setters
/**
* categories
*
* @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\MyVendor\MyModels\Domain\Model\Category>
* @lazy
*/
protected $categories;
/**
* Get categories
*
* @return \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\MyVendor\MyModels\Domain\Model\Category>
*/
public function getCategories()
{
return $this->categories;
}
/**
* Set categories
*
* @param \TYPO3\CMS\Extbase\Persistence\ObjectStorage $categories
* @return void
*/
public function setCategories($categories)
{
$this->categories = $categories;
}
In Configuration\TypoScript\setup.txt
map back your model to extbase category table
plugin.tx_mymodels.persistence.classes {
MyVendor\MyModels\Domain\Model\Category {
mapping {
recordType = 0
tableName = sys_category
}
}
}
Use this way:
// gets all categories, all across the site
$categoryRepository->findAll();
// gets categories for this model
$myModel->getCategories();
// get all models for specific category
$collection = \TYPO3\CMS\Frontend\Category\Collection\CategoryCollection::load(
$categoryId,
TRUE,
'tx_mymodels_domain_model_mymodel',
'categories'
);
If you want to take categories records only from a sys folder you need to configure repository:
defaultQuerySettings->setRespectStoragePage(true)
defaultQuerySettings->setStoragePageIds($storagePageIds);
I got also a strange behaviour in TYPO3 8.7.24 like schnoedet describes above.
If no user is logged in the backend, the categories not shown in FE. After login to the backend and clear the caches the categories shown in FE. But after a while, again no categories are shown in the FE.
UPDATE