What is a Zend View Filter? - zend-framework

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

Related

How to see AJAX query behind list view in Backpack?

Is it possible in Backpack 4.1 to view the actual query that is run behind a list view, the query that is AJAXed? Would be useful for debugging purposes.
Having a look at the query builder object being used is pretty trivial, you can get it in a controller via $this->crud->query. However, getting the actual query that will be run on your DB with the PDO bindings in place for any where clauses that might be applied etc will take a little bit of setup.
You should be able to do this:
1) Make a file like app/Helpers/Database.php with this content:
if (! function_exists('asSql')) {
/**
* Combines SQL and its bindings
*
* #param \Eloquent | \Illuminate\Database\Eloquent\Builder | \Illuminate\Database\Query\Builder $query
* #return string
*/
function asSql($query)
{
return vsprintf(str_replace('?', '%s', $query->toSql()), collect($query->getBindings())->map(static function ($binding) {
$binding = addslashes($binding);
return is_numeric($binding) ? $binding : "'{$binding}'";
})->toArray());
}
}
2) In app/Providers/AppServiceProvider.php add this to register that new helper method:
/**
* Register services.
*
* #return void
*/
public function register()
{
// require all files in app/Helpers/ so their functions get added globally
foreach (glob(app_path('Helpers') . '/*.php') as $file) {
require_once $file;
}
}
3) With phpstorm (or similar IDE) set a breakpoint in vendor/backpack/crud/src/app/Library/CrudPanel/Traits/Read.php->getEntries()
set your breakpoint on this line $entries = $this->query->get();
If you're in php storm, load a list page and let the breakpoint hit, open the evaluator tool and run asSql($this->query) and you will see the fully bound query that will be run to get your records.
Like this:
This is the query for my user CRUD for which I've added the below in my setup:
$this->crud->addClause('where', 'active', 1);
$this->crud->orderBy('email');
NOTE, this probably wont show queries run to get the relationships for those models if applied, that gets a lot trickier in some cases.
If you are not running php storm with xdebug, or you just dont want to do it there, you could also add do this from your crud controller at the bottom of you setupListOperation method with something like
$sql = asSql($this->crud->query);
dd($sql);

How to get current WordPress category in FishPig in Magento 2?

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

template for list view with system categories

I have an extbase extension (TYPO3 7) with a simple model of a contact person.
The person has a name and a picture.
So far this is clear.
But every Person has a category (e.g. where he works. Office, Marketing etc.)
Therefor i use the system categories, as described here:
https://wiki.typo3.org/TYPO3_6.0#Adding_categories_to_own_models_without_using_Extension_Builder
When creating a person via web > list, i can assign a category.
Now the question for templating:
If i debug my contact person, i get the output like screen below.
I want to have a list where every category (headline) is shown with it's contact persons.
How to do this?
Is the logic for this only in the template or also in the controller?
Has anybody an example for this?
Best regards
Markus
I guess the required logic you need is possible with Fluid with using the GroupedFor ViewHelper and many others. Because a person can have multiple categories this would become a huge nesting of Viewhelpers so I can not recommend to use Fluid for this even if its possible. This kind of logics belong to the controllers, models and repositories.
There are multiple ways to solve this logic. Here is an example how to realize this in the controller...
Controller:
/**
* #var \TYPO3\CMS\Extbase\Domain\Repository\CategoryRepository
* #inject
*/
protected $categoryRespoitory = NULL;
/**
* action list
* #return void
*/
public function listAction()
{
$allCategories = $this->categoryRespoitory->findAll();
$categoriesWithContacts = [];
/** #var \TYPO3\CMS\Extbase\Domain\Model\Category $category */
foreach($allCategories as $category) {
$contactsInCategory= $this->contactRepository->findByCategory($category);
if($contactsInCategory->count()>0) {
$categoriesWithContacts[] = [
'category' => $category,
'contacts' => $contactsInCategory
];
}
}
$this->view->assignMultiple([
'categoriesWithContacts' => $categoriesWithContacts
]);
}
Injecting the CategoryRespository will required clearing cache in install tool or reinstalling the extension.
Maybe you need this function in your ContactRepository:
/**
* #param \TYPO3\CMS\Extbase\Domain\Model\Category $category
* #return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
*/
public function findByCategory(\TYPO3\CMS\Extbase\Domain\Model\Category $category) {
$query = $this->createQuery();
return $query->matching($query->contains('categories', $category))->execute();
}
Then in Fluid you can do something like this:
<f:for each="{categoriesWithContacts}" as="categoryWithContact">
{categoryWithContact.category.title}
<f:for each="{categoryWithContact.contacts}" as="contact">
{contact.name}
</f:for>
</f:for>

Zend sanitizing input

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.

Zend Framework: Get whole out output in postDispath while using Layout

I have a layout loader plugin which looks like this:
class Controller_Action_Helper_LayoutLoader extends Zend_Controller_Action_Helper_Abstract
{
public function preDispatch()
{
$config = Zend_Registry::get("config");
$module = $this->getRequest()->getModuleName();
if (isset($config->$module->resources->layout->layout) && !$this->getRequest()->format)
{
$layoutScript = $config->$module->resources->layout->layout;
$this->getActionController()->getHelper('layout')->setLayout($layoutScript);
}
}
}
In a controller plugin I then want to get the whole of the response like so
$this->getResponse()->getBody()
This however only returns the output from the action, not the output from the layout too.
How can I get the whole output, layout and action together?
Thanks!
I believe that Zend_Layout operates at postDispatch() with a high stack index. So, to get the content, you might need to do your access later, at dispatchLoopShutdown().