-1

I would like to show a default value if the user doesn't fill in a field in the user profile.

For example:

I have a Hair type field. If the user fills it in, i would like the following output:

Hair type: long.

It is already working. If the user doesn't fill in the field, i would like the following output:

Hair type: Not specified.

I use Display Suite to display the user profile. I don't want to set a default value to the field, because i just want to show the Not specified value on the user profile page.

Which contrib modules or hooks can i use to solve this problem?

  • 2
    Why don't you set 'Not specified' as a Default value for 'Hair type' field? – Krishna Mohan Aug 27 '14 at 13:39
1

You can use hook_node_view_alter to alter the build array. This is a render array so this should be an easy way to update the data.

1

To add default value to User Profile field. Go to admin/config/people/account settings/manage fields and edit that particular field. When you on the edit form of that field, there is a field to set Default Value. You can add any default value that you want in that field.

0

Sorry, this sentence doesn't make sense to me: "I use Display Suite to display the user profile. I don't want to set a default value to the field, because i just want to show the Not specified value on the user profile page."

I think Krishna Mohan answered your question.

  • He is using Display Suite to render the display and he just wants to show a default value when the field is empty. He does not want to set a real default value that is saved to the databasse. – J. Reynolds Aug 28 '14 at 3:56
  • Thanks for the explanation. I guess I'm just not understanding why he doesn't want to use a real default value. – user3166999 Aug 28 '14 at 18:07
0

Here is a method that does it with Display Suite.

Install modules: Entity, Display Suite UI, Display Suite Format

Go to Manage Display page for 'Account' where you already have Display Suite active.

Create a Custom Field of type 'code field'. Select 'User' in Entities section, and in Field code box make sure Display Suite Code is the text format, and add this:

<?php 
$entity_user = entity_metadata_wrapper('user', $entity);
$set = FALSE;
if (!$entity_user->field_hair_type->value()) {
  $entity_user->field_hair_type->set('Not specified');
  $set= TRUE;       
}
print render(field_view_field(
  'user', $entity, 'field_hair_type')
);
// set it back to empty, just in case someone calls save on it which is highly unlikely
if ($set) $entity_user->field_hair_type->set(''); 
?>

Your field name will be different so just sort that out.

And lastly just drag your field into the layout area of display suite where you want it displayed. Leave the original field to not display.

0

You can easily do this using Rules module. In user accounts I create a field and I called it field_hair_type of type text.The Event, condition and action are as follow:

Event

The Event must be After updating an existing user account

enter image description here

Condition

The condition must be Data value is empty. Now using Data Selector select the hair type field, as follow

enter image description here

Action

The action must be Set a data value, again same as condition section select the hair type field in Data Selector and after saving and set the value to Not Specified. as follow

enter image description here

Now it is done

  • Too complicated for what can be achieved by making field required and setting good default value. – Mołot Aug 29 '15 at 22:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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