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

Definition of views_handler_field_contact_link.

File

modules/contact/views_handler_field_contact_link.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Definition of views_handler_field_contact_link.
  5. */
  6. /**
  7. * A field that links to the user contact page, if access is permitted.
  8. *
  9. * @ingroup views_field_handlers
  10. */
  11. class views_handler_field_contact_link extends views_handler_field_user_link {
  12. function options_form(&$form, &$form_state) {
  13. $form['text']['#title'] = t('Link label');
  14. $form['text']['#required'] = TRUE;
  15. $form['text']['#default_value'] = empty($this->options['text']) ? t('contact') : $this->options['text'];
  16. parent::options_form($form, $form_state);
  17. }
  18. // An example of field level access control.
  19. // We must override the access method in the parent class, as that requires
  20. // the 'access user profiles' permission, which the contact form does not.
  21. function access() {
  22. return user_access('access user contact forms');
  23. }
  24. function render_link($data, $values) {
  25. global $user;
  26. $uid = $this->get_value($values, 'uid');
  27. if (empty($uid)) {
  28. return;
  29. }
  30. $account = user_load($uid);
  31. if (empty($account)) {
  32. return;
  33. }
  34. // Check access when we pull up the user account so we know
  35. // if the user has made the contact page available.
  36. $menu_item = menu_get_item("user/$uid/contact");
  37. if (!$menu_item['access'] || empty($account->data['contact'])) {
  38. return;
  39. }
  40. $this->options['alter']['make_link'] = TRUE;
  41. $this->options['alter']['path'] = 'user/' . $account->uid . '/contact';
  42. $this->options['alter']['attributes'] = array('title' => t('Contact %user', array('%user' => $account->name)));
  43. $text = $this->options['text'];
  44. return $text;
  45. }
  46. }