I'm building a webpage that uses three separate profile types, this is, the "Show on all user account registration forms" option is not ticked for any of the three. Also, I have configured the registration forms to show up as tabs, and have disabled the "plain" user registration (user/register). The tabs work like a charm, only the fields of each type show for the corresponding type. However, when I try to use the registration blocks, the three following notices appear:

Notice: Trying to get property of non-object in profile2_regpath_block_view() (line 40 of *******/sites/all/modules/profile2_regpath/profile2_regpath_blocks/profile2_regpath_blocks.module).
Notice: Trying to get property of non-object in profile2_regpath_build_block_form() (line 57 of *******/sites/all/modules/profile2_regpath/profile2_regpath_blocks/profile2_regpath_blocks.module).
Notice: Trying to get property of non-object in profile2_regpath_build_block_form() (line 59 of *******/sites/all/modules/profile2_regpath/profile2_regpath_blocks/profile2_regpath_blocks.module).

And no matter what registration block I use, it appends all of the fields by weight, as if the "Show on all user account registration forms" was checked on all three profile types. Of course, the problem doesn't arise when logged in, as the block returned is NULL.

Comments

Axagthoth’s picture

The following two lines are from the function profile2_regpath_block_view($delta = '')

$profile_id = str_replace('p2rp_register_', '', $delta);
$profile_type = profile2_load($profile_id);

If someone could please explain to me what the first one is intended to do, I think I could then pinpoint and solve the issue. All the problematic lines appear after the second one, and have in common that they try to access fields of $profile_type. It is my assumption that something fails at the profile2_load call, since I have tried substituting the referenced values by hardcoded values and the problem disappears. Any help would be appreciated!

Axagthoth’s picture

Component: User interface » Code

Okay, sorry for the frequent updates but this is a pressing matter to me. The line

$profile_id = str_replace('p2rp_register_', '', $delta);

works just fine, retrieving the correct profile_id. However for some reason in the following line

$profile_type = profile2_load($profile_id);

the call to profile2_load always returns false. I'm currently looking at the code in profile2, but as long as I can't solve it:

Does someone know why the call to profile2_load returns false for correct (i.e. existing) profile id's? Has anybody else encountered the issue?
Also excuse my ignorance, but I'm not sure whether this should be in the profile2 issue queue or here.

grasmash’s picture

It's possible that $profile_type is not being correctly cast as an integer.

Let's try this:

$profile_id = (int)str_replace('p2rp_register_', '', $delta);

This may also be specific to your version of php. Can you please tell me which version you are using?

grasmash’s picture

Status: Active » Fixed

Ah, I found the issue. profile2_load() actually loads profile2 objects, NOT profile2_type objects. So, we can use entity_load() instead:

        $profile_id = (int)str_replace('p2rp_register_', '', $delta);
        if ($profile_type = reset(entity_load('profile2_type', array($profile_id)))) {
          module_load_include('inc', 'profile2_regpath', 'registration_form');
          
          $block['subject'] = t('Register as @label', array('@label' => $profile_type->label));
          $block['content'] = profile2_regpath_build_block_form($profile_type);
        }

Pushed to latest dev.

Axagthoth’s picture

Status: Fixed » Needs work

Ok, so now $profile_type seems to be correctly set as this line works (the correct label is printed):

$block['subject'] = t('Register as @label', array('@label' => $profile_type->label));

However, from the following function:

function profile2_regpath_build_block_form($profile_type) {
  // We build the form with the 'block' argument so that profile2_regpath does not attach fields
  // via hook_form_alter().
  $form_state['profile_type_id'] = $profile_type->pid;    //This line fails
  $form_state['block_style'] = TRUE;
  $form = drupal_build_form('profile2_regpath_form_block_'.$profile_type->pid, $form_state);   //This line fails
  
  return $form;
}

The notices said something about an "undefined property" being called at those lines, so I looked at the DB tables created by profile2, and guess what? The property is called "id" not "pid". So if you rewrite the function like this:

function profile2_regpath_build_block_form($profile_type) {
  // We build the form with the 'block' argument so that profile2_regpath does not attach fields
  // via hook_form_alter().
  $form_state['profile_type_id'] = $profile_type->id;    //Works for me!
  $form_state['block_style'] = TRUE;
  $form = drupal_build_form('profile2_regpath_form_block_'.$profile_type->id, $form_state);   //Works for me!
  
  return $form;
}

Problem solved! Maybe you should push taht change to dev too.

Also, I hope I'm not being too much of a pain in the *** ;) couldn't live without your module!

grasmash’s picture

Status: Needs work » Fixed

Sorry, I forgot to mention the change from pid to id. I did make that change in the commit. Glad it's working for you! Good luck.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.