I'm having player inside a views_slideshow so when I start player I need to pause a slideshow.

so I add own preprocess function in custom module

  $vars['config']['events'] = array(
    'onPlay' => 'function () {$("#views_slideshow_cycle_main_front_slideshow-default").cycle("pause"); alert("onPlay");}',
    'onPause' => 'function() {$("#views_slideshow_cycle_main_front_slideshow-default").cycle("resume"); alert("onPause")}',
    'onComplete' => 'function() {$("#views_slideshow_cycle_main_front_slideshow-default").cycle("resume"); alert("onComplete")',
  );

This code does not works in inline mode because events are passed as strings.
In none-inline mode behaviour throws error

jw-events.png

CommentFileSizeAuthor
#1 1447592-events.patch651 bytesandypost
jw-events.png5.58 KBandypost
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

andypost’s picture

Status: Active » Needs review
FileSize
651 bytes

This cause by that setup happens before events are added to config (config contains event handlers as strings not functions)

Patch suggests to add event handlers to config and then call to setup player

rickvug’s picture

Status: Needs review » Fixed

My javascript isn't any good so I'll trust your judgment on this one. I did test to confirm that that player is loading and working fine after the patch is applied. Committed, thanks!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

julianmancera’s picture

Hi Andy,

Would you mind to post the module code function , I'm not sure how to send the variables to the Drupal behaviour.

Thank you

Julian

julianmancera’s picture

Nevermind this is the preprocess function you should use for sending the events

function [your_module_name]_preprocess_jw_player(&$variables) {
	$variables['config']['events'] = array(
			'onComplete' => 'function() { alert("onComplete")}',
	);
}

Julian Mancera

El Alemaño’s picture

Version: 7.x-1.x-dev » 7.x-2.x-dev
Status: Closed (fixed) » Active
function [your_module_name]_preprocess_jw_player(&$variables) {
	$variables['config']['events'] = array(
			'onComplete' => 'function() { alert("onComplete")}',
	);
}

I try to use this function but i am getting an error, because events are passed as strings.
How can i do that? I already use the patch for jw_player.js...

mysticbovine’s picture

When I use the following:

function [your_module_name]_preprocess_jw_player(&$variables) {
$variables['config']['events'] = array(
'onComplete' => 'function() { alert("onComplete")}',
);
}

I get the following outputted. And it causes an error:

SyntaxError: missing } after property list

       jwplayer('jwplayer1commercial').setup({
              "file":"Know.mp4",
              "width":"720",
              "height":"480",
              "stretching":"uniform",
              "autostart":"true",
              "events":{
                 "onComplete":"function() { alert("onComplete")}",
              }
       });

What am I doing wrong?

Thanks

pbuyle’s picture

IMHO, there is no changes needed in the JW Player module. The patch in #1 uses JavaScript eval() of inline code in order to allow JavaScript callbacks to be written in PHP strings. Writing inline JavaScript in PHP string is ugly and error prone, it should be avoided. But the proposed patch actually encourage it. eval() is also incompatible with strict and secure JavaScript, as enforceable using the Content Security Policy HTTP header.

The proper solution would be to include an additional JavaScript file that uses the JW Player JavaScript API on the page containing the view. If the file is added by a theme, it will be included after the jwplayer.js and jw_player.js files. If the file is included by a module, a weight should be set in order to ensure proper ordering of the .js files.

Something like this should do for the additional JavaScript files.

(function($) {
  Drupal.behaviors.nameOfTheCustonModuleOrTheme = {
    attach: function(context, settings) {
      $('#views_slideshow_cycle_main_front_slideshow-default').once('name-of-the-custom-module-or-theme', function() {
        var $this = $(this);
        jwplayer().onPlay(jQuery.proxy($this.cycle, $this, 'pause'));
        jwplayer().onPause(jQuery.proxy($this.cycle, $this, 'resume'));
        jwplayer().onComplete(jQuery.proxy($this.cycle, $this, 'resume'));
      }
    }
  }
})(jQuery);
pbuyle’s picture

Issue summary: View changes
Status: Active » Closed (works as designed)

I'm closing this a 'work as designed' for the reasons explained in #8 (ie. JavaScript code belongs in .js file, not in PHP).