You are here

cck_video.module in CCK Video 7

Same filename and directory in other branches
  1. 6 cck_video.module

File

cck_video.module
View source
<?php

/**
 * Menu callback. Prints a listing of active nodes on the site.
 */

/**
 * @todo Please document this function.
 * @see http://drupal.org/node/1354
 */
function cck_video_menu() {
  $items = array();
  $items['admin/config/media/cck_video'] = array(
    'title' => 'CCK Video settings',
    'description' => 'CCK Video',
    'access arguments' => array(
      'administer cck_video',
    ),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'cck_video_admin_settings',
    ),
    'type' => MENU_NORMAL_ITEM,
    'file' => 'cck_video.settings.inc',
  );
  return $items;
}

//==========================================//

// DEFINING A FORMATTER

//==========================================//

/**
 * Implementation of hook_field_formatter_info().
 *
 * All fields should have a 'default' formatter.
 * Any number of other formatters can be defined as well.
 * It's nice for there always to be a 'plain' option
 * for the raw value, but that is not required.
 *
 */

// CONTENT_HANDLE_CORE:   CCK will pass the formatter a single value.
// CONTENT_HANDLE_MODULE: CCK will pass the formatter an array of all the values.
// None of CCK's core formatters use multiple values, that is an option available to other modules that want it.

/**
 * @todo Please document this function.
 * @see http://drupal.org/node/1354
 */
function cck_video_field_formatter_info() {
  $formatters = array(
    'video' => array(
      'label' => t('Video'),
      'field types' => array(
        'file',
      ),
      'settings' => array(
        'cck_video_player' => 'swfobject',
      ),
    ),
  );
  return $formatters;
}

/**
 * Implements hook_field_formatter_settings_form().
 */
function cck_video_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {

  //This gets the view_mode where our settings are stored
  $display = $instance['display'][$view_mode];

  //This gets the actual settings
  $settings = $display['settings'];
  $element = array();

  //Add your select box
  $element['cck_video_player'] = array(
    '#type' => 'select',
    '#title' => t('Player'),
    '#default_value' => $settings['cck_video_player'],
    // Get the value if it's already been set
    '#options' => array(
      'flowplayer' => 'flowplayer',
      'swfobject' => 'swfobject',
    ),
  );
  return $element;
}

/**
 * Implements hook_field_formatter_settings_summary().
 */
function cck_video_field_formatter_settings_summary($field, $instance, $view_mode) {
  $display = $instance['display'][$view_mode];
  $settings = $display['settings'];
  $summary = t('Selected player: @cck_video_player', array(
    '@cck_video_player' => $settings['cck_video_player'],
  ));
  return $summary;
}

/**
 * Implements hook_theme().
 */
function cck_video_theme() {
  $theme = array(
    'cck_video_formatter_video' => array(
      'render element' => 'element',
    ),
  );
  return $theme;
}

/**
 * Implements hook_field_formatter_view().
 */
function cck_video_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  global $base_path, $cck_video_init;
  $element = array();
  $settings = $display['settings'];
  $player_name = $settings['cck_video_player'];
  foreach ($items as $delta => $item) {
    $file_name = $item['filename'];
    $file_video = file_create_url($item['uri']);
    $file_ext = substr($file_video, -3);
    if ($file_ext == 'm3u8') {
      $video_type = 'application/x-mpegurl';
    }
    if ($file_ext == 'webm') {
      $video_type = 'video/webm';
    }
    if ($file_ext == 'mp4') {
      $video_type = 'video/mp4';
    }
    if ($file_ext == 'ogg') {
      $video_type = 'video/ogg';
    }
    if ($file_ext == 'flv') {
      $video_type = 'video/flash';
    }
    $cck_video_image_ext = variable_get('cck_video_image_ext', 'jpg,bmp,png');
    $cck_video_video_ext = variable_get('cck_video_video_ext', 'flv,swf,mp4,wmv');
    $cck_video_width = variable_get('cck_video_width', '570');
    $cck_video_height = variable_get('cck_video_height', '334');
    if (in_array($file_ext, explode(',', $cck_video_image_ext))) {
      $output = '<div class="cck-video-img">';
      $output .= '<img src="' . $file_video . '" />';
      $output .= '</div>';
    }
    elseif (in_array($file_ext, explode(',', $cck_video_video_ext))) {
      if ($player_name == 'flowplayer') {
        $player_path = drupal_get_path('module', 'cck_video') . '/players/flowplayer-5.4.2';
        $player_swf = $player_path . '/flowplayer.swf';
        drupal_add_js($player_path . '/flowplayer.min.js');
        drupal_add_css($player_path . '/skin/minimalist.css');
        $output = '
          <div class="flowplayer">
            <video><source type="' . $video_type . '" src="' . $file_video . '"></video>
          </div>';

        /*
        $output = '<a href="' . $file_video . '" style="display:block;width:425px;height:300px;" class="player"></a>';

        if (!$cck_video_init) {
          drupal_add_js('flowplayer("a.player", "' . $player_swf . '", {clip: {autoPlay: false}});',
                        array('type' => 'inline', 'scope' => 'footer'));
          $cck_video_init = TRUE;
        }
        */
      }
      elseif ($player_name == 'swfobject') {
        $player_path = drupal_get_path('module', 'cck_video') . '/players/swfobject-2.2';
        drupal_add_js($player_path . '/swfobject.js');
        drupal_add_css(drupal_get_path('module', 'cck_video') . '/cck_video.css');
        drupal_add_js('swfobject.registerObject("' . $file_name . '", "9.0.0");', array(
          'type' => 'inline',
          'scope' => JS_DEFAULT,
        ));

        // STATIC only
        $output = '
          <div id="cck_video">
  			  <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
            width="' . $cck_video_width . '" height="' . $cck_video_height . '" id="' . $file_name . '">
  			  	<param name="movie" value="' . $file_video . '" />
  			  	<param name="play" value="true" />
  			  	<param name="loop" value="true" />
  			  	<param name="wmode" value="opaque" />
  			  	<!--[if !IE]>-->
  			  	<object type="application/x-shockwave-flash" data="' . $file_video . '"
              width="' . $cck_video_width . '" height="' . $cck_video_height . '">
  			  		<param name="play" value="true" />
  			  		<param name="loop" value="true" />
  			  		<param name="wmode" value="opaque" />
  			  	<!--<![endif]-->
  			  		<a href="http://www.adobe.com/go/getflashplayer">
  			  			<img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" />
  			  		</a>
  			  	<!--[if !IE]>-->
  			  	</object>
  			  	<!--<![endif]-->
  			  </object>
  		    </div>
        ';
      }
    }
    else {
      $output = '<div class="cck-video-vid">';
      $output .= '<a href="' . $file_video . '">' . $file_name . '</a>';
      $output .= '</div>';
    }
    $element[$delta]['#markup'] = $output;
  }
  return $element;
}