2

I want to add some CSS and JS to multiple matched wildcard URL patterns. According to Add javascript to multiple specific urls, I can use a wildcard using drupal_match_path() and that works well for one pattern. However, I am trying to use a few patterns to add the same JS and CSS. So far I've tried these:

  // By path matching wildcard patterns.
  $path = drupal_get_path_alias();
  $pattern = 'portfolio/* || photo-blog';

and

  $path = drupal_get_path_alias();
  $pattern = 'portfolio/*, photo-blog';

and

  $pattern = 'portfolio/*' 'photo-blog';
  $path = drupal_get_path_alias();

... but none of these work. I am guessing it may not be possible in the context of:

if (drupal_match_path($path, $pattern)) {
// add JS and CSS based on above
  }

2 Answers 2

8

The API page you've linked describes the format of the $patterns argument:

$patterns: String containing a set of patterns separated by \n, \r or \r\n.

These are types of carriage returns, in different Operating Systems.

Sooo .... did you try:

 $pattern = "portfolio/*\nphoto-blog";
 // or
 $pattern = "portfolio/*\rphoto-blog";
 // or
 $pattern = "portfolio/*\r\nphoto-blog";

EDIT:

 // for platform portability heh
 $pattern = "portfolio/*" . PHP_EOL . "photo-blog";
0
1

This one got me so many times - drupal_match_path() is expecting a newline character to separate the paths. Add this function to your project and never look back:

function _get_path_pattern_string(array $items) {
  return implode(PHP_EOL, $items);
}

Example usage (overly verbose):

$patterns = array('portfolio/*', 'photo-blog', 'something-else');
$pattern_string = _get_path_pattern_string($patterns);
if (drupal_match_path($path, $pattern_string)) {
  // Do something...
}
2
  • 1
    hmmm ... return implode(PHP_EOL, $items)' ?
    – tenken
    Commented Apr 24, 2013 at 15:45
  • @tenken Yep stupid mistake, thanks for pointing that out
    – Clive
    Commented Apr 24, 2013 at 15:49

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.