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

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>

Related

Retrieve content element field from within a plugin template?

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.

Why FLUIDTEMPLATE variable is not passing to fluid Template html file?

I am trying to pass variables via FLUIDTEMPLATE. But my attempts are miserably failing every time. I have created a simpleblog extension via ext-builder. So I have my template files in simpleblog/Resources/Private/Templates/Blog/List.html. I am using this basic example to access data using FLUIDTEMPLATE. I put following code it in my extension's setup.typoscript.
page = PAGE
page {
10 = FLUIDTEMPLATE
10 {
file = EXT:simpleblog/Resources/Private/Templates/Blog/List.html
settings {
copyrightYear = 2013
}
}
}
and access this in template file as
...
<h3>{settings.copyrightYear}</h3>
I understand this is simple. But I don't know what's stopping me to access the data.
if you use a FLUIDTEMPLATEobject you can add variables similar to your example. but with the property variables:
10 = FLUIDTEMPLATE
10 {
file = EXT:simpleblog/Resources/Private/Templates/Blog/List.html
variables {
copyrightYear = 2013
}
}
But you are talking about an extension.
Extensions have their own templating without an explicit declaration in typoscript.
The template and the variables are prepared inside the PHP code of the extension (probably the controller).
some variables which often(!) are copied to the fluid are the settings of the plugin. These values could then be found in a fluid variable setting.
read the manual of the extension and you will find something like:
plugin {
myextension {
settings {
mySetting = myValue
}
}
}
but that can be different for each extension or plugin, so the path could differ with plugin name instead of extension name or with additional pathes for different actions.
plugin {
myplugin {
list {
settings {
myListSetting = myListValue
}
}
show {
settings {
myShowSetting = myShowValue
}
}
}
}
and don't forget that these settings might be overriden with values from the plugin content element inside the PHP code.
How do fill the blogs variable? If you already have created an extension, then logic and the settings should be set in plugin.tx_yourext.settings and not in a global scope. I think you’re mixing up some things here. See https://docs.typo3.org/m/typo3/book-extbasefluid/master/en-us/b-ExtbaseReference/Index.html#typoscript-configuration

Wordpress Plugins / Custom Class / get_option() / shortcode

I'm having an issue displaying information returned from a custom class defined within a plugin's files, when using a shortcode. I'll write up some mock files that showcase my issue.
/wp-content/plugins/my-plugin/classes/my_class.php
<?php
class People {
public $api_url = "https://www.external-service.com/api";
private $api_key;
function __construct($key = null) {
if $(key) {
$this->api_key = $key;
}
function get_response() {
$path = $this->api_url . "?my_api_token=" . $this->api_key;
}
}
?>
/wp-content/plugins/my-plugin/my-plugin.php
<?php
/**
* all of the wordpress plugin comments
* ...
*/
require "myplg_options.php";
require "myplg_shortcodes.php";
The options page and menu is generated from myplg_options; it is functioning correctly (including using get_option to retrieve the saved option (in this case, the api key).
/wp-content/plugins/my-plugin/myplg_shortcodes.php
<?php
require "classes/my_class.php";
$options = get_option('myplg_settings');
$myplg = new People($options['myplg_api_key']);
$response = $myplg->get_response();
function myplg_list_result(){
echo "the shortcode is working!";
var_dump($options, $myplg, $respnose);
}
add_shortcode('myplg_list', 'myplg_list_result');
?>
Testing externally from wordpress, the class works and everything is fine and dandy. The plugin's option page sets and retains the single option perfectly; the shortcode actually registers and is usable from within a WordPress page/portfolio/etc.
The issue I'm having is that using var_dump, all three of those variables are dumped as NULL.
After doing some homework, I was able to determine that moving the three variable declarations inside the shortcode makes it work. It would seem to me, however, that doing that is not the best workflow, as I'd need to re-grab the option, instantiate a new class, and call the class' function for every shortcode.
Is there a way around this?
As mentioned in the comment it's because variables are function scoped. You may be better off using a Closure.
<?php
require "classes/my_class.php";
$options = get_option('myplg_settings');
$myplg = new People($options['myplg_api_key']);
$response = $myplg->get_response();
add_shortcode('myplg_list', function() use ($options, $response, $myplg) {
echo "the shortcode is working!";
var_dump($options, $myplg, $respnose);
});

Typo3: Constant as Page UID

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={..}
}

Calling TYPO3 plugin's method in TypoScript?

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'].