TYPO3 v11 persistence.storagePid is ignored - typo3

I have an TYPO3 Extbase extension with TYPO3 11.
My problem is, that every item is shown.
In my plugin there is a Flexform Field for the storagePid. But the plugin completely ignored this settings and every time lists all items.
How can I tell my extension that it should only load items from the selected page?
I have tried to set this:
public function getItems()
{
$table = 'TABLE';
$query = $this->itemRepository->createQuery();
$query->getQuerySettings()->setRespectStoragePage(true);
return $query->execute();
}
But that doesn't help.
This doesn't change anything in the repository
public function initializeObject() {
/** #var Typo3QuerySettings $querySettings */
$querySettings = GeneralUtility::makeInstance(Typo3QuerySettings::class);
$querySettings->setRespectStoragePage(TRUE);
$this->setDefaultQuerySettings($querySettings);
}

I am assuming you mean what you set in the default field tt_content.pages and tt_content.recursive.
Here is an example how to get the page IDs from the fields: https://github.com/TYPO3/typo3/blob/11.5/typo3/sysext/felogin/Classes/Controller/AbstractLoginFormController.php#L35-L50
And then you would set that for your Extbase query:
$querySettings = $this->myRepository->createQuery()->getQuerySettings();
$querySettings->setStoragePageIds(...);
Background:
Here is a great write-up how naming a Flexform field persistence.storagePid would give you that, too https://www.derhansen.de/2016/02/how-extbase-determines-storagepid.html

Related

TYPO3 v9.5 Extbase Error Handling with routeEnhancers

In my Extbase TYPO3 Extension I want to show a custom fluid template when the record is not available anymore (hidden or deleted). The error handling loads a fluid template where the path is defined in the setup.typoscript.
But when I add the routEnhancers in site config.yaml file for my Extension then the Error handling doesnt work anymore and it just shows the default TYPO3 Error Page: "The requested page does not exist".
In the docs of the site config I didn't find any way to set a special error handling for my Ext.
Here is the Code that handles it so far:
Controller:
class RecordController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController
{
/**
* Error handling if no entry is found
*
* #param string $configuration configuration what will be done
* #throws \InvalidArgumentException
* #return string
*/
protected function handleNoRecordFoundError($configuration)
{
$statusCode = HttpUtility::HTTP_STATUS_404;
HttpUtility::setResponseCode($statusCode);
$this->getTypoScriptFrontendController()->set_no_cache('Record record not found');
$standaloneTemplate = GeneralUtility::makeInstance(StandaloneView::class);
$standaloneTemplate->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName($configuration));
return $standaloneTemplate->render();
}
/**
* #return TypoScriptFrontendController
*/
protected function getTypoScriptFrontendController()
{
return $GLOBALS['TSFE'];
}
/**
* action show
* #param \Digitalgizmo\Vehicles\Domain\Model\Vehicle $vehicle
* #return void
*
*
*/
public function showAction(\Vendor\MyExt\Domain\Model\Record $record = null)
{
if ($record !== null){
$this->view->assign('record', $record);
}
else {
$errorContent = $this->handleNoRecordFoundError($this->settings['show']['errorTemplate']);
if ($errorContent) {
return $errorContent;
}
}
}
}
config.yaml;
routeEnhancers:
MyExt:
type: Extbase
extension: MyExt
plugin: MyExt
routes:
-
routePath: '/staticName/{uid}/{slug}'
_controller: 'ControllerName::show'
_arguments:
slug: record
uid: id
defaultController: 'ControllerName::show'
aspects:
slug:
type: PersistedAliasMapper
tableName: tx_myext_domain_model_record
routeFieldName: slug
routeValuePrefix: /
uid:
type: PersistedAliasMapper
tableName: tx_myext_domain_model_record
routeFieldName: uid
The RoutEnhancer works just fine if the record is available.
How can I catch that error, so I can handle it and show my fluid template? My showAction isn't even being loaded (tested with XDebug). I assum this is because the TYPO3 core throws the error.
Everything seems fine with that code, the problem is that the RouteEnhancer is affected by the same constraints as your showAction: once the record is deleted, the resolve method in the routeEnhancer will no longer be able to find it nor its slug.
As a reference, see the resolve function in the API: https://api.typo3.org/9.5/_persisted_alias_mapper_8php_source.html . It instanciates a queryBuilder which, by default, builds a deleted=0 clause.
To get deleted redcords by their slug, what you need to do is build a custom RouteEnhancer, maybe by extending the PersistendAliasMapper class in a way that it also finds deleted records, refer https://docs.typo3.org/m/typo3/reference-coreapi/master/en-us/ApiOverview/Routing/ExtendingRouting.html , but be aware of the implications: the slug field in your model will no longer be able to find colliding slugs even with the eval=uniqueInSite option set because that, too, only sees non-deleted records.
Thanks to j4k3
I've created my own aspect type for the routeEnhancers, which removes the deleted and hidden constrains, so it won't throw an error.
I followed this documentation to create it: https://docs.typo3.org/m/typo3/reference-coreapi/master/en-us/ApiOverview/Routing/ExtendingRouting.html
Here is my CustomMapper Class.
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
use TYPO3\CMS\Core\Database\Query\Restriction\FrontendGroupRestriction;
use TYPO3\CMS\Core\Database\Query\Restriction\FrontendRestrictionContainer;
use TYPO3\CMS\Core\Database\Query\Restriction\HiddenRestriction;
use TYPO3\CMS\Core\Routing\Aspect\PersistedAliasMapper;
use TYPO3\CMS\Core\Utility\GeneralUtility;
class CustomMapper extends PersistedAliasMapper
{
protected function createQueryBuilder(): QueryBuilder
{
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable($this->tableName)
->from($this->tableName);
$queryBuilder->setRestrictions(
GeneralUtility::makeInstance(FrontendRestrictionContainer::class, $this->context)
);
// Frontend Groups are not available at this time (initialized via TSFE->determineId)
// So this must be excluded to allow access restricted records
$queryBuilder->getRestrictions()->removeByType(FrontendGroupRestriction::class);
$queryBuilder->getRestrictions()->removeByType(DeletedRestriction::class);
$queryBuilder->getRestrictions()->removeByType(HiddenRestriction::class);
return $queryBuilder;
}
}
Basically the only thing I added was the removal of the DeletedRestriction and HiddenRestriction.
Further more I had to change the Slug field how it gets built. I added the uid of the dataset to the slug and removed the separate uid GET Parameter, so now the slug is unique in the database. Before I did that I had the problem that the query found multiple of the same slug values and it always took the first one.
And now since the slug is unique in the database table it will return the object and wont throw an error, so I can handle the "error" in the controller.

