10

I would like to add a body class in D6 if you are on a specific page(s) currently in my template.php I have:

if url('the/path') {
 add the body class...
}

but that function doesn't seem to be working for me.

6

You should use the following code, as url() returns the URL for the path passed as argument; therefore, it returns a value that the IF-statement would consider equivalent to TRUE (except in the case the string is "0" or is an empty string).

if ($_GET['q'] == 'the/internal/Drupal/path') {
  // Do stuff
}
  • You might also need drupal_get_path_alias($_GET['q']) to check for source/destination path aliases. – cam8001 Mar 25 '11 at 3:33
12

Drupal's Alias is what your looking for

<?php
$path = drupal_get_path_alias($_GET['q']);
if ($path == 'the/path') {
  // do stuff
}
?>

Others Below:

Full URL

<?php
global $base_root;
$base_root . request_uri()
?>

Drupal's "Internal" URL

<?php
$arg = arg();
// Path of 'node/234' -> $arg[0] == 'node' && $arg[1] == 234
?>
  • arg(); is the one for me. cheers ;) – Daithí Jun 23 '12 at 21:18
4

Drupal 7

// Retrieve an array which contains the path pieces.
$path_args = arg();

// Check if the current page is admin.
if (arg(0) == 'admin') {
  // This is wrong even in D7. path_is_admin() should be used instead.
}

// Conditionally add css or js in certain page.
function mymodule_page_build(&$page) {
  if (arg(0) == 'sth' && arg(1) == 'else') {
    drupal_add_css(drupal_get_path('module', 'mymodule') . '/css/my.css');
  }
}

// Load the current node.
if (arg(0) == 'node' && is_numeric(arg(1))) {
  // This is wrong even in D7. menu_get_object() should be used instead.
}

Drupal 8 (procedural)

// Retrieve an array which contains the path pieces.
$path_args = explode('/', current_path());

// Check if the current page is admin.
if (\Drupal::service('router.admin_context')->isAdminRoute(\Drupal::routeMatch()->getRouteObject())) {
}

// Conditionally add css or js in certain page.
function mymodule_page_build(&$page) {
  if (\Drupal::routeMatch()->getRouteName() == 'my.route') {
    $page['#attached']['css'][] = drupal_get_path('module', 'mymodule') . '/css/my.css';
  }
}

// Load the current node.
$node = \Drupal::routeMatch()->getParameter('node');
if ($node) {
}

Drupal 8 (OOP)

use Symfony\Component\HttpFoundation\Request;
use Drupal\Core\Routing\AdminContext;
use Drupal\Core\Routing\RouteMatchInterface;
class myClass {
  public function __construct(Request $request, AdminContext $admin_context, RouteMatchInterface $route_match) {
    $this->request = $request;
    $this->adminContext = $admin_context;
    $this->routeMatch = $route_match;
  }

  public function test() {
    // This might change in https://drupal.org/node/2239009
    $current_path = $this->request->attributes->get('_system_path');
    // Retrieve an array which contains the path pieces.
    $path_args = explode('/', $current_path);

    // Check if the current page is admin.
    if ($this->adminContext->isAdminRoute($this->routeMatch->getRouteObject())) {
    }

    // Load the current node.
    $node = $this->routeMatch->getParameter('node');
    if ($node) {
    }
  }
}

Source: arg() is deprecated and will be removed at Drupal.org

2

Have you tried request_uri()?

http://api.drupal.org/api/drupal/includes--bootstrap.inc/function/request_uri/6

  • Not sure what the 'best' way is, but this always worked for me. – stefgosselin Dec 9 '11 at 20:18
2

You'll want to use the arg() function. You can use it one of two ways,

$args = arg();

Which will basically give you an array with each url argument being another value, or you can check specific arguments like so:

$arg = arg(0);

So for your example, you could do:

if(!is_null(arg(1)) && arg(0) == 'the' && arg(1) == 'path') { Do something  }

or I'd recommend this:

$args = arg();
if(!empty($args[1]) && $args[0] == 'the' && $args[1] == 'path') { Do something  }
  • You could also use $arg = implode('/',arg()); – tim.plunkett Mar 17 '11 at 19:07
1

If you don't want to have a different page template for pages with a specific URL, you can check the current URL using the following code.

if (arg(0) == 'the' && arg(1) == 'path') {
  // Add the extra CSS class.
}

url() is not the function that returns the URL of the current page; if you call url() without providing any parameters, you would get (at least on Drupal 7, and without any modules implementing hook_ulr_outbound_alter()) the base URL of the Drupal installation.
Calling url('the/path') will just return you "the/path", if no module is altering the value returned from the function; that means that the code you have shown will always be executed, and the CSS class always added.

0

If you're looking for the canonical path that Drupal's serving a request for, you can hit $_GET['q'], which should translate even if Drupal's using clean URLs.

0

Drupal 8:

arg() alternative since it no longer works:

$path_args = explode('/', current_path());
print $path_args[1];

Read more: https://www.drupal.org/node/2274705

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.