I've sanitized my form's input (textarea field) and when I display it on my view it comes out like this:
<p>I\'m in it to win it!! I\'m looking forward to playing the contest in <br />Contest Central. He aims to cross-pollinate the stage, screen and stereo <br />with work that speaks to both the humor and frustrations of modern life.</p>
In my controller I have this:
public function init(){
$this->view->setEscape('html_entity_decode');
$this->view->setEscape('stripslashes');
}
But only one works, if I erase one the setEscape then the other works and vice versa. So I can get stripslashes to work if I put it first but html_entity_decode wont work and vice versa
You need to define your own function that should be used for escaping. For example, you can defined a class My_Tools in library/My/Tools.php as follows:
<?php
#Tools.php
class My_Tools {
/**
* My custom escape function
*
* #param string $str String to be escaped
* #return string Escaped string
*/
static function myEscape($str) {
$str = html_entity_decode($str);
return stripslashes($str);
}
}
?>
Then, your init() could have the following form:
public function init() {
require_once(APPLICATION_PATH . '/../library/My/Tools.php');
$this->view->setEscape(array('My_Tools', 'myEscape'));
}
Off course it would be better to add Tools to Autoloader, but for this is just an example.
Related
What is the most straightforward way to get the current category in the view? I notice that there is a getTerm method in the Term class:
public function getEntity()
{
return $this->getTerm();
}
/**
* Returns the current Wordpress category
* This is just a wrapper for getCurrentCategory()
*
* #return \FishPig\WordPress\Model\Term
*/
public function getTerm()
{
if (!$this->hasTerm()) {
$this->setTerm($this->_registry->registry(Term::ENTITY));
}
return $this->_getData('term');
}
However if I try to utilize the method within a template (for example, the default post list wrapper.phtml template which utilizes the Term block in the layout) it throws an error:
<?php echo $this->getTerm() ?>
Recoverable Error: Object of class FishPig\WordPress\Model\Term could
not be converted to string in
I'm probably just missing something simple, any help would be greatly appreciated. Thanks!
$term = \Magento\Framework\App\ObjectManager::getInstance()
->get('Magento\Framework\Registry')
->registry('wordpress_term');
With Symfony 2.7, you could customize a form's name in your EntityType class with the method getName()
This is now deprecated. Is there another way to do that with Symfony 3.0 ?
I have custom prototype entry_rows for collections that I would need to use in different forms.
Since the name of the rows is based on the form's name, I would need to change the later in order to use them with a different form.
You should implements the getBlockPrefix method instead of getName as described in the migration guide here.
As example:
/**
* Returns the prefix of the template block name for this type.
*
* The block prefix defaults to the underscored short class name with
* the "Type" suffix removed (e.g. "UserProfileType" => "user_profile").
*
* #return string The prefix of the template block name
*/
public function getBlockPrefix()
{
return "form_name";
}
Hope this help
Depending on how your form is built, there is different ways to set the name of your form.
If you are creating the form through $this->createForm(CustomType::class):
$formFactory = $this->get('form.factory');
$form = $formFactory->createNamed('custom_form_name', CustomType::class);
If you are building the form from the controller directly through $this->createFormBuilder():
$formFactory = $this->get('form.factory');
$form = $formFactory->createNamedBuilder('custom_form_name', CustomType::class);
Look at the FormFactory and FormBuilder APIs for more information.
You can try it, remove prefix on field name
public function getBlockPrefix()
{
return null;
}
Problem: I wrote a conditional VH (extending AbstractConditionViewHelper) and it works as usually, anyway I realized that in non-cached version it is evaluated only once. Initialy I thought that's my bug, but checked common <f:if> and the problem is identical :S
In general when I visit my page for the first time, condition is evaluated and valid result is given, but when I'll refresh the page VH isn't called anymore (checked by setting breakpoint inside the VH) and VH is always treated as FALSE. Only any change in view's code will cause that VH will be evaluated once, and again next refresh(es) won't call VH anymore.
typo3conf/ext/toolbox/Classes/ViewHelpers/IsFieldRequiredViewHelper.php:
<?php
namespace Vendor\Toolbox\ViewHelpers;
class IsFieldRequiredViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractConditionViewHelper {
/**
* #param string $fieldName Current field name
* #param string $requiredFields List of required names separated by commas
*
* #return string the rendered string
*/
public function render($fieldName, $requiredFields) {
$requiredArray = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $requiredFields, true);
return (in_array($fieldName, $requiredArray))
? $this->renderThenChild()
: $this->renderElseChild();
}
}
Usage:
{namespace toolbox=Vendor\Toolbox\ViewHelpers}
<toolbox:isFieldRequired fieldName="foo" requiredFields="foo, bar, baz">
<f:then>TRUE</f:then>
<f:else>FALSE</f:else>
</toolbox:isFieldRequired>
For the first hit I have TRUE but later only FALSE.
Any suggestions? Did I missed some important change in ViewHelpers API since 7.x- ?
Of course if extension is cached it will be not visible, as the first hit will be saved in cache with proper VH return.
The AbstractConditionViewHelper implements the TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface interface. This means that it implements a compile method that actually returns PHP code that will be stored in the compiled Fluid views.
Have a look at this method in the source code:
public function compile($argumentsVariableName, $renderChildrenClosureVariableName, &$initializationPhpCode, \TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\AbstractNode $syntaxTreeNode, \TYPO3\CMS\Fluid\Core\Compiler\TemplateCompiler $templateCompiler)
{
foreach ($syntaxTreeNode->getChildNodes() as $childNode) {
if ($childNode instanceof ViewHelperNode
&& $childNode->getViewHelperClassName() === ThenViewHelper::class) {
$childNodesAsClosure = $templateCompiler->wrapChildNodesInClosure($childNode);
$initializationPhpCode .= sprintf('%s[\'__thenClosure\'] = %s;', $argumentsVariableName, $childNodesAsClosure) . LF;
}
if ($childNode instanceof ViewHelperNode
&& $childNode->getViewHelperClassName() === ElseViewHelper::class) {
$childNodesAsClosure = $templateCompiler->wrapChildNodesInClosure($childNode);
$initializationPhpCode .= sprintf('%s[\'__elseClosure\'] = %s;', $argumentsVariableName, $childNodesAsClosure) . LF;
}
}
return sprintf('%s::renderStatic(%s, %s, $renderingContext)',
get_class($this), $argumentsVariableName, $renderChildrenClosureVariableName);
}
Once compiled, the render() method will not be called anymore (it will on the first invocation, when the template is not yet compiled). Instead, the renderStatic() method will be called.
Solution: You can either
also override the renderStatic() method and implement your ViewHelper logic there (again)
not implement the render() method and simply overwrite the static evaluateCondition($arguments) method. This method is actually designed to be overwritten -- the default implementations of both render() and renderStatic() call this method:
This method decides if the condition is TRUE or FALSE. It can be overriden in extending viewhelpers to adjust functionality.
static protected function evaluateCondition($arguments = null)
{
$requiredArray = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $arguments['requiredFields'], true);
return (in_array($arguments['fieldName'], $requiredArray));
}
The quickest solution is to overwrite the class render and evaluateCondition like this:
public function initializeArguments()
{
parent::initializeArguments();
$this->registerArgument('yourArgument','array','',true);
}
public function render()
{
return self::evaluateCondition($this->arguments) ? $this->renderThenChild() : $this->renderElseChild();
}
/**
* #return bool
*/
protected static function evaluateCondition($arguments = null)
{
//do your stuff
return true;
}
What is a Zend View Filter? I see them mentioned in the ZF1 documentation, http://framework.zend.com/manual/1.12/en/zend.view.introduction.html, and in the Zend_View code, but I can't find an explanation for them.
Perhaps it is to support other templating systems which have filters? In that case, what do the filters do in these templating systems?
Thanks!
here is an example of a Zend View Filter:
http://dev.bigace.org/api/3.0/Bigace_Zend/View_Filter/Bigace_Zend_View_Filter_ObfuscateMailto.html
It filters found mailto links and obfuscates them.
A Zend View Filter does something on an already rendered phtml file (= html code) before it is send to the client.
It's a Zend_Filter that can be used on the Zend View output.
Here is another example with code from:
http://www.phpgangsta.de/zend_view-output-filter-whitespaces-aus-html-entfernen
The filter class (filters whitespaces from html = less code to send):
<?php
class App_View_Filter_Minify implements Zend_Filter_Interface
{
public function filter($string)
{
return preg_replace(
array('/>\s+/', '/\s+</', '/[\r\n]+/'),
array('>', '<', ' '),
$string
);
}
}
And then adding the filter to the view:
/**
* Add Output filters to View
*
* #return void
*/
protected function _initViewFilter()
{
$view = $this->getResource('view');
$view->addFilterPath('App/View/Filter', 'App_View_Filter_')
->addFilter('Minify');
}
I am using the extension News System, "news", and while changing the templates, I've noticed that while I can use things like {newsItem.datetime} or {newsItem.uid}, I cant use this with the custom fields i have created when extending the table tx_news_domain_model_news, like {newsItem.mycustomfield}
Edit: I have been pointed to this url and I've followed the instructions, but it's not working. This is my code
News.php
<?php
class Tx_WedoExtendnews_Domain_Model_News extends Tx_News_Domain_Model_News {
/**
* #var string
*/
protected $txWedoextendnewsLocation;
public function getTxWedoextendnewsLocation() {
return "this";
return $this->txWedoextendnewsLocation;
}
public function getWedoextendnewsLocation() {
return "that";
return $this->txWedoextendnewsLocation;
}
}
?>
Since I wasn't getting anything, I changed the returning values to string literals, to see if the problem was in the class and method names, or the property. Im still not getting anything. I think the underscored might be playing tricks on my code.
My extension key is wedo_extendnews and the new field is tx_wedoextendnews_location. Any ideas where the error lies?
Yes. To be able to access an object in fluid, you need the according setters in your model and maybe (not sure right now) an entry in the TCA.
If you want to access {newsItem.mycustomfield} you need an according setter in the model, like public function getMycustomfield() (note the get in get<Myfuncname>, it is mandatory).