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