I am trying to workout how to change the names in the Filter by author block to Real Names ( http://drupal.org/project/realname ).

I think it has something to do with the apachesolr_search_get_username function:

/**
 * Callback function for the 'Filter by name' facet block.
 */
function apachesolr_search_get_username($facet) {
  if ($facet == 0) {
    return variable_get('anonymous', t('Anonymous'));
  }
  else {
    return db_result(db_query("SELECT name FROM {users} WHERE uid = %d", $facet));
  }
}

But it may not as I am getting out of my depth!

Comments

pwolanin’s picture

Hmm, well this should probably be a theme function or call a theme function.

stopbox’s picture

Yes, if it uses the theme function, the Real Name module will automatically format the user name correctly (it already is in the Solr search results). I am just struggling to work out to change the code so it calls a theme function.

janusman’s picture

I guess the actual request is to have more hooks in the module.

How about having the callback function inside apachesolr_facet_block() [optionally?] be calls to theming functions? E.g. instead of calling locale_language_name() to get the locale name, it could try to call theme('locale_language_name')

Another would be renaming apachesolr_facet_block() to theme_apachesolr_facet_block()? This might be overkill though =) I guess theming functions are just the ones "that return HTML" and this module is returning an block array, of which only the "content" element is HTML.

JacobSingh’s picture

I don't have a good architectural idea here...

Perhaps we just make that particular block provide an option?

I'm not sure what we want to do here, we certainly don't want to call user_load on every facet.

-J

rjbrown99’s picture

I don't know about 1.x, but at least in 2.x you can do this at the theme layer. The module uses hook_theme for theme_apachesolr_breadcrumb_uid. Head on over to your template.php for your theme, and start with:

function MYTHEME_theme(&$existing, $type, $theme, $path) {
  return array(
    'apachesolr_breadcrumb_uid' => array(
      'arguments' => array('field' => NULL),
    ),
  );
}

Then you can take over the function and theme it however you like. I'm using content profiles with CCK fields for firstname and lastname.

function MYTHEME_apachesolr_breadcrumb_uid($field) {
  $uid = $field;
  if ($uid == 0) {
    return variable_get('anonymous', t('Anonymous'));
  }
  else {
    $profile = content_profile_load($type, $uid);
    $name = $profile->field_mycckfirstnamefield[0]['value'] . ' ' . $profile->field_myccklastnamefield[0]['value'];
    return($name);
  }
}

Works for me. You will need to adjust the content_profile_load call appropriately for $type to match the content type of your profiles.

jpmckinney’s picture

Status: Active » Closed (won't fix)

Seems to be the case in 1.x, too. Follow instructions in #5. Marking #704238: Real name to appear in author filter block duplicate.

rockwright’s picture

#5—I’m using core’s profile along with the RealName module. I don’t allow anonymous users to submit content so there won’t be any anonymous authors in the filter block. Can you please help by rewriting the snippet?

jpmckinney’s picture

#7 - the snippet will still work.

3dloco’s picture

Status: Closed (won't fix) » Active

#5 is not working for me...instead of getting the author's realname I am getting the realname of the user that is browsing the search results.

Below is the modification I did to make it work...+1 for adding this (if possible) ;)

apachesolr_search.module

/**
 * Return the username from $uid
 */
function theme_apachesolr_breadcrumb_uid($field) {
  if ($field['#value'] == 0) {
    return variable_get('anonymous', t('Anonymous'));
  }
  //else {
    //return db_result(db_query("SELECT name FROM {users} WHERE uid = %d", $field['#value']));
   if(module_exists('realname')) {
    	return db_result(db_query("SELECT realname FROM {realname} WHERE uid = %d", $field['#value']));
   }
    else {
    	return db_result(db_query("SELECT name FROM {users} WHERE uid = %d", $field['#value']));
   }
 }
pwolanin’s picture

Category: feature » support
geerlingguy’s picture

Version: 6.x-1.0-beta6 » 6.x-2.x-dev

I'm having trouble getting the names for anyone besides user 1 loaded... If I use either snippets, in #5 or #9, the first user listed (user 1) has that user's realname... but any other authors underneath are not listed. Also, in each search snippet, other users' usernames (not their real names) are listed as well.

Is that theme function used both for the 'Filter by author' block and the Search Results snippet metadata?

[Edit:] Nevermind, it looks like I had some content that was published by a blocked user... and Solr didn't reindex the content, so the uid was wrong... Here's the code I ended up using (I was going to do a user_load(), but found that realname_get_user() performs a bit better for our purposes:

/**
 * Implementation of hook_theme().
 */
function THEMENAME_theme(&$existing, $type, $theme, $path) {
  return array(
    // Apachesolr's username search facet
    'apachesolr_breadcrumb_uid' => array(
      'arguments' => array('field' => NULL),
    ),
  );
}

/**
 * Theme function to change name of ApacheSolr Search's user facet.
 */
function THEMENAME_apachesolr_breadcrumb_uid($field) {
  $uid = $field;
  if ($uid == 0) {
    return variable_get('anonymous', t('Anonymous'));
  }
  else {
    $user = realname_get_user($uid);
    $realname = $user->name;
    return($realname);
  }
}

Follow-up issue created in Realname's issue queue: #989908: Add ApacheSolr Search Facet uid's to Realname

imDhaval’s picture

+1

dave reid’s picture

Does the code in #11 work? It would have been nice to have that posted in #989908: Add ApacheSolr Search Facet uid's to Realname. :)

geerlingguy’s picture

Code in #11 seems to work well, and I've posted it into #989908: Add ApacheSolr Search Facet uid's to Realname as well.

pwolanin’s picture

Status: Active » Closed (fixed)

seems fixed