I'm struggling to come up with tidy little snippet for a computed field to interact with the user reference module.
The big picture:
- I use profiles for Department Faculty so that they can log in and manage their own info.
- In order to link content types I want to associate with a particular faculty person (say, a book section), I add the user reference field.
- I find it handy to sort by the faculty member's last name, so I want the computed field module to pull the value from the user-reference field and trim it.
I've succeeded with taxonomy terms, here's an example where we trim text from a term reference:
$entity_field[0]['value'] = preg_match('~([^\s]+)(?:,.*)?$~',taxonomy_term_title(taxonomy_term_load($entity->YOUR_CCK_FIELD['und'][0]['tid'])), $match);
$entity_field[0]['value'] = $match[0];
For my current issue, I need to call the UID, and then the name associated with it, but am at a loss with regards to the user reference correct function I should be going after.
If someone has dealt with this type of issue before, or could spare the time to look at it, it'd be greatly appreciated
Comments
The following should work for
The following should work for setting the value of the computed field if you're using a user reference field called "editor" on a node:
$account = user_load($entity->field_editor['und'][0]['uid']);
$names = explode(" ", $account->name);
$entity_field[0]['value'] = end($names);
I might suggest doing this on the user side of things; it makes more sense to associate a last name field with the user than with a node. Just add a computed field to the user:
$names = explode(" ", $entity->name);
$entity_field[0]['value'] = end($names);
You should then be able to use the user reference field on your node as a relationship in views, and sort by the last name field using this relationship. I can explain that more if you haven't used relationships; I think this is probably the easier, more logical and flexible way of solving the problem.
One warning: to populate the DB with computed field values I had to load/save each entity (at least with users, I didn't try nodes). You might need a small script to do that.
Perhaps longer-term solutions would be to separate out first/last names at the account creation stage. I haven't used it, but http://drupal.org/project/realname might be a solution.
Anybody else have some thoughts?
Alec
CEO @ www.thinktandem.io, the Lando guys