I am developing a module (see here for details Mixing PHP variables and JS variables in output) this shows multiple uses of the same javascript function, as follows:

$key     = $params->get('key');
$secret  = $params->get('secret');
$domain  = $params->get('domain');
$mediaid = $params->get('mediaid');
$expires = $params->get('expires');

function get_signed_player($key, $mediaid, $domain, $secret, $expires)
    $path      = 'players/' . $key . '-' . $mediaid . '.js';
    $signature = md5($path . ':' . $expires . ':' . $secret);
    $url       = 'http://' . $domain . '/' . $path . '?exp=' . $expires . '&sig=' . $signature;

    return $url;

echo '<p>Watch this cool video:</p>';
echo '<script src="' . get_signed_player($key, $mediaid, $domain, $secret, $expires) . '"></script>';

However, when I assign more than one module to a page the page crashes, I'm thinking due to the usage of a duplicate javascript function.

I'm hoping I can assign an ID to the function name. I was thinking the easiest way would be to echo the module ID using

echo $module->id;

but if I add this to the function name, it still breaks. Ideally I want something along these lines:

function get_signed_player' . $module->id . '($playerkey, $videokey, $domain, $secret, $expires)

Which I then call for the frontend:

echo '<script src="' . get_signed_player' . $module->id . '($playerkey, $videokey, $domain, $secret, $expires) . '"></script>';
  • Have you assigned different or the same (secret, playerkey, etc) parameter values for each module instance? Which 3rd party video API are you retrieving here.
    – Lodder
    Commented Mar 22, 2016 at 13:21
  • The key, secret, domain and expires are the same. The only difference is the mediaid Commented Mar 22, 2016 at 14:28
  • I've never used the JW Platform before to be honest. Would you be able to come over to the Joomla Chat group for a bit? chat.stackexchange.com/rooms/14071/joomla
    – Lodder
    Commented Mar 22, 2016 at 14:38

1 Answer 1


Resolved, so will post the answer as a reference.

The issue was that the function belonged in the default.php, which is the view file and should be used for the output.

I moved the function to a class in the helper like so:

class ModJWPlayerwithURLTokenSigningHelper
    public function get_signed_player($playerkey, $videokey, $domain, $secret, $expires)
        $path = 'players/' . $videokey . '-' . $playerkey . '.js';
        $sig  = md5($path . ':' . $expires . ':' . $secret);
        $url  = 'http://' . $domain . '/' . $path . '?sig=' . $sig . '&exp=' . $expires;

        return $url;

In the mod_module.php, called the helper.php:

// Get Helper
require_once dirname(__FILE__) . '/helper.php';
$helper = new ModJWPlayerwithURLTokenSigningHelper();

Then in the default.php, called the function like so:

echo '<script src="' . $helper->get_signed_player($playerkey, $videokey, $domain, $secret, $expires) . '"></script>';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.