ZF-11910: Zend_View_Helper_HeadMeta fatal error when no View object is set


$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.


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:

$headmeta = new Zend_View_Helper_HeadMeta();
$headmeta->setView(new Zend_View());

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.

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.

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:

Index: library/Zend/View/Helper/HeadMeta.php
--- library/Zend/View/Helper/HeadMeta.php (revision 24569)
+++ library/Zend/View/Helper/HeadMeta.php (working copy)
@@ -209,7 +209,7 @@

         //  is only supported with doctype RDFa
-        if (!$this->view->doctype()->isRdfa()
+        if ( !is_null($this->view) && !$this->view->doctype()->isRdfa()
             && $item->type === 'property') {
             return false;

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.

Attached patch which protects helper against calls to {{$this->view}} when no view has been provided.

Fixed in trunk (1.12.0): r24776