TYPO3 change template inside action method - typo3

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.

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.

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

Is there a Fluid viewhelper to truncate an URL? If not, how do I make one?

In TYPO3's Fluid or in Fedext/vhs, is there a viewhelper that can convert
http://www.stackoverflow.com/questions/ask
into
www.stackoverflow.com
?
PS: that's the goal:
<f:format.raw><f:link.external uri="{item.link}">{item.just-display-the-domain}</f:link.external></f:format.raw>
EDIT (adapting the question to the answer I got): If I have to build a custom view helper, how do I proceed?
I really doubt if there would be any sensible reason for adding this kind of VH into the core, de facto, writing custom VH is like a piece of cake (when you finally realize it is) so simple formatters can be created by devs in their custom tool exts just in minutes.
Ie. in TYPO3 4.x assuming that you have a custom extension with key urs all you need to do is create one proper class, containing render($params) method and extending Tx_Fluid_Core_ViewHelper_AbstractViewHelper class:
/typo3conf/ext/urs/Classes/ViewHelpers/GetDomainViewHelper.php:
<?php
class Tx_Urs_ViewHelpers_GetDomainViewHelper extends Tx_Fluid_Core_ViewHelper_AbstractViewHelper {
/**
* #param $link string Each `allowed` param need to have its line in PHPDoc
* #return string
*/
public function render($link) {
$link = str_replace('http://', '', $link);
$link = str_replace('https://', '', $link);
$segments = explode('/', $link);
return trim($segments[0]);
}
}
?>
Next in your templae declare its namespace and... that's all, you can use it:
{namespace urs=Tx_Urs_ViewHelpers}
<urs:getDomain link="http://stackoverflow.com/questions/20499453" />
Take special attention about letter case in things like Tx_Urs_ViewHelpers... etc.
More details in http://docs.typo3.org/typo3cms/ExtbaseFluidBook/8-Fluid/8-developing-a-custom-viewhelper.html
In TYPO3 ver. 6.x
Things works preaty similar the main change of course is new namespacing
/typo3conf/ext/urs/Classes/ViewHelpers/GetDomainViewHelper.php:
<?php
namespace TYPO3\Urs\ViewHelpers;
class GetDomainViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper {
/**
* #param $link string Each `allowed` param need to have its line in PHPDoc
* #return string
*/
public function render($link) {
$link = str_replace('http://', '', $link);
$link = str_replace('https://', '', $link);
$segments = explode('/', $link);
return trim($segments[0]);
}
}
In templates:
{namespace urs=TYPO3\Urs\ViewHelpers}
<urs:getDomain link="http://stackoverflow.com/questions/20499453" />
Of course in both cases instead of using hardcoded links you will use:
<urs:getDomain link="{item.link}" />
It's a little bit cumbersome and not very efficient, but it should work and would of course prevent the need for a custom ViewHelper class:
With protocol:
{url -> v:iterator.explode(glue: '/') -> v:iterator.slice(length: 3) -> v:iterator.implode(glue: '/')}
Without protocol:
{url -> v:iterator.explode(glue: '/') -> v:iterator.slice(start: 2, length: 1) -> v:iterator.first()}
Where {url} can come from anywhere, as long as it contains a full http:// or other protocol prefix. The line above of course explodes the URL into parts separated by / then slices off the first three segments and re-joins those using /. Or it simply picks offset 2 (which would be the full domain without protocol) and returns that single element. The result should be an url to the domain of the link, with or without protocol, without trailing slash (which you may or may not want to add yourself after cutting the URL).
Cheers,
Claus
All ViewHelper are located at typo3/sysext/fluid/Classess/ViewHelper. There are also examples in the header of each file. All ViewHelper of Fedext can be reviewed on the website.

Does exist Zend Filter similar to Zend Validator Identical?

