Projet

Général

Profil

Paste
Télécharger (7,97 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / colorbox / views / colorbox_handler_field_colorbox.inc @ 62f5c69e

1
<?php
2

    
3
/**
4
 * @file
5
 * Views handlers for Colorbox module.
6
 */
7

    
8
/**
9
 * A handler to provide a field that is completely custom by the administrator.
10
 *
11
 * @ingroup views_field_handlers
12
 */
13
class colorbox_handler_field_colorbox extends views_handler_field {
14
  function query() {
15
    // Do nothing, as this handler does not need to do anything to the query itself.
16
  }
17

    
18
  function option_definition() {
19
    $options = parent::option_definition();
20

    
21
    $options['trigger_field'] = array('default' => '');
22
    $options['popup'] = array('default' => '');
23
    $options['caption'] = array('default' => '');
24
    $options['gid'] = array('default' => TRUE);
25
    $options['custom_gid'] = array('default' => '');
26
    $options['width'] = array('default' => '600px');
27
    $options['height'] = array('default' => '400px');
28

    
29
    return $options;
30
  }
31

    
32
  function options_form(&$form, &$form_state) {
33
    parent::options_form($form, $form_state);
34

    
35
    // Get a list of the available fields and arguments for trigger field and token replacement.
36
    $options = array();
37
    $fields = array('trigger_field' => t('- None -'));
38
    foreach ($this->view->display_handler->get_handlers('field') as $field => $handler) {
39
      $options[t('Fields')]["[$field]"] = $handler->ui_name();
40
      // We only use fields up to (and including) this one.
41
      if ($field == $this->options['id']) {
42
        break;
43
      }
44

    
45
      $fields[$field] = $handler->definition['title'];
46
    }
47
    $count = 0; // This lets us prepare the key as we want it printed.
48
    foreach ($this->view->display_handler->get_handlers('argument') as $arg => $handler) {
49
      $options[t('Arguments')]['%' . ++$count] = t('@argument title', array('@argument' => $handler->ui_name()));
50
      $options[t('Arguments')]['!' . $count] = t('@argument input', array('@argument' => $handler->ui_name()));
51
    }
52

    
53
    $this->document_self_tokens($options[t('Fields')]);
54

    
55
    // Default text.
56
    $patterns = t('<p>You must add some additional fields to this display before using this field. These fields may be marked as <em>Exclude from display</em> if you prefer. Note that due to rendering order, you cannot use fields that come after this field; if you need a field not listed here, rearrange your fields.</p>');
57
    // We have some options, so make a list.
58
    if (!empty($options)) {
59
      $patterns = t('<p>The following tokens are available for this field. Note that due to rendering order, you cannot use fields that come after this field; if you need a field not listed here, rearrange your fields.
60
If you would like to have the characters %5B and %5D please use the html entity codes \'%5B\' or  \'%5D\' or they will get replaced with empty space.</p>');
61
      foreach (array_keys($options) as $type) {
62
        if (!empty($options[$type])) {
63
          $items = array();
64
          foreach ($options[$type] as $key => $value) {
65
            $items[] = $key . ' == ' . $value;
66
          }
67
          $patterns .= theme('item_list',
68
            array(
69
              'items' => $items,
70
              'type' => $type
71
            ));
72
        }
73
      }
74
    }
75

    
76
    $form['trigger_field'] = array(
77
      '#type' => 'select',
78
      '#title' => t('Trigger field'),
79
      '#description' => t('Select the field that should be turned into the trigger for the Colorbox.  Only fields that appear before this one in the field list may be used.'),
80
      '#options' => $fields,
81
      '#default_value' => $this->options['trigger_field'],
82
      '#weight' => -12,
83
    );
84

    
85
    $form['popup'] = array(
86
      '#type' => 'textarea',
87
      '#title' => t('Popup'),
88
      '#description' => t('The Colorbox popup content. You may include HTML. You may enter data from this view as per the "Replacement patterns" below.'),
89
      '#default_value' => $this->options['popup'],
90
      '#weight' => -11,
91
    );
92

    
93
    $form['caption'] = array(
94
      '#type' => 'textfield',
95
      '#title' => t('Caption'),
96
      '#description' => t('The Colorbox Caption. You may include HTML. You may enter data from this view as per the "Replacement patterns" below.'),
97
      '#default_value' => $this->options['caption'],
98
      '#weight' => -10,
99
    );
100

    
101
    $form['gid'] = array(
102
      '#type' => 'checkbox',
103
      '#title' => t('Automatic generated Colorbox gallery'),
104
      '#description' => t('Enable Colorbox gallery using a generated gallery id for this view.'),
105
      '#default_value' => $this->options['gid'],
106
      '#weight' => -9,
107
    );
108

    
109
    $form['custom_gid'] = array(
110
      '#type' => 'textfield',
111
      '#title' => t('Custom Colorbox gallery'),
112
      '#description' => t('Enable Colorbox gallery with a given string as gallery. Overrides the automatically generated gallery id above. You may enter data from this view as per the "Replacement patterns" below.'),
113
      '#default_value' => $this->options['custom_gid'],
114
      '#weight' => -8,
115
    );
116

    
117
    $form['width'] = array(
118
      '#type' => 'textfield',
119
      '#title' => t('Width'),
120
      '#description' => t('Specify the width of the Colorbox popup window. Because the content is dynamic, we cannot detect this value automatically. Example: "100%", 500, "500px".'),
121
      '#default_value' => $this->options['width'],
122
      '#weight' => -6,
123
    );
124

    
125
    $form['height'] = array(
126
      '#type' => 'textfield',
127
      '#title' => t('Height'),
128
      '#description' => t('Specify the height of the Colorbox popup window. Because the content is dynamic, we cannot detect this value automatically. Example: "100%", 500, "500px".'),
129
      '#default_value' => $this->options['height'],
130
      '#weight' => -7,
131
    );
132

    
133
    $form['patterns'] = array(
134
      '#type' => 'fieldset',
135
      '#title' => t('Replacement patterns'),
136
      '#collapsible' => TRUE,
137
      '#collapsed' => TRUE,
138
      '#value' => $patterns,
139
    );
140
  }
141

    
142
  /**
143
   * Render the trigger field and its linked popup information.
144
   */
145
  function render($values) {
146
    // Load the necessary js file for Colorbox activation.
147
    if (_colorbox_active() && !variable_get('colorbox_inline', 0)) {
148
      drupal_add_js(drupal_get_path('module', 'colorbox') . '/js/colorbox_inline.js');
149
    }
150

    
151
    // We need to have multiple unique IDs, one for each record.
152
    static $i = 0;
153
    $i = mt_rand();
154

    
155
    // Return nothing if no trigger filed is selected.
156
    if (empty($this->options['trigger_field'])) {
157
      return;
158
    }
159

    
160
    // Get the token information and generate the value for the popup and the
161
    // caption.
162
    $tokens = $this->get_render_tokens($this->options['alter']);
163
    $popup = filter_xss_admin($this->options['popup']);
164
    $caption = filter_xss_admin($this->options['caption']);
165
    $gallery = filter_xss_admin($this->options['custom_gid']);
166
    $popup = strtr($popup, $tokens);
167
    $caption = strtr($caption, $tokens);
168
    $gallery = drupal_html_class(strtr($gallery, $tokens));
169

    
170
    // Return nothing if popup is empty.
171
    if (empty($popup)) {
172
      return;
173
    }
174

    
175
    $width = $this->options['width'] ? $this->options['width'] : '';
176
    $height = $this->options['height'] ? $this->options['height'] : '';
177
    $gallery_id = !empty($gallery) ? $gallery : ($this->options['gid'] ? 'gallery-' . $this->view->name : '');
178
    $link_text = $tokens["[{$this->options['trigger_field']}]"];
179
    $link_options = array(
180
      'html' => TRUE,
181
      'fragment' => 'colorbox-inline-' . $i,
182
      'query' => array(
183
        'width' => $width,
184
        'height' => $height,
185
        'title' => $caption,
186
        'inline' => 'true'
187
      ),
188
      'attributes' => array(
189
        'class' => array('colorbox-inline'),
190
        'rel' => $gallery_id
191
      )
192
    );
193
    // Remove any parameters that aren't set.
194
    $link_options['query'] = array_filter($link_options['query']);
195

    
196
    // If the nid is present make the link degrade to the node page if
197
    // JavaScript is off.
198
    $link_target = isset($values->nid) ? 'node/' . $values->nid : '';
199
    $link_tag = l($link_text, $link_target, $link_options);
200

    
201
    // The outside div is there to hide all of the divs because if the specific Colorbox
202
    // div is hidden it won't show up as a Colorbox.
203
    return $link_tag . '<div style="display: none;"><div id="colorbox-inline-' . $i . '">' . $popup . '</div></div>';
204
  }
205
}