ZF-11910: Zend_View_Helper_HeadMeta fatal error when no View object is set
Description
$headmeta = new Zend_View_Helper_HeadMeta(); $width = 320; $min_scale = 1; $max_scale = 1; $headmeta->headMeta()->appendName('viewport',"$width, minimum-scale=$min_scale, maximum-scale=$max_scale, user-scalable=no");
This is part of source code of My_Controller_Plugin_DevicePresets extends Zend_Controller_Plugin_Abstract, so it's a plugin controller. Running this code causes a fatal error: Fatal error: Call to a member function doctype() on a non-object in ZendFramework\library\Zend\View\Helper\HeadMeta.php on line 212.
Which is (roughly):
if (!$this->view->doctype()->isRdfa()
I noticed this bug when decided to update ZF lib from 1.11.2 to 1.11.6, and that it's also there with latest release.
Comments
Posted by Adam Lundrigan (adamlundrigan) on 2011-11-30T14:19:06.000+0000
The {{Zend_View_Helper_HeadMeta}} class requires an instance of {{Zend_View}}, and does not adequately check to ensure one is provided before trying to call other view helpers. I would either pull the instance of {{Zend_View}} from your application and inject it into HeadMeta...but passing in a new instance works too:
I think the view helper should be updated to do the above automatically if no {{Zend_View}} instance is provided to the helper before it's used.
Posted by Tymoteusz Paul (arleen) on 2011-11-30T14:37:55.000+0000
I worked around it by rolling back ZF library to 1.11.2 and this is my main issue here - is that a change i have missed (and if it is, then why helper doesn't throw an exception about missing view before error strikes?) or just an error somewhere? I don't really have the time to do a regression test and check where the problem lays.
Posted by Adam Lundrigan (adamlundrigan) on 2011-11-30T14:58:52.000+0000
The code causing the issue (call to $this->view->doctype()->isRdfa()) was added in 1.11.3 (see ZF-9743), and so any use of {{Zend_View_Helper_HeadMeta}} without first providing a {{Zend_View}} object is broken since that release.
The simplest fix is this:
If there is no view then there is no doctype to parse, and so we have to assume it's HTML (and not RDF) so we can safely short-circuit that if statement in this case. I'll work up a more complete fix w/ unit tests when I get a chance.
Posted by Adam Lundrigan (adamlundrigan) on 2012-03-20T23:37:47.000+0000
Attached patch which protects helper against calls to {{$this->view}} when no view has been provided.
Posted by Adam Lundrigan (adamlundrigan) on 2012-05-08T18:38:25.000+0000
Fixed in trunk (1.12.0): r24776