lightbox2_handler_field_lightbox2.inc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /**
  3. * @file
  4. * Contain the integration with views
  5. * A handler to provide a field that is completely custom by the administrator.
  6. *
  7. * @ingroup views_field_handlers
  8. */
  9. class lightbox2_handler_field_lightbox2 extends views_handler_field {
  10. function query() {
  11. // Do nothing, as this handler does not need to do anything to the query itself.
  12. }
  13. function option_definition() {
  14. $options = parent::option_definition();
  15. $options['trigger_field'] = array('default' => '');
  16. $options['popup'] = array('default' => '');
  17. $options['caption'] = array('default' => '');
  18. $options['rel_group'] = array('default' => TRUE);
  19. $options['custom_group'] = array('default' => '');
  20. $options['height'] = array('default' => '400px');
  21. $options['width'] = array('default' => '600px');
  22. return $options;
  23. }
  24. function options_form(&$form, &$form_state) {
  25. parent::options_form($form, $form_state);
  26. $fields = array('trigger_field' => t('<None>'));
  27. foreach ($this->view->display_handler->get_handlers('field') as $field => $handler) {
  28. // We only use fields up to this one. Obviously we can't use this handler
  29. // as the trigger handler.
  30. if ($field == $this->options['id']) {
  31. break;
  32. }
  33. $fields[$field] = $handler->definition['title'];
  34. }
  35. $form['trigger_field'] = array(
  36. '#type' => 'select',
  37. '#title' => t('Trigger field'),
  38. '#description' => t('Select the field that should be turned into the trigger for the lightbox. Only fields that appear before this one in the field list may be used.'),
  39. '#options' => $fields,
  40. '#default_value' => $this->options['trigger_field'],
  41. '#weight' => -12,
  42. );
  43. $form['popup'] = array(
  44. '#type' => 'textarea',
  45. '#title' => t('Popup'),
  46. '#description' => t('Combine tokens from the "Replacement patterns" below and html to create what the lightbox popup will become.'),
  47. '#default_value' => $this->options['popup'],
  48. '#weight' => -11,
  49. );
  50. $form['caption'] = array(
  51. '#type' => 'textfield',
  52. '#title' => t('Caption'),
  53. '#description' => t('Combine tokens from the "Replacement patterns" below and html to create the caption shown under the lightbox. Leave empty for no caption.'),
  54. '#default_value' => $this->options['caption'],
  55. '#weight' => -10,
  56. );
  57. $form['rel_group'] = array(
  58. '#type' => 'checkbox',
  59. '#title' => t('Automatic generated Lightbox group'),
  60. '#description' => t('Enable Lightbox grouping using a generated group name for this view.'),
  61. '#default_value' => $this->options['rel_group'],
  62. '#weight' => -9,
  63. );
  64. $form['custom_group'] = array(
  65. '#type' => 'textfield',
  66. '#title' => t('Custom Lightbox group'),
  67. '#description' => t('Enable Lightbox grouping with a given string as group. Overrides the automatically generated group name above.'),
  68. '#default_value' => $this->options['custom_group'],
  69. '#weight' => -8,
  70. );
  71. $form['height'] = array(
  72. '#type' => 'textfield',
  73. '#title' => t('Height'),
  74. '#description' => t('Specify the height of the lightbox2 popup window. Because the content is dynamic, we cannot detect this value automatically.'),
  75. '#default_value' => $this->options['height'],
  76. '#weight' => -7,
  77. );
  78. $form['width'] = array(
  79. '#type' => 'textfield',
  80. '#title' => t('Width'),
  81. '#description' => t('Specify the width of the lightbox2 popup window. Because the content is dynamic, we cannot detect this value automatically.'),
  82. '#default_value' => $this->options['width'],
  83. '#weight' => -6,
  84. );
  85. // Remove the checkboxs and other irrelevant controls.
  86. unset($form['alter']['alter_text']);
  87. unset($form['alter']['make_link']);
  88. unset($form['alter']['text']);
  89. unset($form['alter']['path']);
  90. unset($form['alter']['alt']);
  91. unset($form['alter']['prefix']);
  92. unset($form['alter']['suffix']);
  93. unset($form['alter']['text']['#dependency']);
  94. unset($form['alter']['text']['#process']);
  95. }
  96. /**
  97. * Render the trigger field and its linked popup information.
  98. */
  99. function render($values) {
  100. // We need to have multiple unique IDs, one for each record.
  101. static $i = 0;
  102. static $link;
  103. if (!empty($this->options['trigger_field'])) {
  104. // We don't actually use the link, but we need it there for lightbox to function.
  105. if (empty($link)) {
  106. // Get the path name.
  107. $path = isset($_GET['q']) ? $_GET['q'] : '<front>';
  108. $link = url($path, array('absolute' => TRUE));
  109. }
  110. // Get the token information and generate the value for the popup and the
  111. // caption.
  112. $tokens = $this->get_render_tokens($this->options['alter']);
  113. $popup = filter_xss_admin($this->options['popup']);
  114. $caption = filter_xss_admin($this->options['caption']);
  115. $popup = strtr($popup, $tokens);
  116. $caption = strtr($caption, $tokens);
  117. $i++;
  118. // The outside div is there to hide all of the divs because if the specific lightbox
  119. // div is hidden it won't show up as a lightbox. We also specify a group
  120. // in the rel attribute in order to link the whole View together for paging.
  121. $group_name = !empty($this->options['custom_group']) ? $this->options['custom_group'] : ($this->options['rel_group'] ? 'lightbox-popup-' . $this->view->name . '-' . implode('/', $this->view->args) : '');
  122. return "<a href='$link #lightbox-popup-{$i}' rel='lightmodal[{$group_name}|width:" . ($this->options['width'] ? $this->options['width'] : '600px') . ';height:' . ($this->options['height'] ? $this->options['height'] : '600px') . "][" . $caption . "]'>" . $tokens["[{$this->options['trigger_field']}]"] . "</a>
  123. <div style='display: none;'><div id='lightbox-popup-{$i}' class='lightbox-popup'>$popup</div></div>";
  124. }
  125. else {
  126. return;
  127. }
  128. }
  129. }