2

I have a small problem with altering js code loaded from custom module. I need to change the js script from a module for a custom page

Module code code :

function module_script_src($environment) {
  $script = '';
  $script_id = variable_get('script_id', '');
  $script_hash = variable_get('script_hash', '');
  switch ($environment) {
    case 'prod':
      if ($script_id && $script_hash) {
        $script = '//example.com/' . $script_id . '/lib-' . $script_hash . '.js';
      }
      break;

    case 'dev':
      if ($adobe_dtm && $site_hash) {
        $script = '//example.com/' . $script_id . '/lib-dev' . $script_hash . '.js';
      }
      break;

  }
  return $script;
}

The goal is to change $script just for one custom page, i try to add case for my custom page, simply new case to module

 case 'custom page':
       if ($node && $node->nid = "my_node_id"):
     $script = '//example.com/' . $script_id . '/custom-' . $script_hash . '-.js';
  }
      break;

But it does not work, if it possible in module to load custom is for the specific custom page?

2

In the switch statement you provide, you are switching over $environment variable. It sounds like it will never be passed a URL or 'custom page' value.

You are also trying to use $node object but you have not loaded it from your code. You are also not passing $node as an argument. Also an $adobe_dtm variable is used but never defined. Plus, you are using = to do an equality check, you need == or ===. = is assigning 'my_node_id' to your variable instead of compare it.

In order to have a $node object present in this function, you need to do one of two things to get it:

  • $node = menu_get_object('node');
  • $node = node_load(arg(1));

Then you can just add this after the switch statement:

function module_script_src($environment) {
  $script = '';
  $script_id = variable_get('script_id', '');
  $script_hash = variable_get('script_hash', '');

  switch ($environment) {
    case 'prod':
      if ($script_id && $script_hash) {
        $script = '//example.com/' . $script_id . '/lib-' . $script_hash . '.js';
      }
      break;

    case 'dev':
      if ($adobe_dtm && $site_hash) {
        $script = '//example.com/' . $script_id . '/lib-dev' . $script_hash . '.js';
      }
      break;

  }

  if ($node = menu_get_object('node')) {
    if ($node->nid == "my_node_id"):
      $script = '//example.com/' . $script_id . '/custom-' . $script_hash . '-.js';
    }
  }

  return $script;
}

You may need to think through the cases involved in building the script value. It looks like the switch statement is not even necessary, because $script value is basically the same - in which case you can clean up the code by simplifying it.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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