0

I wish to set some properties in MyFilter with constructor injection but it seems impossible with Zend_View::addFilter(string $filter_class_name) since it loads a new instance upon usage. MyFilter implements Zend_Filter_Interface.

Can I somehow inject an instance of a filter to an instance of Zend_View?

Closing since it (hopefully) will be pushed into 2.0, see ticket on JIRA.

chelmertz
  • 20,399
  • 5
  • 40
  • 46

5 Answers5

0

You may pass object:

$filter = new Your_Filter($params); // implements Zend_Filter_Interface
$view->addFilter($filter);

You may get view instance from viewRenderer, e.g. using staticHelper.

Edit:

The other method may be:

class MyFilterSetup extends MyFilter implements Zend_Filter_Interface 
{
     public function __construct($params)
     {
           $this->_params = $params;
           parent::__construct();
     }

     public function filter($string)
     {
         // .... $this->_params;
     }
}
takeshin
  • 49,108
  • 32
  • 120
  • 164
  • Your code is invalid. `Zend_View::addFilter()` takes a string (or array of strings) as argument and loads the classes dynamically later. If you in fact can get your code to run, feel free to write some more lines of code to cover it :) Thanks. – chelmertz Apr 19 '10 at 20:18
  • The new code example works like the first, still no chance to actually get a value into it before it's used inside Zend_View. – chelmertz Apr 20 '10 at 05:20
0

I'm not certain, but I don't think it's possible. Looking at the sourcecode setFilter() and addFilter() only accept the Filter Classname as a string. You cannot set any options, like you can in Zend_Form for instance. What you could do though is:

class MyFilter implements Zend_Filter_Interface 
{
     protected static $_config;
     public static setConfig(array $options)
     {
         self::_config = $options;
     }
     // ... do something with the options
}

and then you set the options where needed with MyFilter::setOptions(), so when Zend_View instantiates the Filter instance, it got what it needs to properly run the filter.

Gordon
  • 312,688
  • 75
  • 539
  • 559
  • That is nearly one option but not very elegant. What if I would want to add the same filter twice, but with different settings? – chelmertz Apr 20 '10 at 07:53
  • @chelmertz Then you likely would have to customize `Zend_View` to do what you want to do. Or store the number of instantiations the filter went through as another static property and add one config per instance statically. That ain't pretty, but would work. – Gordon Apr 20 '10 at 08:09
  • I'd rather use the output of `Zend_View::render()` and apply a filter on that, but it takes away the power of having associated fitlers with the view. Gonna push this further if there isn't something we've missed. – chelmertz Apr 20 '10 at 08:26
0

You can't in the 1.x branch, ticket is filed:

http://framework.zend.com/issues/browse/ZF-9718

chelmertz
  • 20,399
  • 5
  • 40
  • 46
0

Can't we create a custom view object extending Zend_View that overrides the addFilter() method to accept either a class or an instance. Then override the _filter() method to deal with both types of filters - string and instance - that we have stored.

David Weinraub
  • 14,144
  • 4
  • 42
  • 64
  • I think there's a design error and that those should be revised within the framework in itself. But I think it's a valid solution so.. why not patch it? :) – chelmertz Oct 06 '10 at 19:01
0

Why not assign the filter properties to the view, and then either set the properties when the view is set, or access the view directly in your filtering function? e.g.

$view->assign('MyFilterProperty', 'fubar');

and then in your filter class:

public function setView($aView)
{
    $this->_property = $aView->MyFilterPropery;
}

It's kludgy, but it should get the job done.

Duncan
  • 2,056
  • 13
  • 11
  • I do not think this approach would work because 1) the filter would depend (or hijack) each property, and; 2) it does not filter all of the output. Imagine I'd like to apply a filter invoking `str_replace()`-functionality on the regular rendering of the view, in two sequential calls, with different parameters (sorry for a dumb example:) – chelmertz Apr 14 '11 at 18:28