You are here

public function colorbox_handler_field_colorbox::render in Colorbox 7.2

Same name and namespace in other branches
  1. 6 views/colorbox_handler_field_colorbox.inc \colorbox_handler_field_colorbox::render()
  2. 7 views/colorbox_handler_field_colorbox.inc \colorbox_handler_field_colorbox::render()

Render the trigger field and its linked popup information.

Overrides views_handler_field::render

File

views/colorbox_handler_field_colorbox.inc, line 167
Views handlers for Colorbox module.

Class

colorbox_handler_field_colorbox
A handler to provide a field that is completely custom by the administrator.

Code

public function render($values) {

  // Load the necessary js file for Colorbox activation.
  if (_colorbox_active() && !variable_get('colorbox_inline', 0)) {
    drupal_add_js(drupal_get_path('module', 'colorbox') . '/js/colorbox_inline.js');
  }

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

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

  // Get the token information and generate the value for the popup and the
  // caption.
  $tokens = $this
    ->get_render_tokens($this->options['alter']);
  $popup = filter_xss_admin($this->options['popup']);
  $caption = filter_xss_admin($this->options['caption']);
  $gallery = filter_xss_admin($this->options['custom_gid']);
  $popup = strtr($popup, $tokens);
  $caption = strtr($caption, $tokens);
  $gallery = drupal_html_class(strtr($gallery, $tokens));

  // Return nothing if popup is empty.
  if (empty($popup)) {
    return;
  }
  $width = $this->options['width'] ? $this->options['width'] : '';
  $height = $this->options['height'] ? $this->options['height'] : '';
  $gallery_id = !empty($gallery) ? $gallery : ($this->options['gid'] ? 'gallery-' . $this->view->name : '');
  $link_text = $tokens["[{$this->options['trigger_field']}]"];
  $link_options = array(
    'html' => TRUE,
    'fragment' => 'colorbox-inline-' . $i,
    'query' => array(
      'width' => $width,
      'height' => $height,
      'title' => $caption,
      'inline' => 'true',
    ),
    'attributes' => array(
      'class' => array(
        'colorbox-inline',
      ),
      'rel' => $gallery_id,
    ),
  );

  // Remove any parameters that aren't set.
  $link_options['query'] = array_filter($link_options['query']);

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

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