How to access the ext_conf_template.txt (extension configuration) in typoscript? - typo3

There are a few settings in the ext_conf_template.txt in my extension.
I want to check the value of one of these settings, but in typoscript, not in PHP.
In PHP it works like this:
unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['myExt'])
How should I do this in typoscript?

Thanks to the answer of Marcus I was able to get a extension configuration setting into typoscript. First create the extension setting in ext_conf_template.txt:
# cat=Storage; type=string; label=storageFolderPid
storageFolderPid = 1
In ext_local_conf.php add the following lines:
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addTypoScriptConstants(
"plugin.tx_extensionname.settings.storageFolderPid = ".$GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS']['extension']['storageFolderPid']
);
Then you can use this variabele in your typosript, for instance to create a submenu of a storage folder:
lib.submenu = CONTENT
lib.submenu {
table = tx_extension_domain_model_article
select {
pidInList = {$plugin.tx_extensionname.settings.storageFolderPid}
selectFields = tx_extensionname_domain_model_article.*
}
...
}

I did something similar in my code snippet extension (see complete code on Github), where I just added a custom TypoScript condition:
[DanielGoerz\FsCodeSnippet\Configuration\TypoScript\ConditionMatching\AllLanguagesCondition]
// some conditional TS
[global]
The condition implementation is quite simple:
namespace DanielGoerz\FsCodeSnippet\Configuration\TypoScript\ConditionMatching;
use DanielGoerz\FsCodeSnippet\Utility\FsCodeSnippetConfigurationUtility;
use TYPO3\CMS\Core\Configuration\TypoScript\ConditionMatching\AbstractCondition;
class AllLanguagesCondition extends AbstractCondition
{
/**
* Check whether allLanguages is enabled
* #param array $conditionParameters
* #return bool
*/
public function matchCondition(array $conditionParameters)
{
return FsCodeSnippetConfigurationUtility::isAllLanguagesEnabled();
}
}
And the check for the actual TYPO3_CONF_VARS value is done in FsCodeSnippetConfigurationUtility:
namespace DanielGoerz\FsCodeSnippet\Utility;
class FsCodeSnippetConfigurationUtility
{
/**
* #return array
*/
private static function getExtensionConfiguration()
{
return unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['fs_code_snippet']);
}
/**
* #return bool
*/
public static function isAllLanguagesEnabled()
{
$conf = self::getExtensionConfiguration();
return !empty($conf['enableAllLanguages']);
}
}
Maybe that fits your needs.

Handle the configuration via Extension Manager and call ExtensionManagementUtility::addTypoScriptConstants() in your ext_localconf.php to set a TypoScript constant at runtime.
This way the value can be set at one location and is available both in lowlevel PHP and TypoScript setup.

Related

TYPO3 Extension: How to make a relation to another extension’s model optional?

I have an events extension (for TYPO3 9 LTS and 10 LTS), say MyVendor\MyEvents and a Locations extension, say MyVendor\MyLocations.
The Model MyVendor\MyEvents\Domain\Model\Events has a property eventLocation which is defined to be an object of MyVendor\MyLocations\Domain\Model\Locations.
Now I want to make the relation to MyVendor\MyLocations\Domain\Model\Locations optional. I have found a way for the TCA to show a different form field in the backend depending on the MyLocations extension being installed. But I have no idea how to make all the type definitions in the Events model conditional. They are crucial for the extension to work:
namespace MyVendor\MyEvents\Domain\Model
class Events extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity
{
/**
* #var \MyVendor\MyLocations\Domain\Model\Locations
*/
protected $eventLocation = NULL;
/**
* #return \MyVendor\MyLocations\Domain\Model\Locations $eventLocation
*/
public function getEventLocation()
{
return $this->eventLocation;
}
/**
* #param \MyVendor\MyLocations\Domain\Model\Locations $eventLocation
* #return void
*/
public function setEventLocation(\MyVendor\MyLocations\Domain\Model\Locations $eventLocation)
{
$this->eventLocation = $eventLocation;
}
}
In case MyVendor\MyLocations is loaded it needs to be defined as above, in case it isn’t loaded it should be just an integer.
In the TCA I am using if (TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('my_locations')) for showing a different field in the backend form for an event.
The Locations Model is in a separate extension because I am using it in a third extension as well.
In your events extension you could setup a repository for locations. Then you can map this repository to your location extensions model via TypoScript.

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/

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')
}
}

Using Image Content Objects from tt_content in Extbase

I want to write an Extbase Backend module which needs a list of all Objects generated from tt_content with CType = 'image'.
Now I started defining a simple model
class Tx_Myextension_Domain_Model_Content extends Tx_Extbase_DomainObject_AbstractEntity
{
/**
* #var string
*/
protected $header;
/**
* #return the $header
*/
public function getHeader()
{
return $this->header;
}
/**
* #param string $header
*/
public function setHeader($header)
{
$this->header = $header;
}
}
and a Repository
class Tx_Myextension_Domain_Repository_ContentRepository extends Tx_Extbase_Persistence_Repository
{
public function initializeObject()
{
$querySettings = $this->objectManager->create('Tx_Extbase_Persistence_Typo3QuerySettings');
$querySettings->setRespectStoragePage(FALSE);
$this->setDefaultQuerySettings($querySettings);
}
}
As far as I know the initializeObject method is a way to get all content elements, no matter which pid they have.
At last I tried to map my Content Class on tt_content:
plugin.tx_myextension {
persistence {
classes {
Tx_Myextension_Domain_Model_Content {
mapping {
tableName = tt_content
recordType = Tx_Myextension_Domain_Model_Content
columns {
header.mapOnProperty = header
}
}
}
}
}
}
module.tx_myextension {
persistence < plugin.tx_myextension.persistence
}
No I want to use the Repo. e.g. countAll. Unfortunately it always returns 0. Looking for the MySQL query discovers the problem:
SELECT COUNT(*)
FROM tt_content
WHERE (tt_content.CType='Tx_Myextension_Domain_Model_Content')
AND tt_content.deleted=0 AND tt_content.hidden=0
AND (tt_content.starttime<=1313073660)
AND (tt_content.endtime=0 OR tt_content.endtime>1313073660)
AND tt_content.sys_language_uid IN (0,-1)
AND tt_content.pid IN (0)
Typo 3 or Extbase or something different added all these where clauses to the query. I just want to get rid of the CType and pid clauses. As I said, I thought that the method used in the Repo leads to ignoring the pid, which is obviously not the case.
Can somebody help me? All I want is an array of Image Content Elements. Thank you in advance.
Late answer: You'll most likely want to call
query->getQuerySettings()
->setRespectEnableFields(FALSE)
->setRespectSysLanguage(FALSE);
for your query. You can disable it for all queries in your repository's initializeObject method:
$querySettings = $this->objectManager->create('Tx_Extbase_Persistence_Typo3QuerySettings');
$querySettings
->setRespectStoragePage(FALSE)
->setRespectEnableFields(FALSE)
->setRespectSysLanguage(FALSE);
$this->setDefaultQuerySettings($querySettings);
See: TYPO 3 API docs
Try to remove the Node "recordType" from your Persistence Definition.