• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

XF 2.0 Add mobile detect variables to $visitor object

#1
I need some variables in the visitor object for later template conditions.

Actually i make a:

code event listener: visitor_setup

PHP:
<?php
namespace My/Addon;

use My/Addon/Mobile_Detect;

class Listener
{

    public static function visitorSetup(\XF\Entity\User &$visitor)
    {
        /* Mobile_Detect to visitor Objekt */
            
        $is_mobile = $detect->isMobile();
        $is_smartphone = $detect->isMobile() && !$detect->isTablet();
        $is_tablet = $detect->isTablet();
    
        $visitor ['is_mobile'] = $is_mobile;
        $visitor ['is_smartphone'] = $is_smartphone;
        $visitor ['is_tablet'] = $is_tablet;
    }
}
ERROR:
Code:
An exception occurred: [InvalidArgumentException] Column 'is_mobile' is unknown in src/XF/Mvc/Entity/Entity.php on line 536
In Xenforo 1.5 this was working good. Can someone help?
If someone have a better idea to do that, say it to me.

Final it should work like this for example:

HTML:
<xf:if is="!$visitor['is_smartphone']">
    show content desktop
</xf:if>
 
Last edited:
#2
i extend now the User Entity like that:

PHP:
<?php

namespace My\Addon\XF\Entity;

use XF\Mvc\Entity\Structure;

class User extends XFCP_User
{
    public static function getStructure(Structure $structure)
    {
        $structure = parent::getStructure($structure);

        $structure->columns['is_mobile'] = ['type' => self::BOOL, 'default' => false];
        $structure->columns['is_smartphone'] = ['type' => self::BOOL, 'default' => false];
        $structure->columns['is_tablet'] = ['type' => self::BOOL, 'default' => false];

        return $structure;
    }
}
now i can use

HTML:
<xf:if is="$xf.visitor.is_mobile">
    code for mobile device
</xf:if>
but I do not know if there is a better way to do that?
 

Chris D

XenForo developer
Staff member
#3
I'd recommend you approach it like this:
PHP:
<?php

namespace My\Addon\XF\Entity;

use XF\Mvc\Entity\Structure;

class User extends XFCP_User
{
    public function isMobile()
    {
        // not sure where $detect is coming from, but I assume you'll handle that here
        return $detect->isMobile();
    }

    // ... etc.

    public static function getStructure(Structure $structure)
    {
        $structure = parent::getStructure($structure);

        $structure->getters['is_mobile'] = ['getter' => 'isMobile', 'cache' => true];
        $structure->getters['is_smartphone'] = ['getter' => 'isSmartphone', 'cache' => true];
        $structure->getters['is_tablet'] = ['getter' => 'isTablet', 'cache' => true];

        return $structure;
    }
}
"Getters" enable you to add additional properties to entity classes. The entity system handles caching for you too.
 
#7
PHP:
<?php

namespace My\Addon\XF\Entity;

use XF\Mvc\Entity\Structure;
use My\Addon\Mobile_Detect;

class User extends XFCP_User
{
    public function isMobile()
    {
        $detect = new Mobile_Detect;
       
        return $detect->isMobile();
    }
   
    public function isSmartphone()
    {
        $detect = new Mobile_Detect;
       
        return $detect->isMobile() && !$detect->isTablet();
    }
   
    public function isTablet()
    {
        $detect = new Mobile_Detect;
       
        return $detect->isTablet();
    }
   
    public static function getStructure(Structure $structure)
    {
        $structure = parent::getStructure($structure);

       $structure->getters['is_mobile'] = ['getter' => 'isMobile', 'cache' => true];        
       $structure->getters['is_smartphone'] = ['getter' => 'isSmartphone', 'cache' => true];        
       $structure->getters['is_tablet'] = ['getter' => 'isTablet', 'cache' => true];

        return $structure;
    }
}
 
Last edited: