i use the autoassignrole module (and its path based system) to create different user registration forms for each role on my site. and also use profile_role module to restrict profile categories/fields according to role.

but there was no integration between the two. so i couldnt display all the allowed profile fields/categories for a certain role-signup form(provided by autoassignrole's path based role-signup system).

as it is now, it only displays the account(account information) fieldset on the signup form. but i wanted to display all the fieldsets/categories that are allowed for a given role-signup form.

hope that explanation didnt confuse anyone...

anyway here's what i did to achieve integration between autoassignrole and profile_role:

added the following code to the "profile_role_form_user_register_alter" function after line 77 in "profile_role.module"

if (module_exists('autoassignrole')){
   $path = drupal_get_path_alias($_GET['q']);
   $result = db_fetch_object(db_query("SELECT rid FROM {autoassignrole_page} WHERE path = '%s'", $path));
   $form_state['post']['roles'] = array($result->rid => $result->rid);	
}

didnt change/delete anything else in the module.

it works perfectly on my site now...

hope boombatower can check this code and add it to the module so we could all get future updates...

cheers!

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

boombatower’s picture

Assigned: Unassigned » boombatower
Category: task » feature
FileSize
1.33 KB

What did you use to get autoassignrole_page?

I wrote up a patch that work with the default interface that lets users select their role.

There doesn't appear to be any simple way get around the validation upon selecting of the role. Preferably autoassign_role could do it in two pages. The first select the role and the second redirecting.

Either way let me know what you think of this and then fill me in on the autoassignrole_page stuff.

dj-nitehawk’s picture

What did you use to get autoassignrole_page?

im using the latest dev version of autoassignrole... in the settings (admin/user/autoassignrole) there's now a section called "assign from path"

which basically lets me tie a role to a path alias... so in profile_role we can determine the role that is associated with the current reg form by looking at the path... which is what the code i submitted above does...

have a look on my site for a working example: http://nzbride.co.cc/user/register (its still in development so id appreciate it if people reading this (other than boombatower of course) wouldnt create any accounts on it)

I wrote up a patch that work with the default interface that lets users select their role. There doesn't appear to be any simple way get around the validation upon selecting of the role. Preferably autoassign_role could do it in two pages. The first select the role and the second redirecting. Either way let me know what you think of this and then fill me in on the autoassignrole_page stuff.

i tried the patch code and i see your validation dilemma... how about this:

when the selected value of the roles listbox changes, grab the selected rid, append it to the current url as a query item, do a form GET (page reload) with jquery. doing a GET wont fire the validation routines right?

and in the module code, check for the existence of a query item that has the rid to determine if its a role selection and take it from there...

should work right? i dont really know cos im still on my first drupal project and dont really fully understand the drupal FAPI :-)

bdornbush’s picture

When I applied dj-nitehawk's patch, I found that the categories and roles no longer showed up in the admin/user/profile/role screen. When I deleted the patch, then the categories and roles re-appeared.

I sure hope this can be made to work, as I could use it right now on a new site I have developed that is about to go-live.

boombatower’s picture

Does autoassign_role provide a function that returns the selected roles for a user reguardless of the method being used (ie. URL or user selected)? I could not find anything like that, if not I suggest we ask that it be created and then profile_role can use it and can support any methods that autoassign_role does.

#2: I like the idea of the jquery redirect and I'll give that a shot in a bit. (I'm a bit busy at the moment)

dj-nitehawk’s picture

When I applied dj-nitehawk's patch, I found that the categories and roles no longer showed up in the admin/user/profile/role screen. When I deleted the patch, then the categories and roles re-appeared.

hmmm.... weird... cos it works fine on my site...

was it my code or the patch in #1 post that made the roles disappear?

pls note that the code i submitted only works with autoassignrole's "assign from path" interface... and it doesnt do anything for the default "allow the user to choose" interface... and it wont break the default interface either...

we should wait for a working patch from boobatower for support for the default interface...

dj-nitehawk’s picture

Does autoassign_role provide a function that returns the selected roles for a user reguardless of the method being used (ie. URL or user selected)?

nope... i couldnt find one either... by the looks of it, autoassignrole is determining the method used by looking at the form id and the path... if the path is not /user/register, its an "assigned from path" user reg form...

ill look into this a bit more when i get some free time too...

in any case the form redirect idea should help a lot in the user register form alter... so lets get that sorted first i think...

bdornbush’s picture

re #5
I applied dj-nitehawk's code as shown in the base post.

bdornbush’s picture

RE #5 and #7
I tried it again. I made one change. I used the path option rather than the tab option. Now it all seems to work fine.

