is it possible to call a method of a plugin from within TypoScript? I need to pass the output of a method to a TypoScript TEXT object.
This is how I imagine it (it is not a correct TypoScript code though):
lib.field_some_field.value.wrap < plugin.some_plugin.some_method
Is it possible?
Thanks!
untested:
# If you are using an USER Object
includeLibs.some_plugin = EXT:some_plugin/pi1/class.tx_some_plugin_pi1.php
lib.field_some_field_with_stdWrap.append < USER
lib.field_some_field_with_stdWrap.append {
# you need includeLibs only, if you use an USER_INT Object
includeLibs = EXT:some_plugin/pi1/class.tx_some_plugin_pi1.php
userFunc = tx_some_plugin_pi1->some_method
}
But you need to set in localconf.php / via Install-Tool:
$TYPO3_CONF_VARS['FE']['userFuncClassPrefix'] = false
And some_method will be called tx_some_plugin_pi1->some_method($content, $conf), you cannot change the parameters!
or
If it is your extension, you could simply check for an conf-variable.
lib.field_some_field_with_stdWrap.append < plugin.some_plugin
lib.field_some_field_with_stdWrap.append.useMethod = some_method
Now check in your main() method for $conf['useMethod'].
Related
I am modifying the template of a plugin, and I want to retrieve a field from the content element.
Using f:debug I see the only data available is from the plugin itself, and none from the content element.
Is there any way I can perhaps insert the field I need in the plugin settings?
eg. something like:
plugin.tx_plugin.settings {
contentUid = TEXT
contentUid.field = uid
}
The best way I can think of to do this is with a custom ViewHelper. Something like:
namespace MyVendor\MyExtension\ViewHelpers;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManager;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
class ContentUidViewHelper extends AbstractViewHelper
{
public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
{
$configurationManager = GeneralUtility::makeInstance(ConfigurationManager::class);
return $configurationManager->getContentObject()->data['uid'];
}
}
In your Fluid template:
<mynamespace:contentUid />
This will get the uid of the content element, but you can get any field this way. Just change the key of the data array to the field you need.
In the corresponding method (like the listAction or showAction) of the controller you can get the data of the content element in the following way:
$contentObject = $this->configurationManager->getContentObject();
$this->view->assign('contentObjectData', $contentObject->data);
As far as I know, you can't get to that data using typoscript, but I've never needed it anyway since I've been using the above code in the controller.
settings do not have stdWrap-type per default, but only string. So you can not use cObjects as values.
For one (or a few) settings, you could do the stdWrap-processing in your method/class yourself:
$settingsAsTypoScriptArray = $this->objectManager->get(TypoScriptService::class)->convertPlainArrayToTypoScriptArray($this->settings);
$contentObj = $this->configurationManager->getContentObject();
if ($contentObj === null) {
$contentObj = GeneralUtility::makeInstance(ContentObjectRenderer::class);
}
// now override the values in the settings array with the processed value
$contentUid = (int)$contentObj->stdWrap($settingsAsTypoScriptArray['contentUid'], $settingsAsTypoScriptArray['contentUid.']);
If you wanna have many settings to be stdWraped, have a look into EXT:news. Georg implemented an interesting concept via useStdWrap configuration.
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>
In my ext I have singleAction method. I want to change template inside this method, because I have 2 templates for single action. Is it possible? If it impossible, how can I solve this problem? Maybe generate another action?
It's not that simple to set a template. the setTemplate and getTemplate don't exist in the View.
You could revert to a standaloneview implemtation, which supports the use of setTemplatePathAndFilename
(example copied from Ludwig)
/**
* Renders the fluid email template
* #param string $template
* #param array $assign
* #return string
*/
public function renderFluidTemplate($template, Array $assign = array()) {
$templatePath = \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName('EXT:myextension/Resources/Private/Templates/' . $template);
$view = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Fluid\\View\\StandaloneView');
$view->setTemplatePathAndFilename($templatePath);
$view->assignMultiple($assign);
return $view->render();
}
echo renderFluidTemplate('mail.html', array('test' => 'This is a test!'));
You could also switch to a different template with typoscript.
plugin.tx_yourpluginname.view.templateRootPaths = EXT:extension_name/Resources/Private/CustomPath/Templates/
And this can be put into any typoscript condition you want.
The best way to handle this is simply to have a switch in the template of the action, loading a different partial. This way the whole flow of logic stays intact and it's immediately understandable for everyone who will have to edit your code later on.
Im trying to make an extension with Kickstarter that overrides the normal rendering of the page, and renders a PDF file. For this im using FPDF. But im not sure how to do it. I tried doing this, but it didnt work:
<?php
// require_once(PATH_tslib . 'class.tslib_pibase.php');
class tx_ishurkunde_pi1 extends tslib_pibase {
public $prefixId = 'tx_ishurkunde_pi1';
public $scriptRelPath = 'pi1/class.tx_ishurkunde_pi1.php';
public $extKey = 'ish_urkunde';
public $pi_checkCHash = TRUE;
public function main($content, array $conf) {
if (!t3lib_extMgm::isLoaded('fpdf')) return "Error!";
$pdf = new FPDF();
$pdf->AddPage();
$content = $pdf->Output('', 'S');
return $content;
}
}
?>
It still keeps rendering the normal web template. What am I missing?
FYI, Im not trying to render the HTML as PDF. Im trying to generate a PDF from scratch, using the URL parameters are text variables.
As far as I understood, your aim is to render a PDF instead of page elements.
Your current approach will not work since you are inserting a plugin onto the page. The plugin's return value is then given back to the TYPO3 content parser, and if the page has finished parsing, it is displayed. There is no part in it where you can throw over the whole page rendering; At least it is not intended to do, and you shouldn't to (albeit there are extensions that do this).
The eID approach would be to either create an eID script (have a look at dd_googlesitemap) which is called via GET param and renders only what you need. There you basically can do everything you want to.
In your extension's ext_localconf.php you register the eID script, like this:
$GLOBALS['TYPO3_CONF_VARS']['FE']['eID_include'][$_EXTKEY] = "EXT:{$_EXTKEY}/path/to/my/EIDHandler.php";
An Example eID handler structure:
class Tx_MyExt_EIDHandler
{
public function main()
{
// Your code here
}
}
$output = t3lib_div::makeInstance('Tx_MyExt_EIDHandler');
$output->main();
To call your eID script in the frontend, you append the appropriate GET params, like http://example.com/index.php?eID=tx_myext. This is the array key you defined in your ext_localconf.php (in my example, it is $_EXTKEY, but it can basically be any string).
The plugin/typoscript approach would be like e.g. TemplaVoila does it: You create a PAGE type and call a user_func which does your things. This would be the fastest approach because you already have a plugin. Important is that you render your own page type with only your plugin in it.
Example TypoScript:
specialPage = PAGE
specialPage {
type = 2
10 = USER
10.userFunc = tx_myextension_pi1->myFunc
}
After that, you can call your new page with http://example.com/index.php?type=2. However, headers etc are still rendered and you may need to remove them.
In my typoscript 20.special.value and 10.value.typolink.parameter have the same value, which is a page id. I use this value to build a heading and a menu of its subpages within the same COA object. I would like to be able to re-use this value.
How can I call a object property in typoscript?
Here's what I have tried:
10 = HTML
10 {
value.typolink {
parameter = {$temp.LANDINGPAGEMENU.20.special.value}
}
}
UPDATE:
I am re-utilizing my COA object in different parts of the site, and changing only the special.value, so to display a menu I have:
temp.LANDINGPAGEMENU.10.value.typolink.parameter = 2427
temp.LANDINGPAGEMENU.20.special.value = 2427
temp.COLUMN_NOTSURE < temp.LANDINGPAGEMENU
I am after a cleaner way of handling the
temp.LANDINGPAGEMENU.10.value.typolink.parameter = 2427
temp.LANDINGPAGEMENU.20.special.value = 2427
Full LANDINGPAGEMENU typoscript code is http://pastebin.com/p9kPuZEe
Use the constants but not in a way you tried. You have to define the constant first.
Constants: my_constant = 2427
Setup: parameter = {$my_constant}
...OR...
Assign one of the values by reference using the =< operator. However, this would work only when using the whole object. Example:
temp.something = TEXT
temp.something.value = 2427
...parameter.cObject =< temp.something
I suggest you go with the option 1 using the constants as the 2nd option is somewhat cumbersome.
You can copy the property but not by reference it.