XF 2.1 isMemberOf in PHP file

Ozzy47

Well-known member
Okay what am I doing wrong here?

In my listener file I have this.

PHP:
<?php

namespace OzzModz\Myaddon;

class Listener
{
    public static function appSetup(\XF\App $app)
    {
        // get options
        $options = \XF::options();

        // Get variables
        $this= $options->optionone;
        $that = $options->optiontwo;
        $groups = $options->select_groups;
        $visitor = \XF::visitor();
       
        // If this
        if ($this && $visitor->isMemberOf($groups))
        {
            dothis;
        }
         
        // If that
        if ($that && $visitor->isMemberOf($groups))
        {
            dothat;
        }
         
        if ($this || $that)
        {
            dothem;
        } 
    }   
}

Now if I remove && $visitor->isMemberOf($groups) everything works as intended. But once that is added, it only works if I select every usergroup frpm my ACP option. My ACP option is a XF\Option\UserGroup::renderSelectMultiple which is an array.
 
Tried with
PHP:
\XF::dumpSimple(\XF::visitor()->isMemberOf([3]));
// and
\XF::dumpSimple(\XF::visitor()->isMemberOf(['3']));
// and
\XF::dumpSimple(\XF::visitor()->isMemberOf('3'));
// and
\XF::dumpSimple(\XF::visitor()->isMemberOf(3));

All of them seem to be returning true so something is wrong with $this and $that.
 
