You are here

colorbox_field_formatter.module in Colorbox field formatter 7

Implement a colorbox field formatter for proper links.

File

colorbox_field_formatter.module
View source
<?php

/**
 * @file
 * Implement a colorbox field formatter for proper links.
 */

/**
 * Implements hook_field_formatter_info().
 */
function colorbox_field_formatter_field_formatter_info() {
  return array(
    'colorbox_field_formatter_linked' => array(
      'label' => t('Colorbox link'),
      'field types' => array(
        'text',
        'computed',
        'entityreference',
        'image',
      ),
      'settings' => array(
        'colorbox_field_formatter_image_style' => '',
        'colorbox_field_formatter_style' => 'colorbox-load',
        'colorbox_field_formatter_link_type' => 'content',
        'colorbox_field_formatter_link' => '',
        'colorbox_field_formatter_width' => '500',
        'colorbox_field_formatter_height' => '500',
        'colorbox_field_formatter_iframe' => 0,
        'colorbox_field_formatter_anchor' => '',
        'colorbox_field_formatter_class' => '',
        'colorbox_field_formatter_rel' => '',
      ),
    ),
  );
}

/**
 * Callback to provide an array for a select field containing all supported
 * colorbox styles.
 *
 * @return array
 */
function _colorbox_field_formatter_get_styles() {
  $styles = array();
  if (variable_get('colorbox_inline', 0)) {
    $styles['colorbox-inline'] = t('Colorbox inline');
  }
  if (variable_get('colorbox_load', 0)) {
    $styles['colorbox-load'] = t('Colorbox load');
  }
  if (module_exists('colorbox_node')) {
    $styles['colorbox-node'] = t('Colorbox node');
  }
  return $styles;
}

/**
 * Callback to provide an arry for a select field containing all link types.
 *
 * @return array
 */
function _colorbox_field_formatter_get_link_types() {
  return array(
    'content' => t('Content'),
    'manual' => t('Manually provide a link'),
  );
}

/**
 * Implements hook_field_formatter_settings_form().
 */
function colorbox_field_formatter_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
  $settings = $instance['display'][$view_mode]['settings'];
  $element = array();
  $styles = _colorbox_field_formatter_get_styles();
  $style_keys = array_keys($styles);
  $default_style = array_pop($style_keys);
  if ($field['type'] == 'image') {
    $image_styles = image_style_options(FALSE);
    $image_styles['hide'] = t('Hide (do not display image)');
    $element['colorbox_field_formatter_image_style'] = array(
      '#title' => t('Content image style'),
      '#type' => 'select',
      '#default_value' => empty($settings['colorbox_field_formatter_image_style']) ? '' : $settings['colorbox_field_formatter_image_style'],
      '#empty_option' => t('None (original image)'),
      '#options' => $image_styles,
      '#description' => t('Image style to use in the content.'),
    );
  }
  $element['colorbox_field_formatter_style'] = array(
    '#title' => t('Style of colorbox'),
    '#type' => 'select',
    '#default_value' => !empty($settings['colorbox_field_formatter_style']) ? $settings['colorbox_field_formatter_style'] : $default_style,
    '#options' => $styles,
  );
  $element['colorbox_field_formatter_link_type'] = array(
    '#title' => t('Link colorbox to'),
    '#type' => 'select',
    '#default_value' => $settings['colorbox_field_formatter_link_type'],
    '#options' => _colorbox_field_formatter_get_link_types(),
    '#attributes' => array(
      'class' => array(
        'colorbox-field-formatter-link-type',
      ),
    ),
  );
  $element['colorbox_field_formatter_link'] = array(
    '#title' => t('URI'),
    '#type' => 'textfield',
    '#default_value' => $settings['colorbox_field_formatter_link'],
    '#states' => array(
      'visible' => array(
        'select.colorbox-field-formatter-link-type' => array(
          'value' => 'manual',
        ),
      ),
    ),
  );
  if (module_exists('token')) {
    $element['token_help_wrapper'] = array(
      '#type' => 'container',
      '#states' => array(
        'visible' => array(
          'select.colorbox-field-formatter-link-type' => array(
            'value' => 'manual',
          ),
        ),
      ),
    );
    $element['token_help_wrapper']['token_help'] = array(
      '#theme' => 'token_tree',
      '#token_types' => array(
        'entity' => $form['#entity_type'],
      ),
      '#global_types' => FALSE,
    );
  }
  $element['colorbox_field_formatter_width'] = array(
    '#title' => t('Width'),
    '#type' => 'textfield',
    '#default_value' => $settings['colorbox_field_formatter_width'],
  );
  $element['colorbox_field_formatter_height'] = array(
    '#title' => t('Height'),
    '#type' => 'textfield',
    '#default_value' => $settings['colorbox_field_formatter_height'],
  );
  $element['colorbox_field_formatter_iframe'] = array(
    '#title' => t('iFrame Mode'),
    '#type' => 'checkbox',
    '#default_value' => $settings['colorbox_field_formatter_iframe'],
  );
  $element['colorbox_field_formatter_anchor'] = array(
    '#title' => t('Anchor'),
    '#type' => 'textfield',
    '#default_value' => $settings['colorbox_field_formatter_anchor'],
    '#description' => t('This can be used with the "inline" style by entering the ID (without the leading "#") of the inline content to colorbox.'),
  );
  $element['colorbox_field_formatter_class'] = array(
    '#title' => t('Class'),
    '#type' => 'textfield',
    '#default_value' => $settings['colorbox_field_formatter_class'],
  );
  $element['colorbox_field_formatter_rel'] = array(
    '#title' => t('Rel'),
    '#type' => 'textfield',
    '#default_value' => empty($settings['colorbox_field_formatter_rel']) ? '' : $settings['colorbox_field_formatter_rel'],
    '#description' => t('This can be used to identify a group for Colorbox to cycle through.'),
  );
  return $element;
}

