swfobject_api.module

Tracking 6.x-1.x branch
  1. drupal
    1. 6 contributions/swfobject_api/swfobject_api.module

Integrates the SWFObject Javascript library, allowing you to easily embed flash in your website.

Please see the README.txt file for more information on this module.

Functions & methods

NameDescription
swfobject_api_ensure_swfobjectEnsure that the necessary JS files are loaded.
swfobject_api_helpImplementation of hook_help().
swfobject_api_menuImplementation of hook_menu().
swfobject_api_permImplementation of hook_perm().
swfobject_api_settings_formMenu callback: SWFObject API configuration form.
swfobject_api_themeImplementation of hook_theme().
theme_swfobject_apiTheme function to add a flash file to the page.

File

View source
  1. <?php
  2. /**
  3. * @file
  4. * Integrates the SWFObject Javascript library, allowing you to easily
  5. * embed flash in your website.
  6. *
  7. * Please see the README.txt file for more information on this module.
  8. */
  9. /* *********************************************** */
  10. /* Drupal Hook Functions */
  11. /* *********************************************** */
  12. /**
  13. * Implementation of hook_help().
  14. */
  15. function swfobject_api_help($path, $arg) {
  16. switch ($path) {
  17. case 'admin/help#swfobject_api':
  18. case 'admin/settings/swfobject_api':
  19. return t('This module creates an API for flash content generation in pages based on the swfObject library. For more information, visit <a href="http://code.google.com/p/swfobject/">http://code.google.com/p/swfobject/</a>.');
  20. break;
  21. }
  22. }
  23. /**
  24. * Implementation of hook_perm().
  25. */
  26. function swfobject_api_perm() {
  27. return array('administer swfobject');
  28. }
  29. /**
  30. * Implementation of hook_menu().
  31. */
  32. function swfobject_api_menu() {
  33. $items = array();
  34. $items['admin/settings/swfobject_api'] = array(
  35. 'title' => 'SWFObject API',
  36. 'page callback' => 'drupal_get_form',
  37. 'page arguments' => array('swfobject_api_settings_form'),
  38. 'access arguments' => array('administer swfobject'),
  39. );
  40. return $items;
  41. }
  42. /**
  43. * Implementation of hook_theme().
  44. * @return array
  45. */
  46. function swfobject_api_theme() {
  47. return array(
  48. 'swfobject_api' => array(
  49. 'arguments' => array(
  50. 'url' => NULL,
  51. 'params' => NULL,
  52. 'vars' => NULL,
  53. 'id' => NULL,
  54. 'attributes' => NULL
  55. )
  56. )
  57. );
  58. }
  59. /* *********************************************** */
  60. /* Configuration Functions */
  61. /* *********************************************** */
  62. /**
  63. * Menu callback: SWFObject API configuration form.
  64. * @return array
  65. * drupal form array
  66. */
  67. function swfobject_api_settings_form() {
  68. $form = array();
  69. $form['swfoa_settings_version'] = array(
  70. '#type' => 'textfield',
  71. '#title' => t('Default minimum Flash version required'),
  72. '#description' => t('This value can be overridden via the theme call.'),
  73. '#default_value' => variable_get('swfoa_settings_version', '6'),
  74. '#required' => TRUE,
  75. );
  76. $form['swfoa_settings_express'] = array(
  77. '#type' => 'checkbox',
  78. '#title' => t('Enable express install.'),
  79. '#description' => t('Express install allows player upgrades without having to leave the site. Only versions 6.0.65 and above are supported.'),
  80. '#default_value' => variable_get('swfoa_settings_express', FALSE),
  81. );
  82. $form['swfoa_settings_path'] = array(
  83. '#type' => 'textfield',
  84. '#title' => t('Path to SWFObject JS.'),
  85. '#description' => t('Path to the SWFObject JS file.'),
  86. '#default_value' => variable_get('swfoa_settings_path', drupal_get_path('module', 'swfobject_api') .'/swfobject.js'),
  87. );
  88. return system_settings_form($form);
  89. }
  90. /* *********************************************** */
  91. /* Internal Functions */
  92. /* *********************************************** */
  93. /**
  94. * Ensure that the necessary JS files are loaded.
  95. *
  96. * This function automatically checks to make sure that the JS files are only
  97. * loaded once.
  98. * @param array $settings
  99. */
  100. function swfobject_api_ensure_swfobject($settings = array()) {
  101. static $done = FALSE;
  102. if (! $done) {
  103. // Load the swfobject library and our add-on to activate it.
  104. drupal_add_js(variable_get('swfoa_settings_path', drupal_get_path('module', 'swfobject_api') .'/swfobject.js'), 'module', 'header');
  105. // Load the JS which creates loads the params into the page
  106. drupal_add_js(drupal_get_path('module', 'swfobject_api') .'/swfobject_api.js', 'module', 'footer');
  107. $done = TRUE;
  108. }
  109. drupal_add_js($settings, 'setting', 'footer', FALSE, TRUE, FALSE);
  110. }
  111. /* *********************************************** */
  112. /* Theme Functions */
  113. /* *********************************************** */
  114. /**
  115. * Theme function to add a flash file to the page.
  116. *
  117. * The actual returned HTML from this function is minimal. Its main purpose is
  118. * to setup the Javascript properly on content which is cached by input filters
  119. *
  120. * @ingroup themeable
  121. * @param $url
  122. * A web accessible url to the flash file.
  123. * @param $params
  124. * An associative array of parameters that describe the SWF.
  125. * @param $vars
  126. * An associative array of variables to pass through to the SWF flashvars value.
  127. * @param $id
  128. * An id to appened to the so object.
  129. * @param $attributes
  130. * Associative array of attributes to apply to the resulting flash tag (embed, object, etc.)
  131. */
  132. function theme_swfobject_api($url, $params = array(), $vars = array(), $id = NULL, $attributes = array()) {
  133. // keep track of each div ID
  134. static $swfobject_id = 1;
  135. // Build the base params.
  136. $base_params = array(
  137. 'width' => '100%',
  138. 'height' => '100%',
  139. 'no_flash' => t('Sorry, you need to install flash to see this content.'),
  140. 'version' => variable_get('swfoa_settings_version', '6'),
  141. 'type' => 'movie',
  142. 'bgcolor' => '#FFFFFF',
  143. 'express_redirect' => variable_get('swfoa_settings_express', TRUE) ? drupal_get_path('module', 'swfobject_api') .'/expressinstall.swf' : 'false',
  144. 'class' => '',
  145. );
  146. // Merge in default parameters.
  147. $params += $base_params;
  148. // Increment the div id to allow for multiple players on the page
  149. $div_id = empty($id) ? 'swfobject-'. $swfobject_id++ : $id;
  150. // are the no_flash parameters being passed with a filter?
  151. if (is_array($params['no_flash'])) {
  152. $params['no_flash'] = check_markup($params['no_flash']['text'], $params['no_flash']['filter']);
  153. }
  154. // assign param data to the specific parameters
  155. $height = $params['height'];
  156. $width = $params['width'];
  157. $express_redirect = $params['express_redirect'];
  158. $version = $params['version'];
  159. $bgcolor = $params['bgcolor'];
  160. $no_flash = $params['no_flash'];
  161. $class = implode(' ', array($params['class'], 'swfobject'));
  162. unset($params['height'], $params['width'], $params['express_redirect'], $params['version'], $params['bg_color'], $params['no_flash'], $params['class']);
  163. $settings['swfobject_api']['files'][$div_id] = array(
  164. 'url' => $url,
  165. 'params' => $params,
  166. 'flashVars' => $vars,
  167. 'attributes' => $attributes,
  168. 'height' => $height,
  169. 'width' => $width,
  170. 'express_redirect' => $express_redirect,
  171. 'version' => $version,
  172. 'bgcolor' => $bg_color,
  173. );
  174. // add the JS to the page
  175. swfobject_api_ensure_swfobject($settings);
  176. // Return the placeholder HTML that will normally get
  177. // replaced with flash content.
  178. return "<div id=\"$div_id\" class=\"{$class}\">{$no_flash}</div>\n";
  179. }