I am relatively new to drupal and for days I'm stuck.

I need to hide fields from the user profile page when they have a specific value. Adding a CSS class would also do it. But I prefer deleting the array element.

I try to use the hook_profile2_view_alter() function in my template php. Sure I can access the field options but not it's value directly.

Perhaps I'm digging in the wrong hole and there is a smooth "Drupal way" to do it.


You'd be better off putting your code in a custom module, as you can implement hook_profile2_view() instead. That will give you far more control over the output.

Without more specifics it's hard to be accurate but generally speaking the code would look something like this:

function MYMODULE_profile2_view($profile, $view_mode, $langcode) {
  $field_items = field_get_items('profile2', $profile, 'field_name_of_field');
  if ($field_items) {
    $value = $field_items[0]['value'];

    if ($value == 'something') {
      $profile->content['field_name_of_field']['#access'] = FALSE;
| improve this answer | |
  • thank you for the super quick reply! sadly field_get_items() throws me a EntityMalformedException: Missing bundle property on entity of type profile2 back. – marco l. Jan 9 '13 at 19:33
  • Sorry I just made a couple of changes to the answer (I made a mistake, you don't get the $profile object passed to the alter version of the hook) did you see those? – Clive Jan 9 '13 at 19:33
  • got it thank you very much. but field_get_items doesn't seem to return anything. right now I'm usually debugging with print_r. i will create a module and dig more into your solution. edit ok, field_get_items is a module only function. thanks again :) – marco l. Jan 9 '13 at 19:52

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.