1

I'm looking to implement a simple conditional within a template file (Drupal 7) that checks the URL path and only operates on the condition when the string is exact/page is the parent. At the moment, I have a conditional working, but the condition still executes for sub pages of the URL string when matched. E.g:

$url_vars = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (strpos($url_vars, 'parent-page') !== false) {
// Do something
}

This still operates for anything hierarchical, so:

parent-page/sub-page

How do I make this work so that the condition strictly manages the parent directory only and not the sub-directory? Any guidance or ideas would be of great help. I'd prefer to handle this server-side, and not on the front-end.

Thanks! Mark.

m918273
  • 110
  • 2
  • 11

3 Answers3

1

I am not exactly sure how much your REQUEST_URI string will vary, but this may suffice:

if(strpos($_SERVER['REQUEST_URI'],'parent-page')!==false && strpos($_SERVER['REQUEST_URI'],'parent-page/')===false){

This just checks that the parent_page (static string) exists in the REQUEST_URI and that it DOES NOT have a trailing /

If this doesn't do the trick, please offer more specific details about your URI values.

A potentially useful reference: https://stackoverflow.com/a/4731027/2943403

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
0

You can explode the url with 'parent-page' and count the array to see if it's sublevel.

$arr = explode("parent-page", $url_vars);
If(count($arr) <= 1) {
      // Parent-page
}

Since explode removes 'parent-page' and creates an array of the rest you should end up with one item that holds "http://".
If you get a second item that is your subpage.

Andreas
  • 23,610
  • 6
  • 30
  • 62
-1

$_GET['q'] variable should contains current page path. Try using it instead of your $url_vars.

For some more advanced usage check on Context module:

https://www.drupal.org/project/context

MilanG
  • 6,994
  • 2
  • 35
  • 64