1

Problem

I'm trying to load the current entity object, in this case a profile2, profile page.

I've been using Is there a way to load the current entity object?, to try and solve this. Where am I going wrong?

So Far

I have the following code running in a custom form inside a block. The block appears on the profile page.

function mymodule_form() {
    $node = menu_get_object('profile2', 1);
    dpm($node);
}

The path of my page is: localhost/drupal5/profile-main/154

I have two kinds of profile 2, machine names main and school, however the following doesn't work either.

$node = menu_get_object('main', 1);
4
  • What do you get for arg(0), arg(1), arg(2) and arg(3) respectively in that same function? It might just be you're picking the wrong part of the URL for the ID (your menu_get_object() code looks right)
    – Clive
    Commented Sep 18, 2013 at 13:07
  • I added in $testarg = arg(1); dpm($testarg); Add it threw back 154, so the correct number is going in, but I'm still getting nothing in $node. I guess the entity type must be wrong, but I've been digging through the module and I'm sure it's profile2. drupalcontrib.org/api/drupal/… Commented Sep 18, 2013 at 13:21
  • What if you use profile2_load() instead? If that doesn't work then the ID (154) probably relates to the user, not the profile, so you could use profile2_load_by_user() along with user_load()
    – Clive
    Commented Sep 18, 2013 at 13:25
  • Ok profile2_load does work. But you guessed it and the UID is not equal to PID (because profiles are not created automatically) so I've used profile2_load_by_user with arg(1), to get the correct entity. No idea why menu_get_object didn't work, nevermind. Thanks very much! Commented Sep 18, 2013 at 13:45

1 Answer 1

1

I couldn't get menu_get_object to work with profile2 so instead I used a combination of profile2_load_by_user and arg suggested by Clive.

function mymodule_form() {
   //this arg gets the UID from the URL.
    $testarg = arg(1);

  //The UID is then put into a profile2 defined function which locates and opens the
  //correct profile.
    $currentprofile = profile2_load_by_user($testarg);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.