Typo3: Constant as Page UID - typo3

I have a list of constants assigned to various page IDs (e.g. myConstant = 22). Now I'd love to replace the following link
<f:link.page pageUid="22" >Link</f:link.page>
with something like
<f:link.page pageUid="{myConstant}" >Link</f:link.page>
I haven't been able to find the right syntax to do so. Any help?

i think you can't access the constants directly but you can use the constants in the ts-setup.
with plugin.tx_myplugin.settings.myPid = {$myConstant} in the ts-setup you can access the pid in your plugin with {settings.myPid}
if you're not using a plugin but a TS FluidTemplate you can assign it them like this:
page = PAGE
page {
10 = FLUIDTEMPLATE
10 {
file = fileadmin/templates/Home.html
variables {
pidList {
myConstant = {$myConstant}
myConstant2 = {$myConstant2}
}
}
}
}
<f:link.page pageUid="{pidList.myConstant}" >Link</f:link.page>

If you are using a FLUIDTEMPLATE typoscript-Object, you can do it as follows in TypoScript:
lib.output = FLUIDTEMPLATE
lib.output {
# ...
variables {
myPageID = {$myConstant}
}
# ...
}
In the fluid-template you can use the variables like you want:
<f:link.page pageUid="{myPageID}" >Link</f:link.page>
In case the template is rendered by an extension in a controller action, you can assign the value to a setting of your plugin: plugin.tx_<extkey>[_pi1].settings.myPageID = {$myConstant}. Then you can use it in the fluid template like this:
<f:link.page pageUid="{settings.myPageID}">Link</f:link.page>
In any case, you can assign that value to some TypoScript Object and read that in your template by either using the f:cObject ViewHelper or the v:var.typoscript ViewHelper from the extension vhs.

I tried something like that in t3 7.6 If you want to use a ts constant (defined in ts-constants field as oneConst) somewhere in your page fluid template you must do somthing like this:
page.10 = FLUIDTEMPLATE
page.10 {
variables{
const_one=TEXT
const_one.value={$oneConst}
}
}
}
Without the TEXT definition you will not get the value. Access it in your template:
{const_one}
Hint: i was not able to organize const in an array. Like
const{
one=TEXT
one.value={..}
}

Related

How can I display the version of my sitepackage in the frontend?

I want to display the version of my sitepackage (from my declaration file ext_emconf.php ) in the frontend.
How do I query this information? I was thinking of using a DataProcessor in my FLUIDTEMPLATE, but I’m not sure whether I need to write my own or if there’s already one I can use for that.
Thank you!
Depending on your exact needs you could make use of ExtensionManagementUtility::getExtensionVersion() to inject a global TypoScript constant via ExtensionManagementUtility::addTypoScriptConstants():
// ext_localconf.php
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addTypoScriptConstants(sprintf(
'site.version = %s',
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::getExtensionVersion('sitepackage')
));
Afterwards you can use this constant anywhere in TypoScript setup including variables of Fluid templates:
// TypoScript setup
10 = FLUIDTEMPLATE
10 {
// ...
variables {
siteVersion = {$site.version}
}
}
Now use this variable anywhere you like in your template:
<!-- Fluid template -->
<p>Site {siteVersion}</p>
There is no DataProcessor for that.
I'd suggest to create a small PHP class to read out the version and integrate it via TypoScript as a USER object.
namespace Vendor\Sitepackage\Service;
class SitepackageVersionService
{
public function getVersion(): string
{
$version = '';
$packageManager = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Package\PackageManager::class);
$extensionKey = 'my_extension_key';
if ($packageManager->isPackageActive($extensionKey)) {
$package = $packageManager->getPackage($extensionKey);
$version = $package->getPackageMetaData()->getVersion();
}
return $version;
}
}
page.10.variables.sitePackageVersion = USER
page.10.variables.sitePackageVersion.userFunc = Vendor\Sitepackage\Service\SitepackageVersionService->getVersion
<strong>Version: {sitePackageVersion}</strong>

Typo3: How to get the View exception error

