drupal csu-open atriumname

34
Drupal @ CSU atrium_username feature Emanuele Quinto Identity Management/Portal

Upload: emanuele-quinto

Post on 12-Dec-2014

1.036 views

Category:

Technology


0 download

DESCRIPTION

atrium_username is a Drupal feature for managing user name display:* a lightweight alternative to realname module;* "works" before theme layer* uses the node title of the user profile

TRANSCRIPT

Page 1: Drupal csu-open atriumname

Drupal @ CSUatrium_username feature

Emanuele QuintoIdentity Management/Portal

Page 2: Drupal csu-open atriumname

2

atrium_username

Page 3: Drupal csu-open atriumname

3

atrium_username

• a Drupal feature for managing user name display;

• a lightweight alternative to realname module;

• "works" before theme layer

• uses the node title of the user profile

function atrium_username_get_name($uid) { static $users = array(); if (!isset($users[$uid])) { // get data from db DIRECTLY $users[$uid] = db_result(db_query("SELECT title FROM {node} WHERE type='profile' AND uid=%d", $uid)); } return $users[$uid];}

It doesn't implements its own theme_username allowing another theme to override it.

Page 4: Drupal csu-open atriumname

4

Account Block

Page 5: Drupal csu-open atriumname

5

Account Block

Page 6: Drupal csu-open atriumname

6

Account Block

// Preprocess block. // Inject custom preprocessor before tao/ginkgo. $preprocess_function = $theme_registry['block']['preprocess functions']; $preprocess_theme = array_slice($preprocess_function, 2); array_unshift($preprocess_theme, 'atrium_username_preprocess_block'); array_splice($preprocess_function, 2, count($preprocess_function), $preprocess_theme);

$theme_registry['block']['preprocess functions'] = $preprocess_function; $theme_registry['block']['include files'][] = $atrium_username_path . '/atrium_username.theme.inc';

Preprocess injection

Insert atrium_username_preprocess_block before other preprocess

Page 7: Drupal csu-open atriumname

7

Account Block

function atrium_username_preprocess_block(&$variables) { // atrium-account if($variables['block']->bid == "atrium-account") { global $user; if ($user->uid) { $atrium_username = atrium_username_get_name($user->uid); $user_name = $atrium_username ? $atrium_username : check_plain($user->name); $variables['block']->subject = theme('user_picture', $user) . $user_name; } }}

Preprocess implementation

Don't use theme_username, it adds a link!

Page 8: Drupal csu-open atriumname

8

User Profile

Page 9: Drupal csu-open atriumname

9

User Profile

Page 10: Drupal csu-open atriumname

10

User Profile

function atrium_username_views_pre_render(&$view) {

// set title for profile_display (http://drupal.org/node/1176080) if($view->name == 'profile_display' && $view->current_display == 'page_1') { $uid = $view->args[0]; $atrium_username = atrium_username_get_name($uid); if(!empty($atrium_username)) { drupal_set_title($atrium_username); } }}

Hook views_pre_render

Page 11: Drupal csu-open atriumname

11

User Name Field

Page 12: Drupal csu-open atriumname

12

User Name Field

Page 13: Drupal csu-open atriumname

13

User Name Field

function render_link($data, $values) {

// get uid field name $field_uid = isset($this->aliases['uid']) ? $this->aliases['uid'] : 'uid';

if (!empty($this->options['link_to_user']) || !empty($this->options['overwrite_anonymous'])) { $account = new stdClass(); $account->uid = $values->{$field_uid}; if (!empty($this->options['overwrite_anonymous']) && !$account->uid) { // This is an anonymous user, and we're overriting the text. return check_plain($this->options['anonymous_text']); } elseif (!empty($this->options['link_to_user'])) { $account->name = $values->{$this->field_alias}; return theme('username', $account); } } // Otherwise, there's no special handling, so try to return atrium_username. if(isset($values->{$field_uid})) { $atrium_username = atrium_username_get_name($values->{$field_uid}); return $atrium_username ? $atrium_username : $data; } else { return $data; } }

render_link() method

Page 14: Drupal csu-open atriumname

14

Casetracker

Page 15: Drupal csu-open atriumname

15

Casetracker – issue page

Page 16: Drupal csu-open atriumname

16

Casetracker – issue page

case 'assign_to': $old->{$field} = atrium_username_get_name($old->{$field}) ? atrium_username_get_name($old->{$field}) : check_plain(casetracker_get_name($old->{$field})); $new->{$field} = atrium_username_get_name($new->{$field}) ? atrium_username_get_name($new->{$field}) : check_plain(casetracker_get_name($new->{$field})); break;

Function atrium_username_theme_casetracker_comment_changes

Unfortunately we have to rewrite everything as the lines to be changed are inside a switch/case with no other options.

Only the case 'assign_to' is modified.

Page 17: Drupal csu-open atriumname

17

Casetracker – assign to

Page 18: Drupal csu-open atriumname

18

Casetracker – issue page

if(!empty($form['casetracker']['assign_to']['#options'])) { $assign_to = $form['casetracker']['assign_to']['#options']; foreach($assign_to as $key => $value) { $uid = casetracker_get_uid($key); $atrium_username = atrium_username_get_name($uid); $name = $atrium_username ? $atrium_username : $value; $form['casetracker']['assign_to']['#options'][$key] = $name; } }

hook_form_alter

Radio options are set at theme level (theme_casetracker_case_form_common) but this is a form

(comment_form()) so we can hook_form_alter:

Page 19: Drupal csu-open atriumname

19

Casetracker – issue filter

Page 20: Drupal csu-open atriumname

20

Casetracker – issue filter

if($form_id=='views_exposed_form' && $form['#id']=='views-exposed-form-casetracker-cases-page-1') { foreach($form['assign_to']['#options'] as $uid => $value) { if(is_int($uid) && $uid>0) { $atrium_username = atrium_username_get_name($uid); if(!empty($atrium_username)) { $form['assign_to']['#options'][$uid] = $atrium_username . "~"; } } } }

hook_form_alter

The "Filter results" block is an exposed filter from the casetracker_cases view.

It's a form so we user hook_form_alter to modify user names

Page 21: Drupal csu-open atriumname

21

Casetracker – assigned column

Page 22: Drupal csu-open atriumname

22

Casetracker – assigned column

class atrium_username_casetracker_views_handler_field_user_name extends views_handler_field {

function render($values) { $uid = $values->{$this->field_alias}; if($uid==0) { return casetracker_get_name($uid); } else { $atrium_username = atrium_username_get_name($uid); return $atrium_username ? $atrium_username : casetracker_get_name($uid); } }

}

Override field handler

Override Casetracker Module implementation of views_handler_field (casetracker_views_handler_field_user_name):

Page 23: Drupal csu-open atriumname

23

Theming User Name

Page 24: Drupal csu-open atriumname

24

Theming User Name

Page 25: Drupal csu-open atriumname

25

Theming User Name

// Simulate a preprocess function for theme("username"). $_SESSION['theme_registry_username_function'] = $theme_registry['username']['function']; $theme_registry['username']['function'] = 'atrium_username_preprocess_username'; $theme_registry['username']['include files'] = array($atrium_username_path '/atrium_username.theme.inc');

Simulate preprocess

function atrium_username_preprocess_username($object) { $theme_username = $_SESSION['theme_registry_username_function'] ? $_SESSION['theme_registry_username_function'] : 'theme_username'; $user = drupal_clone($object); if($user->uid) { $atrium_username = atrium_username_get_name($user->uid); $user->name = $atrium_username ? $atrium_username : $user->name; } return $theme_username($user);}

Override theme but apply original theme (saved in $_SESSION)

Page 26: Drupal csu-open atriumname

26

Notifications

Page 27: Drupal csu-open atriumname

27

Notifications

Page 28: Drupal csu-open atriumname

28

Notifications

// Change notification_team form options if(!empty($form['notifications']['notifications_team']['options']['#value'])) { foreach($form['notifications']['notifications_team']['options']['#value'] as $uid => $name) { $atrium_username = atrium_username_get_name($uid); $name = $atrium_username ? $atrium_username : $name; $form['notifications']['notifications_team']['options']['#value'][$uid] = $name; } }

hook_form_alter

Page 29: Drupal csu-open atriumname

29

Autocomplete

Page 30: Drupal csu-open atriumname

30

Notifications

Page 31: Drupal csu-open atriumname

31

Autocomplete

function atrium_username_menu_alter(&$callbacks) { $callbacks['members/add/autocomplete']['page callback'] = 'atrium_username_autocomplete'; $callbacks['members/add/ajax']['page callback'] = 'atrium_username_addform_ajax';}

hook_menu_alter

function atrium_username_autocomplete($string = '') { $matches = array(); if ($string) { $query = "SELECT u.uid, u.name, n.title FROM {users} u LEFT JOIN {node} n ON (n.uid=u.uid AND n.type='profile') WHERE LOWER(n.title) LIKE LOWER('%s%%') OR LOWER(u.name) LIKE LOWER('%s%%')"; $query = db_rewrite_sql($query, 'users', 'uid', array($string, $string));

$result = db_query_range($query, array($string, $string), 0, 10); while ($user = db_fetch_object($result)) { $user_name = $user->title ? check_plain($user->title) : check_plain($user->name); $matches[$user_name] = $user_name; } } drupal_json($matches);}

callback

Page 32: Drupal csu-open atriumname

32

Files

Page 33: Drupal csu-open atriumname

33

Thank you

Q & A

Page 34: Drupal csu-open atriumname

34

About

Emanuele Quinto [email protected]

http://www.linkedin.com/in/emanuelequinto

@emaV

atrium_usernamehttp://drupal.org/project/atrium_username

https://github.com/emaV/atrium_username