Frankentheme mobile theme switching

Permalink
Here's a good one:

I have written some code in a package controller that attempts to switch the page theme depending on a browser cookie and/or a _GET parameter. The native theme switching is NOT enabled, but there are two distinct themes installed.

Now here's the funky part: When I have the cookie set to use the mobile theme, the mobile header and footer element are loaded (I have h1 tags in both so I can be sure), but the template itself is the desktop one (again H1 to the rescue). So I have a page built with the mobile header and footer and the desktop content. If I switch themes in the dashboard, the effect is reversed, with desktop header and footer and a mobile template.

This is the code from my package controller:

class Controller extends Package {
/////etc.....
   public function on_start(){
      Events::addListener('on_before_render', function($event) {
/* some cookie related stuff */
$theme = Theme::getByHandle($current == 'mobile' ? 'custom_mobile' : 'custom');
if (is_object($theme)) {
      $event['view']->controller->setTheme($theme);
      $event['view']->setViewTheme($theme);
   }
});
}
}


It's driving me nuts! Any suggestions as to what I'm doing wrong?

jero
 
jero replied on at Permalink Reply
jero
Weird!
mnakalay replied on at Permalink Best Answer Reply
mnakalay
Hi jero,
I had something a bit related and for me it was solved by hooking on the on_start event instead of the on_before_render event. To be clear, I do mean the on_start EVENT not the on_start() function in the package's controller
jero replied on at Permalink Reply
jero
@mnakalay thanks, but the on_start event didn't make any difference, but you set me on the right path. I tried on_page_view, and once I'd adjusted the code for the page event object that's passed instead of the Symfony generic event, it all works. The monster is dead!

Just in case anyone else has this problem
public function on_start(){
      Events::addListener('on_page_view', function($event) {
         /* @var $event \Concrete\Core\Page\Event */
         $session = Core::make('app')->make('session');
         $current = $session->get('customTheme');
         if (!$current) {
            $md = new \Mobile_Detect();
            if ($md->isMobile())
               $session->set('customTheme', 'mobile');
            else
               $session->set('customTheme', 'desktop');
         }
         if (array_key_exists('t', $_GET)) {
            $required = $_GET['t'];
            if ($required == 'mobile' || $required == 'desktop') {
mnakalay replied on at Permalink Reply
mnakalay
Glad i could kind of help ;-)