Different user attributes based on user's group?
Permalink 1 user found helpful
Is there a way to set up different user attributes based on the group that the user is in? I'm working through a situation here where there are a number of different types of users who are interacting with the system, however not all user attributes are relevant to all users. However I would still like to have different fields be required depending on the group the user is in.
Has anyone else tackled this before?
Thanks
Has anyone else tackled this before?
Thanks
Thanks for the response. I had thought about advanced permissions as well as that seems like it may get things going in the right direction. I don't think the forms block will help as these fields I'm wanting to affect would be associated with the user's profile, and not just a regular form submission.
I guess I could hook into the on_user_add event and do some validation there, however that seems like it could be a little clunky and not quite as flexible to maintain as it would be through an interface.
I guess I could hook into the on_user_add event and do some validation there, however that seems like it could be a little clunky and not quite as flexible to maintain as it would be through an interface.
I think I understand you would like different attributes visible on the actual attributes page depending on the users group not just to show a different form depending on the user.
It seems I fully misunderstood the question.
Would you want these to be accessible to the user via their profile page on on registration? (If at all or have I miss understood again?)
It seems I fully misunderstood the question.
Would you want these to be accessible to the user via their profile page on on registration? (If at all or have I miss understood again?)
Yes, that is correct. Let's say there are two user groups: teachers and students for this example. On the registration page I would like the user to be able to choose which group is applicable to them, and then show them the proper fields.
What I'm thinking it will come down to is creating an attribute set for each user group, and then setting up the user attributes there. Then, overriding the signup and profile pages to use a bit of logic to display the correct information. To take care of the required fields, I am thinking that running through the on_user_add event would be the most logical place to do that.
This would allow the system admin to still manage the attributes for each of the pre-defined groups through the UI. The only downside is that they won't be able to create new user groups on the fly in this setup (which isn't a requirement for this project so that's alright)
Thanks for the responses though. I definitely appreciate having another mind thinking about how to accomplish what I'm going for :)
What I'm thinking it will come down to is creating an attribute set for each user group, and then setting up the user attributes there. Then, overriding the signup and profile pages to use a bit of logic to display the correct information. To take care of the required fields, I am thinking that running through the on_user_add event would be the most logical place to do that.
This would allow the system admin to still manage the attributes for each of the pre-defined groups through the UI. The only downside is that they won't be able to create new user groups on the fly in this setup (which isn't a requirement for this project so that's alright)
Thanks for the responses though. I definitely appreciate having another mind thinking about how to accomplish what I'm going for :)
I'm about to tackle the exact same problem: Two different groups of users with a different set of attributes.
Has you solution worked well? Any hints you can offer from your experience working around this problem?
Has you solution worked well? Any hints you can offer from your experience working around this problem?
I am also running up against this problem and what I've done is follow these instructions:
- All attributes are optional (so I have no problems with validation)
- I have adjusted the controller (register), which I've adapted the validation. See my sample code below.
- I have adjusted the view (register), where I have the fields manually in the form instead. See my sample code below.
register.php (controller):
OLD:
NEW:
register.php (view):
OLD:
NEW (for example 1 field):
- All attributes are optional (so I have no problems with validation)
- I have adjusted the controller (register), which I've adapted the validation. See my sample code below.
- I have adjusted the view (register), where I have the fields manually in the form instead. See my sample code below.
register.php (controller):
OLD:
$aks = UserAttributeKey::getRegistrationList(); foreach($aks as $uak) { if ($uak->isAttributeKeyRequiredOnRegister()) { $e1 = $uak->validateAttributeForm(); if ($e1 == false) { $e->add(t('The field "%s" is required', $uak->getAttributeKeyName())); } else if ($e1 instanceof ValidationErrorHelper) { $e->add($e1); } } }
NEW:
private $attrs = "type_of_construction:required,type_of_home:required,year,living_area:required,household_size,project_status,energy_label_before,energy_label_after,electricity_before,electricity_after,gas_before,gas_after,water_before,water_after,address:required,publish_address,visit_house,info_for_visitors,image1:required,image2,image3,image4,image5"; foreach($attrs as $attr => $required){ $ak = CollectionAttributeKey::getByHandle($attr); if($required){ $e1 = $ak->validateAttributeForm(); if ($e1 == false) { $this->error->add(t('The field "%s" is required', $ak->getAttributeKeyName())); } else if ($e1 instanceof ValidationErrorHelper) { $this->error->add($e1); } } }
register.php (view):
OLD:
<?php $attribs = UserAttributeKey::getRegistrationList(); $af = Loader::helper('form/attribute'); foreach($attribs as $ak) { ?> <?php echo $af->display($ak, $ak->isAttributeKeyRequiredOnRegister()); ?> <?php }?>
NEW (for example 1 field):
<?php $ak = UserAttributeKey::getByHandle("salutations"); print $af->display($ak, $ak->isAttributeKeyRequiredOnProfile()); ?>
I'm also trying to do this same thing, did you find a useful solution in the end?
Hi they way I did it is place the users in a group ( /dashboard/users/groups/)
set your attributes which you created in sets.
( /dashboard/system/attributes/sets/category/2/)
in single_pages/profile/edit (copied from the concrete folder)
uncomment
then added
set your attributes which you created in sets.
( /dashboard/system/attributes/sets/category/2/)
in single_pages/profile/edit (copied from the concrete folder)
uncomment
/* $attribs = UserAttributeKey::getEditableInProfileList();
if(is_array($attribs) && count($attribs)) { */
then added
//%%%%%%%%%%%%%%%%%%%%% added bit %%%%%%%%%%%%%%/ $u = new User(); if($u->inGroup(Group::getByName('customers-group'))){ echo 'You are in: <strong>Customers</strong> group<BR /><BR />'; $set = AttributeSet::getByHandle('customers-attribute-set'); } elseif($u->inGroup(Group::getByName('members-group'))){ echo 'Members'; $set = AttributeSet::getByHandle('members-attribute-set'); } (don't need users groups if you just want to have attibute sets) if(is_object($set)){ $attribs = $set->getAttributeKeys(); } //%%%%%%%%%%%%%%%%%%%%% added bit end %%%%%%%%%%%%%%%/ foreach($attribs as $ak) {
Viewing 15 lines of 19 lines. View entire code block.
Thanks for the post, that did it for me :) (although I get an error when I have multiple address fields, for now I just recreated the address as text fields
Hi this was really useful for me on the edit user page, what did you use to show just the relevant attributes on the view.php page for the user profile?
Thanks
Thanks
I have a similar set up for a project that I am working on with several groups and different attributes associated to each those group. I have run into a problem with the edit profile page. I am trying to make one of the select attributes types editable by the user on the edit profile page.
Do you guys know how to use the form helper to display the forms for a select attribute?
I posted my question here but haven't had a response yet. Thanks!
http://www.concrete5.org/community/forums/customizing_c5/form-helpe...
Do you guys know how to use the form helper to display the forms for a select attribute?
I posted my question here but haven't had a response yet. Thanks!
http://www.concrete5.org/community/forums/customizing_c5/form-helpe...
On a more technical level you could check with the form blocks view.php
if the user resides in a group and then show relevant fields but you would need to know the id of the question(the community form could help with this) and then run either a switch or a if statement.
Certainly the easiest way would be to add however many forms you need and restrict the permissions for each form block.