Does exist Zend Filter similar to Zend Validator Identical?
The case I should filter input=='test'
$el->addFilter('Identical','test');
The problem that such filter not exist.
Thanks,
Yosef
I'm not sure how this filter should work, since it is not clear from your question. Anyway, I made some custom filter that will check if a value of input filed is equal to some $token. If they are equal than the input value will be empty string.
The filter looks as follows:
// file: APPLICATION_PATH/filters/Identical.php
class My_Filter_Identical implements Zend_Filter_Interface {
/**
* Token with witch input is compared
*
* #var string
*/
protected $_token;
/**
* Set token
*
* #param string
* #return void
*/
public function __construct($token = '') {
$this->_token = $token;
}
/**
* Filtering method
*
* #param string $value value of input filed
* #return string
*/
public function filter($value) {
if ($value !== $this->_token) {
return $value;
}
return '';
}
}
To apply it to a given form element:
require_once (APPLICATION_PATH . '/filters/Identical.php');
$el1->addFilter(new My_Filter_Identical('test'));
Off course instead of require_once it could be added to your resource autoloader, but as an example I think it is not needed right now.
Edit:
Forgot to mention pregReplace filter.
The same what the custom filter above does could be done using pregReplace filter:
$el1->addFilter('pregReplace',array('/test/',''));
But, as I said, I'm not sure how you want your filter to work. If you provide more info maybe I could help more.
Your question isn't all that clear - do you want a filter which removes the word test? Or are you talking about filtering a form input? So taking your example you want to remove from the el input what the test input contains?
If you want to remove test from your input, you could use Zend_Filter_PregReplace
$filter = new Zend_Filter_PregReplace(array('match' => '/test/', 'replace' => ''));
$input = 'What is this test about!';
$filter->filter($input);
Should give you What is this about!
There isn't a filter which would filter identical form input if its been entered into another input I don't think. You could try to create your own input filter and perform your own logic on the input.
It is not that clear what you are trying to do. If you give more explanation that would be good.
I need to remove all input, soo its not good to use regex.
If you just want to clear the data in form elements you can use one of the following:
Clear an element value by setting the value of the element to nothing.
$el->setValue(null);
or reset all form elements
$form->reset();

Render action to HTML email in Zend Framework

I have an action which is rendering some content via a layout.
I actually want to send this output in an email. What is the best way to achieve this in the Zend Framework?
I know I need to use the Zend_Mail component to send the email, but I'm unclear about how to attach the output of my action to Zend_Mail.
I've done some reading on the ContextSwitch action helper, and I think that might be appropriate, but I'm still not convinced.
I'm still new to Zend Framework. I'm used to using techniques like output buffering to capture output, which I don't think is the correct way to do this in Zend.
From your controller:
// do this if you're not using the default layout
$this->_helper->layout()->disableLayout();
$this->view->data = $items;
$htmlString = $this->view->render('foo/bar.phtml');
If you're doing this from a class that's not an instance of Zend_Controller_Action, you may have to create an instance of a Zend_view first, to do this:
$view = new Zend_view();
// you have to explicitly define the path to the template you're using
$view->setScriptPath(array($pathToTemplate));
$view->data = $data;
$htmlString = $view->render('foo/bar.phtml');
public static function sendMail($data = array(), $template = ''){
$html = new Zend_View();
$html->setScriptPath(APPLICATION_PATH . '/modules/default/views');
// assign valeues
if(count($data['Assigni'])){
foreach($data['Assigni'] as $assign){
$html->assign($assign['key'], $assign['value']);
}
}
// create mail object
$mail = new Zend_Mail('utf-8');
// render view //'scripts/newsletter/emailtemplate.phtml'
$bodyText = $html->render($template);
$mail->addTo($data['To']);
$mail->setSubject($data['Subject']);
$mail->setFrom($data['From'], $data['FromName']);
$mail->setBodyHtml($bodyText);
$mail->send();
}
when you dispatch the action, you can catch the event in postDispatch() method of plugin, that you can dynamically add to the stack from desired action. In that you recieve the contents of response by
//in action
//...some php code
Zend_Controller_Front::getInstance()->registerPlugin(new My_Plugin());
//in plugin
$htmlCode = $this->_response->getBody();
I can't give you a super-detailed answer, but if you want the full output (including the layout), I think you want to write your email function as an Action helper, and insert it at the PostDispatch hook of the Zend_Controller_Action->dispatch() loop.
See http://nethands.de/download/zenddispatch_en.pdf for the full Zend Framework Dispatch Process Overview.
If you don't need the layout included in your email content, then you could do this at many points, including by the use of a context switch, as you mentioned.