Oddly enough if I drop $this only use $visitor->isMemberOf($groups) nothing shows either. :(
 
If you select ALL of the groups in your option and it works, then the visitor is not in the groups you have selected in your option when you only select one or two groups.

Assuming you have user group 3 selected in your option....

Dump $visitor, expand the #_values section and check both the user_group_id and if user group 3 is in the secondary_group_ids for the visitor.
Code:
"user_group_id" => 2
"secondary_group_ids" => "3,4,30,64"

If 3 is not in either one of those then the visitor is not in one of the user groups selected in the $groups you have selected in your option and therefore won't execute the code where you expect them to be in the group with $visitor->isMemberOf($groups).

If you want the code to execute when they are NOT in that group, the code would be !$visitor->isMemberOf($groups)
 
Okay, running this, \XF::dumpSimple(\XF::visitor()->isMemberOf([3])); returns false, as well as \XF::dumpSimple(\XF::visitor()->isMemberOf([2])); only running, \XF::dumpSimple(\XF::visitor()->isMemberOf([1])); returns true. So this tells me that either I am not getting the visitor state correctly, or it is not accessible for some reason.
 
Okay, running this, \XF::dumpSimple(\XF::visitor()->isMemberOf([3])); returns false, as well as \XF::dumpSimple(\XF::visitor()->isMemberOf([2])); only running, \XF::dumpSimple(\XF::visitor()->isMemberOf([1])); returns true. So this tells me that either I am not getting the visitor state correctly, or it is not accessible for some reason.
Is the user you're using to view the page an Unregistered/Unconfirmed user?
 
Right after this in your code $visitor = \XF::visitor();

Put \XF::dumpSimple($visitor); and post the ["_values":protected] section here.
 
Code:
object(XFRM\XF\Entity\User)#226 (15) {
  ["_uniqueEntityId":"XF\Mvc\Entity\Entity":private] => int(5)
  ["rootClass":protected] => string(14) "XF\Entity\User"
  ["_useReplaceInto":protected] => bool(false)
  ["_newValues":protected] => array(0) {
  }
  ["_values":protected] => array(32) {
    ["user_id"] => int(0)
    ["style_id"] => int(0)
    ["language_id"] => int(0)
    ["timezone"] => string(15) "America/Chicago"
    ["visible"] => int(1)
    ["activity_visible"] => int(1)
    ["user_group_id"] => int(1)
    ["secondary_group_ids"] => string(0) ""
    ["display_style_group_id"] => int(0)
    ["permission_combination_id"] => int(1)
    ["message_count"] => int(0)
    ["alerts_unread"] => int(0)
    ["conversations_unread"] => int(0)
    ["register_date"] => int(1557178320)
    ["last_activity"] => int(1557178320)
    ["trophy_points"] => int(0)
    ["avatar_date"] => int(0)
    ["avatar_width"] => int(0)
    ["avatar_height"] => int(0)
    ["avatar_highdpi"] => int(0)
    ["gravatar"] => string(0) ""
    ["user_state"] => string(5) "valid"
    ["is_moderator"] => int(0)
    ["is_admin"] => int(0)
    ["is_staff"] => int(0)
    ["is_banned"] => int(0)
    ["reaction_score"] => int(0)
    ["custom_title"] => string(0) ""
    ["warning_points"] => int(0)
    ["privacy_policy_accepted"] => int(0)
    ["terms_accepted"] => int(0)
    ["xfrm_resource_count"] => int(0)
  }
  ["_relations":protected] => array(4) {
    ["Auth"] => object(XF\Entity\UserAuth)#219 (15) {
      ["_uniqueEntityId":"XF\Mvc\Entity\Entity":private] => int(1)
      ["rootClass":protected] => string(18) "XF\Entity\UserAuth"
      ["_useReplaceInto":protected] => bool(false)
      ["_newValues":protected] => array(0) {
      }
      ["_values":protected] => array(1) {
        ["user_id"] => int(0)
      }
      ["_relations":protected] => array(0) {
      }
      ["_previousValues":protected] => array(0) {
      }
      ["_options":protected] => array(0) {
      }
      ["_deleted":protected] => bool(false)
      ["_readOnly":protected] => bool(false)
      ["_writePending":protected] => bool(false)
      ["_writeRunning":protected] => bool(false)
      ["_errors":protected] => array(0) {
      }
      ["_cascadeSave":protected] => array(0) {
      }
      ["_behaviors":protected] => NULL
    }
    ["Option"] => object(XF\Entity\UserOption)#221 (15) {
      ["_uniqueEntityId":"XF\Mvc\Entity\Entity":private] => int(2)
      ["rootClass":protected] => string(20) "XF\Entity\UserOption"
      ["_useReplaceInto":protected] => bool(false)
      ["_newValues":protected] => array(0) {
      }
      ["_values":protected] => array(2) {
        ["user_id"] => int(0)
        ["content_show_signature"] => string(1) "0"
      }
      ["_relations":protected] => array(0) {
      }
      ["_previousValues":protected] => array(0) {
      }
      ["_options":protected] => array(0) {
      }
      ["_deleted":protected] => bool(false)
      ["_readOnly":protected] => bool(false)
      ["_writePending":protected] => bool(false)
      ["_writeRunning":protected] => bool(false)
      ["_errors":protected] => array(0) {
      }
      ["_cascadeSave":protected] => array(0) {
      }
      ["_behaviors":protected] => NULL
    }
    ["Profile"] => object(XF\Entity\UserProfile)#223 (15) {
      ["_uniqueEntityId":"XF\Mvc\Entity\Entity":private] => int(3)
      ["rootClass":protected] => string(21) "XF\Entity\UserProfile"
      ["_useReplaceInto":protected] => bool(false)
      ["_newValues":protected] => array(0) {
      }
      ["_values":protected] => array(1) {
        ["user_id"] => int(0)
      }
      ["_relations":protected] => array(0) {
      }
      ["_previousValues":protected] => array(0) {
      }
      ["_options":protected] => array(0) {
      }
      ["_deleted":protected] => bool(false)
      ["_readOnly":protected] => bool(false)
      ["_writePending":protected] => bool(false)
      ["_writeRunning":protected] => bool(false)
      ["_errors":protected] => array(0) {
      }
      ["_cascadeSave":protected] => array(0) {
      }
      ["_behaviors":protected] => NULL
    }
    ["Privacy"] => object(XF\Entity\UserPrivacy)#225 (15) {
      ["_uniqueEntityId":"XF\Mvc\Entity\Entity":private] => int(4)
      ["rootClass":protected] => string(21) "XF\Entity\UserPrivacy"
      ["_useReplaceInto":protected] => bool(false)
      ["_newValues":protected] => array(0) {
      }
      ["_values":protected] => array(1) {
        ["user_id"] => int(0)
      }
      ["_relations":protected] => array(0) {
      }
      ["_previousValues":protected] => array(0) {
      }
      ["_options":protected] => array(0) {
      }
      ["_deleted":protected] => bool(false)
      ["_readOnly":protected] => bool(false)
      ["_writePending":protected] => bool(false)
      ["_writeRunning":protected] => bool(false)
      ["_errors":protected] => array(0) {
      }
      ["_cascadeSave":protected] => array(0) {
      }
      ["_behaviors":protected] => NULL
    }
  }
  ["_previousValues":protected] => array(0) {
  }
  ["_options":protected] => array(0) {
  }
  ["_deleted":protected] => bool(false)
  ["_readOnly":protected] => bool(true)
  ["_writePending":protected] => bool(false)
  ["_writeRunning":protected] => bool(false)
  ["_errors":protected] => array(0) {
  }
  ["_cascadeSave":protected] => array(0) {
  }
  ["_behaviors":protected] => NULL
}

An exception occurred: [ErrorException] [E_WARNING] Cannot modify header information - headers already sent by (output started at /home/ozzmodz/public_html/src/XF/Debugger.php:52) in src/XF/Http/Response.php on line 386

    XF::handlePhpError()
    header() in src/XF/Http/Response.php at line 386
    XF\Http\Response->sendHeaders() in src/XF/Http/Response.php at line 283
    XF\Http\Response->send() in src/XF.php at line 440
    XF::runApp() in index.php at line 20
 
I know, I see that, but I am logged in, viewing the index page, add that code, refresh the page, and that is the output.
 
Top Bottom