/**
 * Implements hook_field_formatter_settings_summary().
 */
function colorbox_field_formatter_field_formatter_settings_summary($field, $instance, $view_mode) {
  $settings = $instance['display'][$view_mode]['settings'];
  $summary = array();
  if ($field['type'] == 'image') {
    $image_styles = image_style_options(FALSE);
    unset($image_styles['']);
    $image_style = '<strong>' . t('Content image style') . '</strong> ';
    if (isset($image_styles[$settings['colorbox_field_formatter_image_style']])) {
      $image_style .= $image_styles[$settings['colorbox_field_formatter_image_style']];
    }
    elseif ($settings['colorbox_field_formatter_image_style'] == 'hide') {
      $image_style .= t('Hide');
    }
    else {
      $image_style .= t('Original image');
    }
    $summary[] = $image_style;
  }
  $styles = _colorbox_field_formatter_get_styles();
  $summary[] = '<strong>' . t('Style of colorbox') . '</strong> ' . $styles[$settings['colorbox_field_formatter_style']];
  $types = _colorbox_field_formatter_get_link_types();
  switch ($settings['colorbox_field_formatter_link_type']) {
    case 'manual':
      $summary[] = '<strong>' . t('Link to') . '</strong> ' . $settings['colorbox_field_formatter_link'];
      break;
    default:
      $summary[] = '<strong>' . t('Link to') . '</strong> ' . $types[$settings['colorbox_field_formatter_link_type']];
      break;
  }
  $summary[] = '<strong>' . t('Width') . '</strong> ' . $settings['colorbox_field_formatter_width'];
  $summary[] = '<strong>' . t('Height') . '</strong> ' . $settings['colorbox_field_formatter_height'];
  $summary[] = '<strong>' . t('iFrame Mode') . '</strong> ' . ($settings['colorbox_field_formatter_style'] ? t('Yes') : t('No'));
  if (!empty($settings['colorbox_field_formatter_anchor'])) {
    $summary[] = '<strong>' . t('Anchor') . '</strong> #' . $settings['colorbox_field_formatter_anchor'];
  }
  if (!empty($settings['colorbox_field_formatter_class'])) {
    $summary[] = '<strong>' . t('Classes') . '</strong> ' . $settings['colorbox_field_formatter_class'];
  }
  return implode('<br />', $summary);
}

/**
 * Implements hook_field_formatter_view().
 */
function colorbox_field_formatter_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $settings = $display['settings'];
  $element = array();
  foreach ($items as $delta => $item) {
    $uri = FALSE;
    if ($field['type'] == 'image') {
      $image = array(
        'path' => $item['uri'],
        'alt' => isset($item['alt']) ? $item['alt'] : '',
        'title' => isset($item['title']) ? $item['title'] : '',
        'style_name' => $settings['colorbox_field_formatter_image_style'],
      );
      if (empty($settings['colorbox_field_formatter_image_style'])) {
        $output = theme('image', $image);
      }
      else {
        $output = theme('image_style', $image);
      }
    }
    else {
      if (isset($item['safe_value'])) {
        $output = $item['safe_value'];
      }
      else {
        if (isset($item['value'])) {
          $output = $item['value'];
        }
        else {
          if (isset($item['target_id'])) {
            $target = entity_load($field['settings']['target_type'], array(
              $item['target_id'],
            ));
            if (is_array($target)) {
              $target = reset($target);
            }
            $output = entity_label($field['settings']['target_type'], $target);
            $uri = entity_uri($field['settings']['target_type'], $target);
          }
          else {
            $output = FALSE;
          }
        }
      }
    }
    if (!empty($output)) {
      if (!$uri) {
        if ($settings['colorbox_field_formatter_link_type'] == 'content') {
          $uri = entity_uri($entity_type, $entity);
        }
        else {
          $link = module_exists('token') ? token_replace($settings['colorbox_field_formatter_link'], array(
            $entity_type => $entity,
          )) : $settings['colorbox_field_formatter_link'];
          $uri = array(
            'path' => $link,
          );
        }
      }
      $options = array(
        'html' => TRUE,
        'attributes' => array(
          'class' => array(
            $settings['colorbox_field_formatter_style'],
          ),
        ),
        'query' => array(
          'width' => $settings['colorbox_field_formatter_width'],
          'height' => $settings['colorbox_field_formatter_height'],
        ),
      );
      if ($settings['colorbox_field_formatter_iframe']) {
        $options['query']['iframe'] = 'true';
      }
      if (!empty($settings['colorbox_field_formatter_anchor'])) {
        $options['fragment'] = $settings['colorbox_field_formatter_anchor'];
      }
      if ($settings['colorbox_field_formatter_style'] == 'colorbox-inline') {
        $options['query']['inline'] = 'true';
      }
      if (!empty($settings['colorbox_field_formatter_class'])) {
        $options['attributes']['class'] = array_merge($options['attributes']['class'], explode(' ', $settings['colorbox_field_formatter_class']));
      }
      if (!empty($settings['colorbox_field_formatter_rel'])) {
        $options['attributes']['rel'] = $settings['colorbox_field_formatter_rel'];
      }
      $output = l($output, $uri['path'], $options);
      $element[$delta] = array(
        '#markup' => $output,
      );
    }
  }
  return $element;
}