views_handler_field_contact_link.inc

  1. 3.x modules/contact/views_handler_field_contact_link.inc
  2. 2.x modules/contact/views_handler_field_contact_link.inc

File

modules/contact/views_handler_field_contact_link.inc
View source
  1. <?php
  2. /**
  3. * A field that links to the user contact page, if access is permitted.
  4. */
  5. class views_handler_field_contact_link extends views_handler_field_user_link {
  6. function option_definition() {
  7. $options = parent::option_definition();
  8. $options['link_display'] = array('default' => 'link', 'translatable' => FALSE);
  9. return $options;
  10. }
  11. function options_form(&$form, &$form_state) {
  12. parent::options_form($form, $form_state);
  13. $form['link_display'] = array(
  14. '#title' => t('Type of link'),
  15. '#default_value' => $this->options['link_display'],
  16. '#type' => 'select',
  17. '#options' => array(
  18. 'link' => t('Link'),
  19. 'icon' => t('Icon'),
  20. ),
  21. );
  22. $form['text']['#title'] = t('Link label');
  23. $form['text']['#required'] = TRUE;
  24. $form['text']['#default_value'] = empty($this->options['text']) ? t('contact') : $this->options['text'];
  25. }
  26. // An example of field level access control.
  27. // We must override the access method in the parent class, as that requires
  28. // the 'access user profiles' permission, which the contact form does not.
  29. function access() {
  30. global $user;
  31. // Only registered users can view other registered user's contact page.
  32. if (empty($user->uid)) {
  33. return FALSE;
  34. }
  35. return TRUE;
  36. }
  37. function render($values) {
  38. global $user;
  39. $uid = $values->{$this->aliases['uid']};
  40. if (empty($uid)) {
  41. return;
  42. }
  43. $account = user_load($uid);
  44. if (empty($account)) {
  45. return;
  46. }
  47. // Check access when we pull up the user account so we know
  48. // if the user has made the contact page available.
  49. $menu_item = menu_get_item("user/$uid/contact");
  50. if (!$menu_item['access'] || empty($account->contact)) {
  51. return;
  52. }
  53. if ($this->options['link_display'] == 'icon') {
  54. return l(theme('image', 'misc/forum-new.png'), 'user/'. $account->uid .'/contact', array('html' => TRUE, 'attributes' => array('title' => t('Contact %user', array('%user' => $account->name)))));
  55. }
  56. else {
  57. return l($this->options['text'], 'user/'. $account->uid .'/contact', array('attributes' => array('title' => t('Contact %user', array('%user' => $account->name)))));
  58. }
  59. }
  60. }