Typo3 9.5 prefill form field with get parameter

I use TYPO3 system extension "form" and want to prefill an input field with a GET parameter.
This TYPO3 8.7. Form prefill input field is working, but only is no_cache=1. Is there another solution without deactivate the whole cache?
Thanks
david
Yes, you can but you need to create HOOK.
This is described in the documentation
For example, the HOOK
/**
* #param \TYPO3\CMS\Form\Domain\Model\Renderable\RenderableInterface $renderable
* #return void
*/
public function initializeFormElement(\TYPO3\CMS\Form\Domain\Model\Renderable\RenderableInterface $renderable)
{
if ($renderable->getUniqueIdentifier() === 'contactForm-text-1') {
$renderable->setDefaultValue('foo');
}
}
And the connect the hook
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/form']['initializeFormElement'][<useATimestampAsKeyPlease>]
= \VENDOR\YourNamespace\YourClass::class;
Please, read the documentation for "Form framework".
I did it and get results what I need.
You can disable the cache of the content column of your form page, for example:
lib.content = COA
lib.content{
10 < styles.content.get
}
[page["uid"] == 512]
lib.content = COA_INT
[global]
Thanks TYPO3UA for you answer. But you should use the hook 'afterBuildingFinished' because the 'initializeFormElement' hook is executed BEFORE the properties from the form definition are set in the form element. So the default values from the form definition (even it is an empty string) will override the values set in the initializeFormElement' hook.
See: https://forge.typo3.org/issues/82615
So this works for setting the default value of an form element:
/**
* #param \TYPO3\CMS\Form\Domain\Model\Renderable\RenderableInterface $renderable
* #return void
*/
public function afterBuildingFinished(\TYPO3\CMS\Form\Domain\Model\Renderable\RenderableInterface $renderable)
{
if (method_exists($renderable, 'getUniqueIdentifier') && $renderable->getUniqueIdentifier() === 'contactForm-text-1') {
$renderable->setDefaultValue('Value');
}
}
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/form']['afterBuildingFinished'][<useATimestampAsKeyPlease>]
= \VENDOR\YourNamespace\YourClass::class;

Extend tx_news with 1 field without extension_builder

