0

I'm using this function to replace the titles on user pages with a user text field:

function MYMODULE_user_view($account, $view_mode, $langcode) {
  $wrapper = entity_metadata_wrapper('user', $account);
  $auth_name = $wrapper->field_account_auth_name->value();
  if ($auth_name) {
    drupal_set_title('Profile overview for ' . $auth_name;
  } else {
    drupal_set_title('Profile overview for ' . $account->name);
  }
}

It works as it should, but when I add a view block with user entities it makes every page have the same title. It's Profile overview for ' . $auth_name; for the first user with a non-empty field_account_auth_name. If I delete that user the next user takes its place. This is for all pages too, not just ones with the block in it.

If it matters I'm using this function

  function MYMODULE_preprocess_user_profile(&$vars) {
    if ($vars['elements']['#view_mode'] == 'auth') {
      $vars['theme_hook_suggestions'][] = 'user_profile__auth';
    }
  }

with a user entity "auth" I created using Entity View Mode, with this in user-profile--auth.tpl:

<div class="authProfile"<?php print $attributes; ?>>
  <?php print render($user_profile); ?>  
</div>

and setting auth as the view mode in the view block. But the title problem happens with the user account view mode as well.

0

You could check the url using the arg function and only set the title if the current page is a user/* page. You could try something like the following.

function MYMODULE_user_view($account, $view_mode, $langcode) {
  if (arg(0) == 'user') {
    $wrapper = entity_metadata_wrapper('user', $account);
    $auth_name = $wrapper->field_account_auth_name->value();
    if ($auth_name) {
      drupal_set_title('Profile overview for ' . $auth_name;
    } else {
      drupal_set_title('Profile overview for ' . $account->name);
    }
  }
}
| improve this answer | |
  • Are you using arg(0) as a placeholder? I don't know php very well. I know of current_path() but AFAIK it only allows for specific URLs, not path/1, path/2, etc. – delz Sep 6 '15 at 5:06
  • Nevermind I messed up. It's no longer on all pages, but it's still just using the first user with a non-empty field_account_auth_name on all user pages. I tried if (empty($auth_name)) . . . $account->name) else . . . $auth_name but it's the same. Any ideas? – delz Sep 6 '15 at 5:36
  • arg is a Drupal function that returns the ith part of the current path. So arg(0) returns the first part of the path (everything after the domain and before the first slash). See api.drupal.org/api/drupal/includes%21bootstrap.inc/function/arg/… – Thomas4019 Sep 7 '15 at 13:10
  • I'm not sure what the problem is. Are you having problems on pages with multiple users? I imagine that on those pages it would be the last user's name that gets rendered. – Thomas4019 Sep 7 '15 at 13:21
  • Okay gotcha. No it's on single user pages, just the regular ?q=user/*. If the view block is in a region then every user page's title is "Profile overview for x's field_account_auth_name", x being the oldest user if I set the sort criteria to User: Created date (desc), and newest if I set it to asc. I'm pretty confused. – delz Sep 7 '15 at 15:13

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.