Thanks for solving a big problem for me.

I look forward to a more permanent patch solution.

mdayton’s picture

subscribing

cyberswat’s picture

Is there anything I need to do to AAR to help this issue?

boombatower’s picture

I think it a function that does, per #4: "Does autoassign_role provide a function that returns the selected roles for a user reguardless of the method being used (ie. URL or user selected)?" would be great.

And then I think the patch needs to be reworked a bit to provide a better user experience. I think we can reload the form using ahah or something so we don't get the validation errors.

cyberswat’s picture

I think this will do what you are looking for?

http://drupal.org/cvs?commit=224798

/**
 * An API like call to return the roles a user has available or will be assigned
 *
 * @param $op
 *  string all, auto, path, user
 * @param $path
 *  array Optional array of paths to restrict selection to when using an $op of path
 * @return
 *  array An array of roles
 */ 
function autoassignrole_get_roles($op = 'all', $path = array()) {
fabdel’s picture

Exactly what i search for !!!

Thanks.

I use the patch in the #1 post.

But before this patch, i had a common category of profile fields for my 2 roles who was visible, now i must select my 2 roles for using my common category.

Also, i prefer the onclick method to the onchange one.

Finally, is it possible not to submit this form when choosing the role because the message with the required field is displaying.
Hide and show the categories in AJAX ?

Manuel Garcia’s picture

+1 on this feature... bit reluctant to apply the change mentioned in #1 -- a solution for these modules to work together would be most desirable.

boombatower’s picture

Title: here's integration with autoassignrole module » Integrate with autoassignrole module
Status: Needs review » Postponed

Been rather busy, but finally got back to this. autoassignrole_get_roles() seems to have a different purpose then what profile_role needs and is broken in general. Filed issue at: #688934: autoassignrole_get_roles() is broken.

Postponed until I have some sort of API to integrate with.

cyberswat’s picture

#688934: autoassignrole_get_roles() is broken could use review if anyone is interested in helping boombatower

larowlan’s picture

FileSize
879 bytes

Hi
I needed different profile fields for different roles (this module) and different registration forms for different roles (different paths).
I found I could integrate profile_role and autoassignrole (path method - not tested with other methods) by adding the following at line 79 of profile_role.module

  if (module_exists('autoassignrole')) {
    if ($rid = autoassignrole_get_active_path_rid()) {
      $rolenames = user_roles();
      $roles[$rid] = $rolenames[$rid];
    }
  }

This just adds the auto role assigned with the current path to the $roles array that is taken from $form_state
Cheers, patch does the same

boombatower’s picture

That is exactly what I need...just autoassignrole_get_rid() and have autoassignrole figuere out the rest.

AntiNSA’s picture

Can someone add this to the next dev version soon? I am currently trying to figure out the best strategy to allow different registration modes for different roles and I so far am looking at autoassignrole and content profile..... I think I should add this one into the mix and apply the patch?

fnikola’s picture

I can verify that this patch works on Drupal 6.14, Auto Assign Role 6.x-1.1, and Profile role 6.x-1.3 when using auto assign role by path on registration. Thanks for the patch larowlan.

izmeez’s picture

subscribing

DanBerk4’s picture

subscribing

sjasmeetg’s picture

I am using auto-assignrole feature "Assign Role From Path" , Autoassign Version 6.x-1.x-dev Version.

I am using Profile Role module version 6.x-1.x-dev version.

I faced the same issue as by dj-nitehaw and applied the patch by dj-nitehawk which worked fine for me. Then I scrawled the page down, found the patch by larowlan. So, I deleted the piece of code advised by dj-nitehawk and copy-pasted the code from larowlan.

Surprisingly the patch by dj-nitehaw is working for me, the patch by larowlan is not working for me. To make things clear, I am using the AutoAssignRole module "Assign Role From Path" feature.

Please help me understanding why dj-nitehaw patch is working for me and if this patch can be checked-in?

I am not an expert PHP developer, so please accept my apologies in advance.

apt94jesse’s picture

#17 is quick and perfect. Thanks so much, this is exactly what I needed! I didn't try the other patch, fyi, so I can't confirm or deny, #17 just worked first so I kept it.

mittalpatel’s picture

Yeah, the patch in comment #17 is working very fine. Worked for me too...

massyboy’s picture

Category: feature » support
Priority: Normal » Major

very stratnge to put this at line 79 because in your code you let the variable/array $roles get some value,
but after your code comes a line:

$roles = isset($form_state['post']['roles']) ? $form_state['post']['roles'] : array();

...where $roles is altered again, so that your code becomes partially irrelevant...

Maybe it's best to exactly say just in between which code your snippet should come, because for any reason possible your line 79 may be different to my "line 79"...

So can you define "in between which existing code should your snippet come?"

massyboy’s picture

I'll reply my own request since I've experimented:

idd if you add the snippet on line #17 you have to comment the line directly after it:

// $roles = isset($form_state['post']['roles']) ? $form_state['post']['roles'] : array();

otherwise it's no use to alter the variable "$roles" in the snippet of #17 ofcourse.

Why didn't this occur to or make any trouble with you guys?
hmmm, that remains a strange inside mystery...

Ela’s picture

#17 works for me (as is...) Thank you for the patch!

j0e’s picture

kudos to the original poster for initiating;
using the patch at #17 and works well.
thanks Lee!

massyboy’s picture

...but you get a $roles as result...
and directly after it (without doing anything with the value of $role yet), you'd alter it by:
$roles = isset($form_state['post']['roles']) ? $form_state['post']['roles'] : array();
You see why I commented this line, so as not to overide the outcome of $role by the snippet of #17?
greetz,

ardnet’s picture

OK, just apply the patch from #17, it work perfectly to me too.
However I got another issue, how to hide the profile fields in the default registration form? (/user/register)

I set up my profile field like this:
- Role 1: Admin (/user/register/admin)
fields:
- field 1
- field 2
- field 3

- Role 2: Editor (/user/register/editor)
fields:
- field 4
- field 5
- field 6

But now, field 1 to 6 is all showing up in default user registration path (/user/register)
Anyone know how to hide these fields in the default registration form? (/user/register)

Thanks

Ela’s picture

@ardnet ..go to /admin/user/profile/role to set this up
Hope this helps :)
Ella

