Different user attributes based on user's group?
Permalink 1 user found helpfulHas anyone else tackled this before?
Thanks
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.
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?)
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 :)
Has you solution worked well? Any hints you can offer from your experience working around this problem?
- 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()); ?>
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) {
Thanks
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.