Answers:
您应该使用以下代码,作为url()
返回作为参数传递的路径的URL;因此,它返回IF语句认为等同的值TRUE
(字符串为"0"
或为空字符串的情况除外)。
if ($_GET['q'] == 'the/internal/Drupal/path') {
// Do stuff
}
Drupal的别名是您想要的
<?php
$path = drupal_get_path_alias($_GET['q']);
if ($path == 'the/path') {
// do stuff
}
?>
以下其他:
完整网址
<?php
global $base_root;
$base_root . request_uri()
?>
Drupal的“内部” URL
<?php
$arg = arg();
// Path of 'node/234' -> $arg[0] == 'node' && $arg[1] == 234
?>
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(过程性)
// 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) {
}
}
}
来源:arg()已过时,将在Drupal.org 上删除。
您是否尝试过request_uri()?
http://api.drupal.org/api/drupal/includes--bootstrap.inc/function/request_uri/6
您将要使用arg()函数。您可以使用以下两种方法之一,
$args = arg();
基本上,这将为您提供一个数组,每个url参数是另一个值,或者您可以像这样检查特定的参数:
$arg = arg(0);
因此,对于您的示例,您可以执行以下操作:
if(!is_null(arg(1)) && arg(0) == 'the' && arg(1) == 'path') { Do something }
或者我建议这样做:
$args = arg();
if(!empty($args[1]) && $args[0] == 'the' && $args[1] == 'path') { Do something }
$arg = implode('/',arg());
如果您不想为具有特定URL的页面使用其他页面模板,则可以使用以下代码检查当前URL。
if (arg(0) == 'the' && arg(1) == 'path') {
// Add the extra CSS class.
}
url()不是返回当前页面URL的函数;如果在url()
不提供任何参数的情况下进行调用,您将(至少在Drupal 7上且未实现任何模块hook_ulr_outbound_alter()
)获得Drupal安装的基本URL。如果没有模块更改该函数返回的值,则
调用url('the/path')
只会返回您"the/path"
。这意味着将始终执行您显示的代码,并始终添加CSS类。
如果您正在寻找Drupal服务的规范路径,则可以点击$ _GET ['q'],即使Drupal使用干净的URL,它也应该翻译。
Drupal 8:
arg()替代,因为它不再起作用:
$path_args = explode('/', current_path());
print $path_args[1];