2

With profile2 all the profile page titles is the profile type.

How do I override it with a field from profile2?

I was wondering something like this

hook_preprocess_page(&$variables){
if ($page-type == profile2) {
$new_title = field_get_items('profile2', $profile, 'field_new_title');

  drupal_set_title($new_title);
}
}

Of course this script does not work.

This needs to happen just on the profile page, not in the profile listing page.

3 Answers 3

2

To change the content title (defaults to the username) you can use this snippet in a custom module:

<?php
function MYMODULE_custom_menu_alter(&$items) {
 $items['user/%user']['title callback'] = MYMODULE_user_page_title;
}

function MYMODULE_user_page_title() {

if(arg(0) == 'user') {
// Load uid from url
$user = user_load(arg(1));
// Load profile2 entity
$profile2 = profile2_load_by_user($user->uid);
// Load fields
$myfield = field_get_items('profile2', $profile2['main'], 'field_myfield');
$output = $myfield[0]['safe_value'];
}

// Fallback to username if no fields are present
if(empty($output)) {
   $output = $user->name;
 }

return $output;

}
?>

Reference : http://drupal.org/node/1262070, Change page title on user-profile using a field value from Profile2

5
  • Ok I will try, but I have to work with the profile2 page, not with the user page Aug 13, 2012 at 12:35
  • If i want to call something from profile2 to user-page it's fine... the opposite is quite tedious. I think I'm going to use user-profile.tpl.php Aug 13, 2012 at 13:13
  • Are you sure this works properly? have you tested it? Aug 14, 2012 at 9:57
  • no. I didn't tested it. Just got that information from the reference links which i have given
    – Cindrella
    Aug 14, 2012 at 10:01
  • I'm just stuck to understand if the script is wrong or the profile type. How do I get the profile type from devel? Should I use the machine name? Aug 14, 2012 at 10:03
1

Issue can be addressed using Rules:

{ "rules_profile2_page_title_change" : {
    "LABEL" : "Profile2 Page Title Change",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "TAGS" : [ "User" ],
    "REQUIRES" : [ "rules", "profile2" ],
    "ON" : { "profile2_view" : [] },
    "DO" : [
      { "data_set" : {
          "data" : [ "profile2:user:profile-main:label" ],
          "value" : "[profile2:user]"
        }
      }
    ]
  }
}
  • Note: change "data_set" > "data" to the name of your profile type

further details

1

You can use HOOK_page_alter(&$page)

function HOOK_page_alter(&$page) {

  if (isset($page['content']['system_main']['profile2']) &&  is_array(($page['content']['system_main']['profile2']))) {
  /* To make sure you're in the profile page */

  $pid = array_keys($page['content']['system_main']['profile2']);
  if (count($pid) === 1) {
     $pid = reset($pid);
      if (isset($page['content']['system_main']['profile2'][$pid]['field_name']['#items'][0]['value'])) {
        /*To get Profile Name */
        $title = drupal_get_title();

        $profile_name = $page['content']['system_main']['profile2'][$pid]['field_name']['#items'][0]['value'];

         drupal_set_title($title . ' ' . $profile_name);
       }
     }
   }        
 }

So the output of this function is : Profile Artist ArtisName

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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