V9 Can't get profile object in block controller on_start()

I’m using code as such in my block controller on_start() method to get the current profile user id:

$userID = 0;
$c = Page::getCurrentPage();
$profile = $c->getPageController()->get('profile');
if (is_object($profile) && $profile->getUserID()) {
	$userID = $profile->getUserID();
}

This works as expected in v8, in v9 $userID never gets set. My next check is if($userID >= 1) fails, and the rest of my code does not work properly.

Similarly, if I place the following into my block view.php:

$c = Page::getCurrentPage();
$profile = $c->getPageController()->get('profile');
if (is_object($profile) && $profile->getUserID()) {
	echo $profile->getUserID();
}

I get the proper user id output to the page. v8 AND v9.

I’m stumped. Anybody have any ideas what’s happening here? Is there a better way to be doing this now?

Just guessing here. It could be one of those things that is actually an object, but automatically stringifies when used in a string context.

You can force such to string by coercion (string)$object or concatenation $object."" (empty string).

More generally, rather than checking uid as a number, get the user object then do
if($u->isRegistered())

Another thought, have you tried using an on_block_load or placing similar at the top of view()?

1 Like
$c = Page::getCurrentPage();
$profile = $c->getPageController()->get('profile');
    //if (is_object($profile) && $profile->getUserID()) {
        $userID = $profile->getUserID();
    //}

If I comment out the check v8 looks good, in v9, $profile is null
Call to a member function getUserID() on null

Edit: v8/v9 both latest release…

view.php test code works in view() as well

try
var_dump_safe($c->getPageController(), true, 2); // 2 is the depth to dump. May need 3 or 4.
That will tell you what the page controller class is and what properties are set.

Does ‘profile’ only exist for the single page, and perhaps it doesn’t exist for other pages beneath the single page?

Playing with the settings in my use case, it doesn’t appear to exist in any v9 profile page scenario:

/members/profile(/view), /members/profile(/view)/1, /members/profile(/view)/2, etc

I find this in all scenarios with the var_dump v9:

[“profile”]=> string(27) “Concrete\Core\User\UserInfo”

I’m a bit surprised how this code would work in the controller’s on_start method in v8

$profile is set in the page’s controller’s view() method so i think when you try to grab it from on_start() view() hasn’t run yet.

After running tests I can confirm the block’s on_start() method is run before the profile page’s controller view() method in v9 so $profile is set after you run your code

It should really be the same in v8

@mnakalay, that explains it. Not certain how or why it works in v8, but it does. I’ve simply moved that little piece of logic to the view() method, as @JohntheFish notes above, and all is good. Thank you!

1 Like