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

Issue Type: Bug Created: 2011-11-24T12:47:14.000+0000 Last Updated: 2012-05-08T18:38:25.000+0000 Status: Resolved Fix version(s): Reporter: Tymoteusz Paul (arleen) Assignee: Adam Lundrigan (adamlundrigan) Tags: - Zend_View

  • FixForZF1.12
  • View_Helper
  • zf-caretaker-adamlundrigan
  • zf-crteam-review

Related issues: Attachments: - ZF-11910.patch


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


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:

<pre class="highlight">
$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.

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:

<pre class="highlight">
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.

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

Have you found an issue?

See the Overview section for more details.


© 2006-2018 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.