4

How can I make a user choose male or female and keep a default image on a specific content?

I mean I want two defaults pictures (for male and female), but only for default image in case user want to upload a different image this should be accept as normal.

And all this for one specific type of content.

I want this, just because this field must captures a photo but for the ones who don't want to upload a photo show a default profile image.

2 Answers 2

2

You can do this on the display level via

  1. hook_preprocess_field() (thx Clive in the comments, give him a thumbs up!), OR
  2. a custom formatter, OR
  3. a custom Display suite field

hook_preprocess_field() on the image field is the easiest.

You have the full user entity available and can use information from other fields to alter the image.

The hook is fired every time theme('field', ..) is fired, so you need to figure out if this is the correct field.

EDIT: Instead of a detailed tutorial, here is what I usually do:

  • Install devel
  • Create your custom module with an implementation of hook_preprocess_field(&$variables).
  • Do dpm($variables) inside that hook implementation to show the function argument (sometimes you need to refresh the page twice to see the result).
  • Manipulate the part of $variables that you are interested in.

With a custom formatter, you can define your own way to display e.g. the image field, while having the data from other node fields available.

Then on the user display configuration, you can choose this formatter to display the image.

With the custom DS field, you can introduce a "field" - that is, a positionable element on the entity display. This field can use data from multiple data fields on the node entity. E.g. you first check if there is an explicitly uploaded image, and if not, you show an image depending on the gender (male, female, transgender / don't say).

You can also define a form to configure display options. E.g. you could make the image style configurable.

Steps:

  • Enable Display suite
  • Implement hook_ds_fields_info() in a custom module, using e.g. DS_FIELD_TYPE_FUNCTION.
  • Provide the function to generate the html or render array (some examples)
  • Configure your user to display with a Display suite layout.
  • Enable your custom field.

There are more details to this. But I the main purpose of this answer is to point you in a direction.


The weakness of all these options is that you have to hardcode the field names, or at least one of them. If you want to reuse this on another site with other field names, you have to change the field names in the code.

8
  • 3
    A 3rd option would be to implement hook_preprocess_field() to alter the image, you also have the full entity available there. Not as reusable as either of the methods you've mentioned, but it's less code than the a custom formatter and means you don't need to rely on having DS installed
    – Clive
    Nov 13, 2013 at 17:46
  • @Clive: Ok if i add that to the answer?
    – donquixote
    Nov 13, 2013 at 18:12
  • Of course, please do :)
    – Clive
    Nov 13, 2013 at 18:19
  • Actually I've been working on a solution for this kind of stuff where you don't have to hardcode the field names :) But this remains in my own sites until I have time to produce something publishable.
    – donquixote
    Nov 13, 2013 at 18:29
  • Thanks for all I'll try to check those... any guide or tutorial (I`m kind of new on Drupal) please. And thanks for all :D
    – user23662
    Nov 13, 2013 at 18:44
1

Maybe you'ld like to add a taxonomy dropdown list to your user accounts /admin/config/people/accounts/fields and set a default user picture - corresponding to what's chosen there && unless no user picture is uploaded already - in a custom page–[node-type].tpl.php.

More informations would be helpful to find out what you trying to do. Where should users choose their gender? Where you need the picture to be displayed exactly?

And last but not least: Not everyone counts itself as female or male. There are transgender people. And also people who don't like others to know their gender.

2
  • 1
    That last paragraph isn't strictly true - some jurisdictions only recognise male and female in response to a person's sex. Either way, I'd go with a plain select field rather than a whole vocabulary for this - the options aren't likely to change very often :)
    – Clive
    Nov 13, 2013 at 16:44
  • Well, I want one specific type of content that has one field which captures the option selected by the user (in case choose male a male profile, for female a female profile... depends on user). I want this, just because this field must captures a photo but for the ones who don't want to upload a photo show a default profile image.
    – user23662
    Nov 13, 2013 at 16:55

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.