I try to extend the tx_news extension with the field imageright.
For that I found this tutorial: https://docs.typo3.org/typo3cms/extensions/news/2.2.1/Main/Tutorial/ExtendingNews/Index.html
The first step is to use extension_builder to add the field. As I already have a extension in where I want to implement the extension I do not want to use the extension_builder (also I tried it with a new extension and extend the news-model did not work - I have no clue how to do it right). However this are the steps I did:
In my extension my_template I added the folders and file: Classes/Domain/Model/News.php:
class MyTemplate_Domain_Model_News extends Tx_News_Domain_Model_News {
/**
* #var bool
*/
protected $imageright;
/**
* Returns the imageright
*
* #return bool $imageright
*/
public function getImageright() {
return $this->imageright;
}
/**
* Sets the sort
*
* #param bool $imageright
* #return void
*/
public function setImageright($imageright)
{
$this->imageright = $imageright;
}
}
?>
/Ressources/Private/extend-news.txt:
Domain/Model/News
Created the field imageright as tinyint in the table tx_news_domain_model_news (and added it to the SQL file)
I knew I have to create a TCA file in /Configuration/TCA/, but I have no clue how this should look like or what name it needs to have. I think this is the last step I need to make this working.
Also note the extension my_template was just a template, so before my changes there where no Classes and no TCA files.
Solution is to use this tutorial: http://www.lukasjakob.com/extend-a-typo3-extbase-model-with-custom-field/

Add data to TYPO3 Repository Extbase

I want to develop an extension for TYPO3 6.2.
In the controller class I created a action called "fetchFeUsersAction".
This action loads a set of data from the table fe_users in the table which was created from the extensionbuilder from the model.
The function to the get the users in the Repository looks like this:
public function getFeUsers()
{
/** #var Query $q */
$q = $this->createQuery();
$q->statement('SELECT * from fe_users');
$data = $q->execute(true);
return $data;
}
This works very fine.
But now I want to store the results from the table fe_users in my model with this action:
public function fetchFeUsersAction()
{
$data = $this->adressRepo->getFeUsers();
foreach ($data as $feuser) {
/** #var Adresse $address */
$address = $this->objectManager->get(Adresse::class);
$q= $this->adressRepo->createQuery();
$q->matching(contains('email', $feuser['email']));
$contain= $q->execute();
if($contain==NULL){
$address->setEmail($feuser['email']);
$this->adressRepo->add($address);
}
}
$this->redirect('list');
}
Here I want to check if the email adress is already stored in my table.
If the email adress is not stored it should be added to the model.
But it doesnt work. Even with an empty table. Without the condition it works very fine.
Extbase persists the complete data into the DB later. You need to persist manually i think, like this:
/** #var \TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager */
$persistenceManager = GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager::class);
$persistenceManager->persistAll();
Not currently tested, just copied it from my of my extensions. but in there i had the same problem to check if it exists.
Hope this helps a little bit.

Edit hidden records in frontend

I am building an extension to edit tt_news records in frontend.
I set setIgnoreEnableFields(TRUE) in my repository.
But if I try edit a hidden record, I get the error
Object with identity „12345" not found.
Any solution for this?
I am guessing you are using an action like
/**
* Single view of a news record
*
* #param \Vendor\Ext\Domain\Model\News $news news item
*/
public function detailAction(\Vendor\Ext\Domain\Model\News $news = null)
Your problem is, that the Repository is not used to fetch the record.
As a solution, remove the argument, clear the caches and try something like that
/**
* Single view of a news record
*
* #param \Vendor\Ext\Domain\Model\News $news news item
*/
public function detailAction() {
$id = (int)$this->request->getArgument('news');
if ($id) {
$news = $this->newsRepository->findByUid($previewNewsId);
}
}
Now you can manipulate the QuerySettings and use those.
The problem is the PropertyMapping. If extbase try to assign an uid (12345) to an Domain Object (tt_news) the "setEnableFields" setting of the Repository isn't respected. So you must fetch the object by yourself.
the simple solution is to do this in an initialize*Action for each "show" action. For editAction an example:
public function initializeEditAction() {
if ($this->request->hasArgument('news')) {
$newsUid = $this->request->getArgument('news');
if (!$this->newsRepository->findByUid($newsUid)) {
$defaultQuerySettings = $this->newsRepository->createQuery()->getQuerySettings();
$defaultQuerySettings->setIgnoreEnableFields(TRUE);
$this->newsRepository->setDefaultQuerySettings($defaultQuerySettings);
if ($news = $this->newsRepository->findByUid($newsUid)) {
$this->request->setArgument('news', $news);
}
}
}
}
The Hard Part is to get the object to update. As I never try this I have found an TypeConverter to fetch also hidden Records at https://gist.github.com/helhum/58a406fbb846b56a8b50
Maybe Instead to register the TypeConverter for everything (like the example in ext_localconf.php) you can try to assign it only in the initializeUpdateAction
public function initializeUpdateAction() {
if ($this->arguments->hasArgument('news')) {
$this->arguments->getArgument('news')->getPropertyMappingConfiguration()
->setTypeConverter('MyVendor\\MyExtension\\Property\\TypeConverters\\MyPersistenObjectConverter')
}
}