oveleon/contao-cookiebar

Debug Mode not working: Attempt to read property "rootId" on null (2.0.0-RC2)

Closed this issue · 12 comments

Using Contao 5.3 and Cookiebar 2.0.0-RC2. When activating the debug mode in Contao Manager, I cannot load any page with error Warning: Attempt to read property "rootId" on null. Disabling cookiebar or without debug mode, everything seems to work.

Stack Trace:

ErrorException:
Warning: Attempt to read property "rootId" on null

  at vendor/contao-themes-net/theme-components-bundle/src/EventListener/ParseFrontendTemplateHook.php:33
  at ContaoThemesNet\ThemeComponentsBundle\EventListener\ParseFrontendTemplateHook->__invoke('<!-- TEMPLATE START: vendor/oveleon/contao-cookiebar/contao/templates/cookiebar/cookiebar_simple.html5 --><!-- indexer::stop --><div  class="contao-cookiebar cookiebar_simple cc-bottom" role="complementary" aria-describedby="cookiebar-desc" data-nosnippet>[...]</div><!-- indexer::continue --><!-- TEMPLATE END: vendor/oveleon/contao-cookiebar/contao/templates/cookiebar/cookiebar_simple.html5 -->', 'cookiebar_simple', object(FrontendTemplate))
     (vendor/contao/core-bundle/contao/classes/FrontendTemplate.php:50)
  at Contao\FrontendTemplate->parse()
     (vendor/oveleon/contao-cookiebar/src/Cookiebar.php:276)
  at Oveleon\ContaoCookiebar\Cookiebar::parseCookiebarTemplate(object(CookiebarModel), 'de')
     (vendor/oveleon/contao-cookiebar/src/EventListener/KernelRequestListener.php:94)
  at Oveleon\ContaoCookiebar\EventListener\KernelRequestListener->prepareCookieBar()
     (vendor/oveleon/contao-cookiebar/src/EventListener/KernelRequestListener.php:70)
  at Oveleon\ContaoCookiebar\EventListener\KernelRequestListener->onKernelRequest(object(RequestEvent), 'kernel.request', object(TraceableEventDispatcher))
     (vendor/symfony/event-dispatcher/Debug/WrappedListener.php:116)
  at Symfony\Component\EventDispatcher\Debug\WrappedListener->__invoke(object(RequestEvent), 'kernel.request', object(TraceableEventDispatcher))
     (vendor/symfony/event-dispatcher/EventDispatcher.php:220)
  at Symfony\Component\EventDispatcher\EventDispatcher->callListeners(array(object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener)), 'kernel.request', object(RequestEvent))
     (vendor/symfony/event-dispatcher/EventDispatcher.php:56)
  at Symfony\Component\EventDispatcher\EventDispatcher->dispatch(object(RequestEvent), 'kernel.request')
     (vendor/symfony/event-dispatcher/Debug/TraceableEventDispatcher.php:139)
  at Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher->dispatch(object(RequestEvent), 'kernel.request')
     (vendor/symfony/http-kernel/HttpKernel.php:157)
  at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), 1)
     (vendor/symfony/http-kernel/HttpKernel.php:76)
  at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), 1, true)
     (vendor/symfony/http-kernel/Kernel.php:197)
  at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
     (web/index.php:42)

Hello @mrflory

you are right. The global $objPage is currently not set at this point when the parseFrontendTemplate-Hook ist called.
We have to check if we could render the Template at later point ...

In Contao 5 the page should always be available under

$request->attributes->get('pageModel')

If it's not, then the request is either not a request in a frontend _scope - or the request is not handled by a regular Contao page.

Yes, I thought that too but at this point global $objPage=null even though the PageModel is in the request.
When does Contao set the global $objPage?

It could also bet set here but somehow I don't think it's right. The other globals are probably not available either...

e.g.

...
 if ($rootPageObject instanceof PageModel)
 {
     $this->objRootPage = $rootPageObject;
     $this->objPage = $pageModel;

     global $objPage;
     $objPage = $pageModel;

     $this->prepareCookieBar();
                
  }
...

the $GLOBALS seems to be fine at this point!
So maybe it´s ok to set the global $objPage to get working the parseTemplate hooks ...
What do you mean? Or should Contao have already set this at this point?

You should not use global $objPage at all. See my previous post on how to retreive the PageModel.

yes we do not use the global $objPage but vendor/contao-themes-net/theme-components-bundle/src/EventListener/ParseFrontendTemplateHook.php does and currently it´s working in Contao 5.x.
So I think we should support that too or not?

No, no code should ever use global $objPage;, especially not new code ;)

@mrflory can you test the pull-request branch and see if it works for you?

Composer package repository is:
https://github.com/xprojects-de/contao-cookiebar

Branch name is:
dev-bugfix/globalObjPage

Here is a tutorial on how to set up another repository URL:
https://getcomposer.org/doc/05-repositories.md#loading-a-package-from-a-vcs-repository

@mrflory do you have any news regarding your issue?
Did you try out the pull request?

I can not test it if it will work as I am not using the theme you are working with.

Sorry no time yet, maybe tonight

Yes Debug Mode works with the pull-request. Thanks a lot.