daggerhart’s picture

Patch in #17 works perfectly. Thank you. This should go into the next release.

gooddesignusa’s picture

If this patch http://drupal.org/node/716158#comment-5239290 ever gets rolled in the autoassignrole module can add this code to its module instead of patching profile_role. I added a hook to allow modules to pass a role to profile_roles to show certain profile fields. I needed this for some custom user add pages.

stewart.adam’s picture

Attached patch is based off of gooddesignusa's patch in #716158: create user, select role forces browser to submit page (comment #12) but modified a bit.

This patch makes the following corrections/changes:

  • hook_profile_role_registration_roles($type) was added to allow other modules to react and inject additional roles. $type is either 'admin' or 'user' to differentiate between the admin or site-wide registration forms, and hook implementations must return an associative array of $role[RID] => RID. This is to remain consistent with the return value of the 'role' form field in the admin registration form, which this module also uses (the hook return values are merged with it).
  • The check against $form['#action'] was removed as not only did it not function correctly if the user had a non-default base url (i.e. ~userdir/), but it meant that profile fields were not hidden if the anonymous user was unselected in the role settings.
  • In adjustment to the change above, hook_profile_role_registration_roles() is implemented to add role ID '1' (anonymous user) to $roles when the user form is being displayed (otherwise, $roles would be empty and all fields would be hidden).

This also allows you to very easily implement the hook and have different registration profiles:

function yourmodule_profile_role_registration_roles($type) {
  $roles = array();
  // do not edit the admin form, only the user form
  if ($type == 'user') {
    // get the arguments
    $item = menu_get_item();
    $role_names = $item['page_arguments'];
    array_shift($role_names); // (to remove the 'user_register' form callback argument)
    // get the corresponding rid for roles provided in the path
    $site_roles = user_roles();
    foreach ($role_names as $role_name) {
      $rid = array_search($role_name, $site_roles);
      if ($rid !== FALSE) {
        // if a match, add to $roles
        $roles[$rid] = $rid;
      }
    }
  }
  return $roles;
}

You can now use the URLs like /user/register/roleName (for a single role) or /user/register/roleName1/roleName2/roleName3 (for multiple roles). The role names from the URL are translated into role IDs by the hook implementation and you will get the appropriate registration profile fields displayed.

With a minimal form alter hook, you can have also have apply_for_role set its default value to the roles supplied in the URL, allowing you to completely customize the user roles upon registration with any combination of roles.

izmeez’s picture

The suggestion of a hook for other modules to use may be the future.

Meanwhile, thanks for the patch in #17. It only seems to work with autoassignrole paths, but helped solve problems using profile_role and have checkbox validation with checkbox_validate module work.

warmth’s picture

It will be great if both modules get upgraded to Drupal 7, then merged and committed to Drupal 8!