Skip to content
media_colorbox.module 20.8 KiB
Newer Older
Kurt Madel's avatar
Kurt Madel committed
<?php
/**
 * @file
Kurt Madel's avatar
Kurt Madel committed
 * Provides integration between Media file entity fields and the Colorbox module.
Kurt Madel's avatar
Kurt Madel committed
 */

/**
 * Implements hook_theme().
 */
Kurt Madel's avatar
Kurt Madel committed
function media_colorbox_theme() {
Kurt Madel's avatar
Kurt Madel committed
  return array(
Kurt Madel's avatar
Kurt Madel committed
    'media_colorbox' => array(
Kurt Madel's avatar
Kurt Madel committed
      'variables' => array(
        'item_style' => NULL,
        'item_class' => NULL,
        'field' => array(),
        'display_settings' => array(),
        'langcode' => NULL,
        'path' => NULL,
        'title' => NULL,
Kurt Madel's avatar
Kurt Madel committed
        'media_colorbox_caption' => NULL, //for file entity field based caption
Kurt Madel's avatar
Kurt Madel committed
      'path' => drupal_get_path('module', 'media_colorbox'),
      'file' => 'media_colorbox.theme.inc',
Kurt Madel's avatar
Kurt Madel committed
    ),
  );
}

Kurt Madel's avatar
Kurt Madel committed
function media_colorbox_menu() {
  $items['media_colorbox/%file/%/%'] = array(
Kurt Madel's avatar
Kurt Madel committed
    'page callback' => 'media_colorbox_callback',
    'page arguments' => array(1, 2, 3),
    'access callback' => 'file_entity_access',
Kurt Madel's avatar
Kurt Madel committed
    'file' => 'media_colorbox.pages.inc',
/**
 * Implements hook_entity_info_alter().
 *
 * Add default Colorbox view mode to the file entity type as a simple starting point.
 */
function media_colorbox_entity_info_alter(&$entity_info) {
  $entity_info['file']['view modes']['colorbox'] = array('label' => t('Colorbox'), 'custom settings' => TRUE);
}


Kurt Madel's avatar
Kurt Madel committed
/**
 * Implements hook_field_formatter_info().
 */
Kurt Madel's avatar
Kurt Madel committed
function media_colorbox_field_formatter_info() {
Kurt Madel's avatar
Kurt Madel committed
  return array(
Kurt Madel's avatar
Kurt Madel committed
    'media_colorbox' => array(
      'label' => t('Media Colorbox'),
      'field types' => array('file', 'image'),
        'file_view_mode' => 'small',
        'fixed_width' => '',
        'fixed_height' => '',
        'colorbox_gallery' => 'post',
        'colorbox_gallery_custom' => '',
Kurt Madel's avatar
Kurt Madel committed
        'colorbox_caption' => 'title',//for file entity field based caption
        'audio_playlist' => FALSE,//for file entity field based caption
Kurt Madel's avatar
Kurt Madel committed
    ),
    'colorbox_link' => array(
      'label' => t('Colorbox Link'),
      'field types' => array('image', 'text'),
      'settings' => array(
        'text_format' => 'text_default',
        'image_style' => 'thumbnail',
        'colorbox_view_mode' => 'default',
        'fixed_width' => '',
        'fixed_height' => '',
        'colorbox_gallery' => 'post',
        'colorbox_gallery_custom' => '',
        'colorbox_caption' => 'title',//for file entity field based caption
        'audio_playlist' => FALSE,//for file entity field based caption
      ),
    ),
Kurt Madel's avatar
Kurt Madel committed
  );
}

/**
 * Implements hook_field_formatter_settings_form().
Kurt Madel's avatar
Kurt Madel committed
 */
Kurt Madel's avatar
Kurt Madel committed
function media_colorbox_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
Kurt Madel's avatar
Kurt Madel committed
  $display = $instance['display'][$view_mode];
  $settings = $display['settings'];
Kurt Madel's avatar
Kurt Madel committed

  if ($display['type'] == 'media_colorbox' || $display['type'] == 'colorbox_link') {
    $entity_info = entity_get_info('file');
    $options = array('default' => t('Default'));
    foreach ($entity_info['view modes'] as $file_view_mode => $file_view_mode_info) {
      $options[$file_view_mode] = $file_view_mode_info['label'];
    }
    
    //if $field is not set, then managing File type file display
    //remove current view_mode as selecting current view mode as file view mode will cause fatal error
    if (!isset($field)) {
      $file_entity_manage = TRUE;
      unset($options[$view_mode]);
    }
    
    //also need to unset any view mode for any entity type already using the media_colorbox formatter
    foreach ($entity_info['view modes'] as $file_view_mode => $file_view_mode_info) {
      foreach ($entity_info['bundles'] as $file_bundle => $file_bundle_info) {
        $display_name = $file_bundle . '__' . $file_view_mode . '__file_field_media_colorbox';
        $result = ctools_export_load_object('file_display', 'names', array($display_name));
        //there will be a record in the file_display table if this view_mode has been configured to use Media Colorbox
        //the status will be 1 if the media_colorbox formatter is active for this view_mode
        if (isset($result[$display_name]) && $result[$display_name]->status == 1) {
          unset($options[$file_view_mode]);
        }
      }
    }
    
    //only for media_colorbox, not colorbox_link
    if ($display['type'] == 'media_colorbox'){
      $element['file_view_mode'] = array(
        '#title' => t('File view mode'),
        '#type' => 'select',
        '#default_value' => $settings['file_view_mode'],
        '#options' => $options,
      );      
    }
    
    if($display['type'] == 'colorbox_link'){
      //had an issue when using this formatter in a View, results in a Fatal error: undefined function field_ui_formatter_options()
      if (!function_exists('field_ui_formatter_options')) {
        module_load_include('inc', 'field_ui', 'field_ui.admin');
      } 
      if($field['type'] == 'text'){
        //formatter for text
        // Get the list of formatters for this field type, and remove our own.
        $formatters = field_ui_formatter_options($field['type']);
        $element['text_format'] = array(
          '#type' => 'select',
          '#title' => t('Text Field format'),
          '#options' => $formatters,
          '#description'    => t('The format to be used for the text being used as Colorbox link.'),  //helper text
          '#default_value' => $settings['text_format'],
        );
      }elseif($field['type'] == 'image'){
        $image_styles = image_style_options(FALSE);
        $element['image_style'] = array(
          '#title' => t('Image style'),
          '#type' => 'select',
          '#description'    => t('The image style to be used for the image being used as Colorbox link.'),  //helper text
          '#default_value' => $settings['image_style'],
          '#options' => $image_styles,
        );
      }
    }
    
    $element['colorbox_view_mode'] = array(
      '#title' => t('Colorbox view mode'),
      '#type' => 'select',
      '#default_value' => $settings['colorbox_view_mode'],
      '#options' => $options,
    );
    //add description for File type manage file dispay form
    if ($file_entity_manage) {
      //only for media_colorbox, not colorbox_link
      if ($display['type'] == 'media_colorbox'){
        $element['file_view_mode']['#description'] = t('Only view modes not already using the Media Colorbox formatter');
      }
      $element['colorbox_view_mode']['#description'] = t('Only view modes not already using the Media Colorbox formatter');
    }
    $element['fixed_width'] = array(
      '#title' => t('Fixed Width'),
      '#description' => t('Will set the total fixed width (numeric value) of the Colorbox to include borders and buttons. Ex: 800'),
      '#default_value' => $settings['fixed_width'],
      '#size' => 10,
    );
    $element['fixed_height'] = array(
      '#title' => t('Fixed Height'),
      '#description' => t('Will set the total fixed height (numeric value) of the Colorbox to include borders and buttons. Ex: 600'),
      '#type' => 'textfield',
      '#default_value' => $settings['fixed_height'],
      '#size' => 10,
    );
    $gallery = array(
      'post' => t('Per post gallery'),
      'page' => t('Per page gallery'),
      'field_post' => t('Per field in post gallery'),
      'field_page' => t('Per field in page gallery'),
      'custom' => t('Custom'),
      'none' => t('No gallery'),
    );
    $element['colorbox_gallery'] = array(
      '#title' => t('Gallery (image grouping)'),
      '#type' => 'select',
      '#default_value' => $settings['colorbox_gallery'],
      '#options' => $gallery,
      '#description' => t('How Colorbox should group the image galleries.'),
    );
    $element['colorbox_gallery_custom'] = array(
      '#title' => t('Custom gallery'),
      '#type' => 'machine_name',
      '#maxlength' => 32,
      '#default_value' => $settings['colorbox_gallery_custom'],
      '#description' => t('All images on a page with the same gallery value (rel attribute) will be grouped together. It must only contain lowercase letters, numbers, and underscores.'),
      '#required' => FALSE,
      '#machine_name' => array(
        'exists' => 'colorbox_gallery_exists',
        'error' => t('The custom gallery field must only contain lowercase letters, numbers, and underscores.'),
      ),
      '#states' => array(
        'visible' => array(
          ':input[name$="[settings_edit_form][settings][colorbox_gallery]"]' => array('value' => 'custom'),
        ),
      ),
    );
Kurt Madel's avatar
Kurt Madel committed
    //for file entity field based caption
    $caption = array(
      'title' => t('Title text'),
      'mediafield' => t('File Type text field'),
      'none' => t('None'),
    );
    $element['colorbox_caption'] = array(
      '#title' => t('Caption'),
      '#type' => 'select',
Kurt Madel's avatar
Kurt Madel committed
      '#default_value' => $settings['colorbox_caption'],
Kurt Madel's avatar
Kurt Madel committed
      '#description' => t('Title will use the label of your Media field for this content type<br/>and File Type text field will use the value <br/>of a specified text field for the file type being displayed.'),
    
    $element['audio_playlist'] = array(
      '#title' => t('Enable Audio Playlist'),
      '#description' => t('Enable this setting for HTML5 audio and Colorbox will automatically load the next audio file that is part of a Colorbox gallery.'),
      '#type' => 'checkbox',
      '#default_value' => $settings['audio_playlist'],
    );
Kurt Madel's avatar
Kurt Madel committed

  return $element;
}

/**
 * Implements hook_field_formatter_settings_summary().
Kurt Madel's avatar
Kurt Madel committed
 */
Kurt Madel's avatar
Kurt Madel committed
function media_colorbox_field_formatter_settings_summary($field, $instance, $view_mode) {
Kurt Madel's avatar
Kurt Madel committed
  $display = $instance['display'][$view_mode];
  $settings = $display['settings'];
  $summary = array();

  if ($display['type'] == 'media_colorbox' || $display['type'] == 'colorbox_link') {
    $entity_info = entity_get_info('file');
    $options = array('default' => t('Default'));
    foreach ($entity_info['view modes'] as $file_view_mode => $file_view_mode_info) {
      $options[$file_view_mode] = $file_view_mode_info['label'];
    }
    if ($display['type'] == 'media_colorbox') {
      if (isset($options[$settings['file_view_mode']])) {
        $summary[] = t('File view mode: @style', array('@style' => $options[$settings['file_view_mode']]));
      }
      else {
        $summary[] = t('File view mode: none');
      }
    
    //settings for colorbox_link
    if($display['type'] == 'colorbox_link'){
      if($field['type'] == 'text'){
        $summary[] = t('Text format: @style', array('@style' => $settings['text_format']));
      }elseif($field['type'] == 'image'){
        $summary[] = t('Image style: @style', array('@style' => $settings['image_style']));
      }
    }

    if (isset($options[$settings['colorbox_view_mode']])) {
      $summary[] = t('Colorbox view mode: @style', array('@style' => $options[$settings['colorbox_view_mode']]));
    }
    else {
      $summary[] = t('Colorbox view mode: none');
    }

    if (isset($settings['fixed_width'])) {
      $summary[] = t('Fixed Width: @dimensions', array('@dimensions' => $settings['fixed_width']));
    }

    if (isset($settings['fixed_height'])) {
      $summary[] = t('Fixed Height: @dimensions', array('@dimensions' => $settings['fixed_height']));
    }

    $gallery = array(
      'post' => t('Per post gallery'),
      'page' => t('Per page gallery'),
      'field_post' => t('Per field in post gallery'),
      'field_page' => t('Per field in page gallery'),
      'custom' => t('Custom'),
      'none' => t('No gallery'),
    );
    if (isset($settings['colorbox_gallery'])) {
      $summary[] = t('Colorbox gallery type: @type', array('@type' => $gallery[$settings['colorbox_gallery']])) . ($settings['colorbox_gallery'] == 'custom' ? ' (' . $settings['colorbox_gallery_custom'] . ')' : '');
    }
Kurt Madel's avatar
Kurt Madel committed
    //custom caption field
    $caption = array(
      'title' => t('Title text'),
      'mediafield' => t('File Type text field'),
      'none' => t('None'),
    );
    if (isset($settings['colorbox_caption'])) {
      $summary[] = t('Colorbox caption: @type', array('@type' => $caption[$settings['colorbox_caption']]));
    }
    $summary[] = t('Audio Playlist: @value', array('@value' => (isset($settings['audio_playlist']) && $settings['audio_playlist']) ? t('Enabled') : t('Disabled')));
Kurt Madel's avatar
Kurt Madel committed
  }

  return implode('<br />', $summary);
}

/**
 * Implements hook_field_formatter_view().
 */
function media_colorbox_field_formatter_view($entity_type, $entity, $field, $instances = NULL, $langcode = NULL, $items = NULL, $display = NULL) {
Kurt Madel's avatar
Kurt Madel committed
  $element = array();
  GLOBAL $nid;
  $nid=1;
  if(isset($entity->nid)) {//file embedded in node
    $nid = $entity->nid;
  }elseif(isset($entity->fid) && $nid != 1){
    $nid = $entity->fid;
  }
  
  if($display['type'] == 'colorbox_link'){

    foreach ($items as $delta => $item) {
      $file_id = NULL;
      $colorbox_field_caption_value = '';
      if($entity_type == 'file'){
        $file_id = $entity->fid;
        $colorbox_caption_var_name = 'media_colorbox_' . $entity->type . '_caption_field_name';
        $colorbox_caption_field_name = variable_get($colorbox_caption_var_name);
        $colorbox_field_caption_value = NULL;
        if (isset($items[0]{$colorbox_caption_field_name})){
          $colorbox_field_caption_value = isset($items[0]{$colorbox_caption_field_name}['und'][0]) ?
          $items[0]{$colorbox_caption_field_name}['und'][0]['value'] : NULL;
        }
      }
      $render_array = array();
      $display['type'] = 'default_formatter';
      $formatter_type = field_info_formatter_types($display['type']);
      $display['module'] = $formatter_type['module'];

      $render_array = field_view_field($entity_type, $entity, $field['field_name'], $display, $langcode); 
      $render_array['#label_display'] = 'hidden';//always set to hidden, label will appear outside of link if set to be visisble

      $element[$delta] = array(
        '#theme' => 'media_colorbox',
        '#entity_id' => $nid,
        '#item' => $render_array,
        '#field' => $field,
        '#display_settings' => $display['settings'],
        '#langcode' => $langcode,
        '#path' => 'media_colorbox/' . $file_id . '/' . $display['settings']['colorbox_view_mode'] . '/' . $langcode,
        //this is where Media is being added as the title attribute
        '#title' => isset($entity->filename) ? strip_tags($entity->filename) : NULL,
        '#media_colorbox_caption' => $colorbox_field_caption_value,
      );
    }
  }
  
  //the $field won't be set if being formatted by file_entity_file_formatter_file_field_view
  if(!isset($field) && ($display['type'] == 'media_colorbox' || $display['type'] == 'colorbox_link')) {//standalone entity
    $colorbox_caption_var_name = 'media_colorbox_' . $items[0]['type'] . '_caption_field_name';
    $colorbox_caption_field_name = variable_get($colorbox_caption_var_name);
    $colorbox_field_caption_value = NULL;

    if (isset($items[0]{$colorbox_caption_field_name})){
      $colorbox_field_caption_value = isset($items[0]{$colorbox_caption_field_name}['und'][0]) ?
      $items[0]{$colorbox_caption_field_name}['und'][0]['value'] : NULL;
    }
    //get attributes for item to use on item and wrapping link
    $class = isset($items[0]['override']['class']) ? $items[0]['override']['class'] : NULL;
    $style = isset($items[0]['override']['style']) ? $items[0]['override']['style'] : NULL;
    
    //variable array for theme_media_colorbox
    $element[0] = array(
        '#theme' => 'media_colorbox',
        '#item_class' => $class,
        '#item_style' => isset($items[0]['override']['style']) ? $items[0]['override']['style'] : NULL,
        '#file_id' => $entity->fid,
        '#field' => $field,
        '#display_settings' => $display['settings'],
        '#langcode' => $langcode,
        '#path' => 'media_colorbox/' . $entity->fid . '/' . $display['settings']['colorbox_view_mode'] . '/' . $langcode,
        '#title' => isset($instances['label']) ? strip_tags($instances['label']) : NULL,
        '#media_colorbox_caption' => $colorbox_field_caption_value,
      );
  }
  elseif ($display['type'] == 'media_colorbox') {//file field on another entity, $field is set
    foreach ($items as $delta => $item) {
      $colorbox_caption_var_name = 'media_colorbox_' . $item['type'] . '_caption_field_name';
Kurt Madel's avatar
Kurt Madel committed
      $colorbox_caption_field_name = variable_get($colorbox_caption_var_name);
      $colorbox_field_caption_value = NULL;
      
      if (isset($item{$colorbox_caption_field_name})){
        $colorbox_field_caption_value = isset($item{$colorbox_caption_field_name}['und'][0]) ?
        $item{$colorbox_caption_field_name}['und'][0]['value'] : NULL;
Kurt Madel's avatar
Kurt Madel committed
      }
Kurt Madel's avatar
Kurt Madel committed
        '#theme' => 'media_colorbox',
        '#entity_id' => $nid,
        '#file_id' => $item['fid'],
        '#field' => $field,
        '#display_settings' => $display['settings'],
        '#langcode' => $langcode,
        '#path' => 'media_colorbox/' . $item['fid'] . '/' . $display['settings']['colorbox_view_mode'] . '/' . $langcode,
Kurt Madel's avatar
Kurt Madel committed
        //this is where Media is being added as the title attribute
        '#title' => isset($instances['label']) ? strip_tags($instances['label']) : NULL,
Kurt Madel's avatar
Kurt Madel committed
        '#media_colorbox_caption' => $colorbox_field_caption_value,
Kurt Madel's avatar
Kurt Madel committed
  }
  return $element;
}

/**
 * Implements hook_form_FORM_ID_alter().
 * Disables the media_colorbox formatter for the default file display
 */
function media_colorbox_form_file_entity_file_display_form_alter(&$form, &$form_state) {
  //disable the colorbox formatter for the default file display view mode
  if($form['#view_mode'] == 'default') {
    $form['displays']['status']['file_field_media_colorbox'] = NULL;
  }
}  

/**
 * Implements hook_form_FORM_ID_alter().
 * Removes the Colorbox Link formatter from all entities except file type.
 */
function media_colorbox_form_field_ui_display_overview_form_alter(&$form, &$form_state) {
  if($form['#entity_type'] != 'file'){
    foreach($form['#fields'] as $field_name){
      if(isset($form['fields']{$field_name}['format']) && isset($form['fields']{$field_name}['format']['type']['#options']['colorbox_link'])){
        unset($form['fields']{$field_name}['format']['type']['#options']['colorbox_link']);
      }
    }
  }
}  

Kurt Madel's avatar
Kurt Madel committed
/**
 * Implements hook_form_FORM_ID_alter().
Kurt Madel's avatar
Kurt Madel committed
 */
Kurt Madel's avatar
Kurt Madel committed
function media_colorbox_form_field_ui_field_edit_form_alter(&$form, &$form_state) {
  //only want this option to appear for text fields on file entities
Kurt Madel's avatar
Kurt Madel committed
  if ($form['#instance']['entity_type'] == 'file' && ($form['#instance']['widget']['type'] == 'text_textfield' || $form['#instance']['widget']['type'] == 'text_long')) {
    $instance = $form['instance'];
    $entity_type = $instance['entity_type']['#value'];
    $entity_info = entity_get_info($entity_type);
    $bundle = $instance['bundle']['#value'];
    $bundle_label = $entity_info['bundles'][$bundle]['label'];

    // Create the fieldset tab.
Kurt Madel's avatar
Kurt Madel committed
    $form['media_colorbox'] = array(
      '#type'   => 'fieldset',
      '#title'  => t('Colorbox Caption Field'),
      '#description' => t('Set field to be used as the Colorbox caption.
        Note: Selecting this text field as the Colorbox caption field will replace an previously checked field for this file type.'),
      '#tree'   => TRUE,
Kurt Madel's avatar
Kurt Madel committed
    );
    
    //get current caption_field variable if set, use to set default value below
    $current_media_colorbox_caption_field_name = variable_get('media_colorbox_' . $bundle . '_caption_field_name', $default = '');
Kurt Madel's avatar
Kurt Madel committed
    $form['media_colorbox']['caption_field'] = array(
      '#type' => 'checkbox',
      '#title' => t('Use field as Colorbox caption'),
      '#default_value' => $current_media_colorbox_caption_field_name == $form['#instance']['field_name'] ? TRUE : FALSE,
    );

    //check to see if the colorbox file caption field is already set,
    //and if it is set to a different field than the current one
Kurt Madel's avatar
Kurt Madel committed
    if (isset($current_media_colorbox_caption_field_name)
      && $current_media_colorbox_caption_field_name !== $form['#instance']['field_name']) {
      $form['media_colorbox']['override_text'] = array(
        '#markup' => t('The @bundle file type is currently using the @field field as the Colorbox caption field,
        checking it for this field will override that value.',
Kurt Madel's avatar
Kurt Madel committed
          array('@bundle' => $bundle_label, '@field' => $current_media_colorbox_caption_field_name)),
Kurt Madel's avatar
Kurt Madel committed
    }

    //additional submit function to process the caption_field checkbox value
Kurt Madel's avatar
Kurt Madel committed
    $form['#submit'] = array_merge($form['#submit'], array('media_colorbox_form_field_ui_field_edit_form_submit'));
Kurt Madel's avatar
Kurt Madel committed
  }
Kurt Madel's avatar
Kurt Madel committed

/**
 * Custom submit handler for the field_ui_field_edit_form altered form.
 */
Kurt Madel's avatar
Kurt Madel committed
function media_colorbox_form_field_ui_field_edit_form_submit($form, &$form_state) {
  if ($form['media_colorbox']['caption_field']) {
    $bundle = $form['instance']['bundle']['#value'];
Kurt Madel's avatar
Kurt Madel committed
    variable_set('media_colorbox_' . $bundle . '_caption_field_name', $form['#field']['field_name']);
Kurt Madel's avatar
Kurt Madel committed
}