colorbox_handler_field_colorbox.inc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. /**
  3. * @file
  4. * Views handlers for Colorbox module.
  5. */
  6. /**
  7. * A handler to provide a field that is completely custom by the administrator.
  8. *
  9. * @ingroup views_field_handlers
  10. */
  11. class colorbox_handler_field_colorbox extends views_handler_field {
  12. function query() {
  13. // Do nothing, as this handler does not need to do anything to the query itself.
  14. }
  15. function option_definition() {
  16. $options = parent::option_definition();
  17. $options['trigger_field'] = array('default' => '');
  18. $options['popup'] = array('default' => '');
  19. $options['caption'] = array('default' => '');
  20. $options['gid'] = array('default' => TRUE);
  21. $options['custom_gid'] = array('default' => '');
  22. $options['width'] = array('default' => '600px');
  23. $options['height'] = array('default' => '400px');
  24. return $options;
  25. }
  26. function options_form(&$form, &$form_state) {
  27. parent::options_form($form, $form_state);
  28. // Get a list of the available fields and arguments for trigger field and token replacement.
  29. $options = array();
  30. $fields = array('trigger_field' => t('- None -'));
  31. foreach ($this->view->display_handler->get_handlers('field') as $field => $handler) {
  32. $options[t('Fields')]["[$field]"] = $handler->ui_name();
  33. // We only use fields up to (and including) this one.
  34. if ($field == $this->options['id']) {
  35. break;
  36. }
  37. $fields[$field] = $handler->definition['title'];
  38. }
  39. $count = 0; // This lets us prepare the key as we want it printed.
  40. foreach ($this->view->display_handler->get_handlers('argument') as $arg => $handler) {
  41. $options[t('Arguments')]['%' . ++$count] = t('@argument title', array('@argument' => $handler->ui_name()));
  42. $options[t('Arguments')]['!' . $count] = t('@argument input', array('@argument' => $handler->ui_name()));
  43. }
  44. $this->document_self_tokens($options[t('Fields')]);
  45. // Default text.
  46. $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>');
  47. // We have some options, so make a list.
  48. if (!empty($options)) {
  49. $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.
  50. 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>');
  51. foreach (array_keys($options) as $type) {
  52. if (!empty($options[$type])) {
  53. $items = array();
  54. foreach ($options[$type] as $key => $value) {
  55. $items[] = $key . ' == ' . $value;
  56. }
  57. $patterns .= theme('item_list',
  58. array(
  59. 'items' => $items,
  60. 'type' => $type
  61. ));
  62. }
  63. }
  64. }
  65. $form['trigger_field'] = array(
  66. '#type' => 'select',
  67. '#title' => t('Trigger field'),
  68. '#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.'),
  69. '#options' => $fields,
  70. '#default_value' => $this->options['trigger_field'],
  71. '#weight' => -12,
  72. );
  73. $form['popup'] = array(
  74. '#type' => 'textarea',
  75. '#title' => t('Popup'),
  76. '#description' => t('The Colorbox popup content. You may include HTML. You may enter data from this view as per the "Replacement patterns" below.'),
  77. '#default_value' => $this->options['popup'],
  78. '#weight' => -11,
  79. );
  80. $form['caption'] = array(
  81. '#type' => 'textfield',
  82. '#title' => t('Caption'),
  83. '#description' => t('The Colorbox Caption. You may include HTML. You may enter data from this view as per the "Replacement patterns" below.'),
  84. '#default_value' => $this->options['caption'],
  85. '#weight' => -10,
  86. );
  87. $form['gid'] = array(
  88. '#type' => 'checkbox',
  89. '#title' => t('Automatic generated Colorbox gallery'),
  90. '#description' => t('Enable Colorbox gallery using a generated gallery id for this view.'),
  91. '#default_value' => $this->options['gid'],
  92. '#weight' => -9,
  93. );
  94. $form['custom_gid'] = array(
  95. '#type' => 'textfield',
  96. '#title' => t('Custom Colorbox gallery'),
  97. '#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.'),
  98. '#default_value' => $this->options['custom_gid'],
  99. '#weight' => -8,
  100. );
  101. $form['width'] = array(
  102. '#type' => 'textfield',
  103. '#title' => t('Width'),
  104. '#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".'),
  105. '#default_value' => $this->options['width'],
  106. '#weight' => -6,
  107. );
  108. $form['height'] = array(
  109. '#type' => 'textfield',
  110. '#title' => t('Height'),
  111. '#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".'),
  112. '#default_value' => $this->options['height'],
  113. '#weight' => -7,
  114. );
  115. $form['patterns'] = array(
  116. '#type' => 'fieldset',
  117. '#title' => t('Replacement patterns'),
  118. '#collapsible' => TRUE,
  119. '#collapsed' => TRUE,
  120. '#value' => $patterns,
  121. );
  122. }
  123. /**
  124. * Render the trigger field and its linked popup information.
  125. */
  126. function render($values) {
  127. // Load the necessary js file for Colorbox activation.
  128. if (_colorbox_active() && !variable_get('colorbox_inline', 0)) {
  129. drupal_add_js(drupal_get_path('module', 'colorbox') . '/js/colorbox_inline.js');
  130. }
  131. // We need to have multiple unique IDs, one for each record.
  132. static $i = 0;
  133. $i = mt_rand();
  134. // Return nothing if no trigger filed is selected.
  135. if (empty($this->options['trigger_field'])) {
  136. return;
  137. }
  138. // Get the token information and generate the value for the popup and the
  139. // caption.
  140. $tokens = $this->get_render_tokens($this->options['alter']);
  141. $popup = filter_xss_admin($this->options['popup']);
  142. $caption = filter_xss_admin($this->options['caption']);
  143. $gallery = filter_xss_admin($this->options['custom_gid']);
  144. $popup = strtr($popup, $tokens);
  145. $caption = strtr($caption, $tokens);
  146. $gallery = drupal_html_class(strtr($gallery, $tokens));
  147. // Return nothing if popup is empty.
  148. if (empty($popup)) {
  149. return;
  150. }
  151. $width = $this->options['width'] ? $this->options['width'] : '';
  152. $height = $this->options['height'] ? $this->options['height'] : '';
  153. $gallery_id = !empty($gallery) ? $gallery : ($this->options['gid'] ? 'gallery-' . $this->view->name : '');
  154. $link_text = $tokens["[{$this->options['trigger_field']}]"];
  155. $link_options = array(
  156. 'html' => TRUE,
  157. 'fragment' => 'colorbox-inline-' . $i,
  158. 'query' => array(
  159. 'width' => $width,
  160. 'height' => $height,
  161. 'title' => $caption,
  162. 'inline' => 'true'
  163. ),
  164. 'attributes' => array(
  165. 'class' => array('colorbox-inline'),
  166. 'rel' => $gallery_id
  167. )
  168. );
  169. // Remove any parameters that aren't set.
  170. $link_options['query'] = array_filter($link_options['query']);
  171. // If the nid is present make the link degrade to the node page if
  172. // JavaScript is off.
  173. $link_target = isset($values->nid) ? 'node/' . $values->nid : '';
  174. $link_tag = l($link_text, $link_target, $link_options);
  175. // The outside div is there to hide all of the divs because if the specific Colorbox
  176. // div is hidden it won't show up as a Colorbox.
  177. return $link_tag . '<div style="display: none;"><div id="colorbox-inline-' . $i . '">' . $popup . '</div></div>';
  178. }
  179. }