You are here

function swfobject2_swftools_embed in SWF Tools 5

Same name and namespace in other branches
  1. 6 swfobject2/swfobject2.module \swfobject2_swftools_embed()
  2. 6.2 swfobject2/swfobject2.module \swfobject2_swftools_embed()

Implementation of swftools_embed hook Returns the markup for the page, plus set necessary javascript.

$methods and $vars should never be empty - unless the only reason for this call is to push the javascript into the header of the page in which case you don't add any paramters as all. This is useful for filtered nodes where the body is not regenerated every time.

File

swfobject2/swfobject2.module, line 34

Code

function swfobject2_swftools_embed($action = 'add_js', $methods = FALSE, $vars = FALSE, $html_alt = '') {

  // Set flag to indicate if the javascript has been added to the header
  static $swfobject2_has_run;

  // Output javascript to the header to load the SWF Object code
  if (!$swfobject2_has_run) {

    // Add swfobject.js
    drupal_add_js(swftools_get_player_path() . '/swfobject2/swfobject.js');
    $swfobject2_has_run = TRUE;
    if ($action == 'add_js') {

      // Exit early having put the javascript in the header.
      return;
    }
  }

  // Initialise a counter to give each div a unique id
  static $unique_id = 0;

  // If a specific id has been passed used this, otherwise generate one by combining time and $unique_id
  if ($vars->params['id']) {
    $id = $vars->params['id'];
    unset($vars->params['id']);
  }
  else {
    $unique_id++;
    $id = 'swfobject2-id-' . time() . $unique_id;
  }

  // Anything in $vars->params will be output - we don't want src_path
  unset($vars->params['src_path']);

  // Unset flashvars string - we can use the array format and let drupal_to_js handle things
  unset($vars->params['flashvars']);

  // Generate js string ready for output to the page header
  // swfObject takes parameters swfURL, id, width, height, version, expressinstall, flashvars, params, attributes
  // At the moment expressInstall isn't enabled
  $swf_js = t('swfobject.embedSWF("!url", "!id", "!width", "!height", "!version", "", !flashvars, !params, !attributes);', array(
    '!url' => $vars->params['src'],
    '!id' => $id,
    '!width' => $vars->params['width'],
    '!height' => $vars->params['height'],
    '!version' => $vars->params['version'],
    '!flashvars' => drupal_to_js($vars->flashvars),
    '!params' => drupal_to_js($vars->params),
    '!attributes' => drupal_to_js(array(
      'id' => 'swf' . $id,
    )),
  ));

  // Generate the html markup ready to receive the substitution
  $html = '<div id="' . $id . '" class="swftools swfobject2">' . "\n";
  $html .= $html_alt . "\n";
  $html .= '</div>' . "\n";

  // Although SWF Object 2 recommends adding js to the page header that will prevent cached filters being used
  // So we add js to the page body in order that it gets cached too
  $html .= '<script type="text/javascript">' . "\n";
  $html .= $swf_js . "\n";
  $html .= '</script>' . "\n";

  // Return html markup
  return $html;
}