I am Developing a custom extension for Typo3. now I am getting an error if the user did not include my extension from template's include section.
I want to catch this error to show a message from controller. How can I do this?
my controller action.
public function listAction()
{
$audits = $this->auditRepository->findAll();
$this->view->assign('arrDetails', $audits);
}
This could be one solution, but not the cleanest.
We first need to get the values from the field include_static_file that it is located on the sys_template table. So:
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('sys_template')->createQueryBuilder();
$result = $queryBuilder
->select('include_static_file')
->from('sys_template')
->execute()
->fetch(0);
The we need to get the string and evaluate if your extension key is present. So:
$extKey = 'your_extension_key';
if (strpos($result['include_static_file'], $extKey) !== false) {
$audits = $this->auditRepository->findAll();
$this->view->assign('arrDetails', $audits);
}
else {
$this->addFlashMessage(
'You forgot to add the static template',
$messageTitle = 'Template is missing',
$severity = \TYPO3\CMS\Core\Messaging\AbstractMessage::WARNING,
$storeInSession = TRUE
);
}
Your HTML
<f:if condition="{arrDetails}">
<f:then>
do something with your content
</:then>
<f:else>
<f:flashMessages />
</f:else>
</f:if>
Of course you can write a static function for this or you can use the LocalizationUtility in order to get the text in multiple languages. It is up to you.
Result:
Are you sure you need your static template?
I think it is important that some values you fetch from typoscript got meaningful content.
As you inspect them anyway you can output a message if one or more values are empty.
Your plugin would work in an installation if all necessary values are set in any way. Even if your static template is not included.
And your plugin would fail if your static template is included but following typoscript would erase the settings from it.
In your error message you can note the possibility to use the values from your static template.

How can I replace custom tags with strings from an xlf file in TYPO3

I'm trying to use the TYPO3 tags function to get strings from an xlf file. If I hard code the reference to the string I want, it returns successfully, but I need to know how to get it to return the appropriate string based on the tag content.
For example, this works:
HTML:
<var>myVar</var>
Typoscript:
lib.parseFunc_myExt{
tags {
var = TEXT
var{
value = {LLL:path/to/locallang.xlf:var.myVar}
insertData = 1
}
}
}
I need a method to take the content of the <var> tag and use it in place of myVar in the value.

TYPO3 inline fluid condition and typoscriptObjectPath

I search for a solution for inline fluid condition and typoscriptObjectPath.
work fine:
<f:cObject typoscriptObjectPath="lib.currentDate" />
work fine:
<f:if condition="{price.day} == {f:cObject(typoscriptObjectPath:'lib.currentDate')}">
<f:then>work</f:then>
<f:else>dont work</f:else>
</f:if>
work fine:
{f:if(condition:'{price.day} == \'Sunday\'',then:'active',else:'test')}
DONT work
{f:if(condition:'{price.day} == \'{f:cObject(typoscriptObjectPath:'lib.currentDate')}\'',then:'active',else:'test')}
how can i use the right inline code?
You do not need to resolve lib.currentDate cObject within your view, as you can just copy its output into fluid variable. It will avoid any problems with nested quotes, brackets etc. etc... Of course I assume, that's in combination with fluid template of the PAGE:
lib.currentDate = TEXT
lib.currentDate {
data = date:U
strftime = %A
}
page = PAGE
page {
# ....
10 = FLUIDTEMPLATE
10 {
# ....
variables {
mainContent < styles.content.get
currentDate < lib.currentDate
}
}
}
so you can use it in condition just like:
<f:if condition="{price.day} == {currentDate}">That's today!</f:if>
<!-- or... -->
{f:if(condition:'{price.day} == {currentDate}', then: 'active', else: 'not-active')}
Of course if you're working in the plugin's context, you can do the same with assign method within your action, like:
$this->view->assign('currentDate', strftime('%A',date('U')));
Note you have also other options:
Create custom if ViewHelper, which will be useful when price.day and currentDate are different types and requires type conversion before comparison.
Create transient field in your price model, which' getter compares day field with strftime('%A',date('U')) and return boolean value, so you can use it directly as:
<f:if condition="{price.myTransientField}">Hooray!</f:if>

TYPO3: How can I access property of objects in a partial or section?

I have an object defined in TypoScript
page.10 {
variables {
myObject = COA
myObject{
1 = TEXT
1.value = yome Text
2 = TEXT
2.value = 42
}
}
}
and I need the data of the myObject in a partial
<f:render partial="myPartial" arguments="{content:myObject}" />
that looks like
<section id="myPartial">
<h2>{content.1}</h2>
<p>{content.2}</p>
</section>
Although the content is there ( because {content} will display all the properties) I cannot access it and h2 and p will be empty...
What should I do to fill h2 and p with the content of myObject?
That is not possible. TypoScript only returns text strings at the moment, not arrays. Thus the variable myObject contains the whole concatenated string of the COA, thus yome Text42.
Note that COA means Content Object Array, but the whole COA is one single object that is returned as one string.
Alternative: use the VHS extension's v:var.typoscript ViewHelper:
{namespace v=Tx_Vhs_ViewHelpers}
{v:var.typoscript(path: 'page.10.variables.myObject') -> v:var.set(name: 'myObject')}
After which you can access {myObject.1} etc. in your template. Note that the so-called "chained" usage of v:var.set is optional, but will make it easier to access your variables using an intermediate template variable instead of more expensive calls to retrieve the value completely in multiple locations. The other way:
{v:var.typoscript(path: 'page.10.variables.myObject.1')}
{v:var.typoscript(path: 'page.10.variables.myObject.2')}
etc.
VHS extension on TER: http://typo3.org/extensions/repository/view/vhs