开发者

Drupal PHP block visibility rules

In a Drupal 开发者_运维百科block's Page Visibility Settings I'd like to prevent a certain block from showing if the second value in the path is a number. This does not seem to be working for me. Cheers.

Show block ONLY when arguments are:

domain.com/video/one (arg 0 is 'video' and arg 1 is present and NOT a number)

Don't show:

domain.com/video

domain.com/video/1

<?php
  if (arg(0) == 'video' && is_nan(arg(1)) && empty(arg(2))) {
    return TRUE; 
  }
  else {
    return FALSE;
  }
?>


I'm assuming this is in a hook_block/hook_block_view function? You could try a different approach:

if (preg_match('/^video\/[0-9]+$/', $_GET['q'])) {
  // Path has matched, don't show the block. Are you sure you should be returning TRUE here?
  return TRUE; 
}
else {
  // Path has matched, go ahead and show the block  
  return FALSE;
}


You can simply use the following code:

<?php
  $arg1 = arg(1);
  $arg2 = arg(2);
  // Check arg(1) is not empty, or is_numeric() returns TRUE for NULL.
  return (arg(0) == 'video' && !empty($arg1) && !is_numeric($arg1) && empty($arg2));
?>

As KingCrunch already said, is_nan() doesn't return TRUE when its argument is a number. The code you reported contains another error too: empty() can be used only with variables, as reported in the PHP documentation.

empty() only checks variables as anything else will result in a parse error. In other words, the following will not work: empty(trim($name)).

The code I reported shows the block for paths like "video/video1"; if you want to show the block also for paths such as "video/video1/edit", then the following code should be used.

<?php
  $arg1 = arg(1);
  return (arg(0) == 'video' && !empty($arg1) && !is_numeric($arg1));
?>

Using arg() doesn't work if the path you are looking for is a path alias. Suppose that "video/video1" is a path alias for "node/10"; in that case arg(0) will return "node," and arg(1) will return "10." The same is true for $_GET['q'] that will be equal to "node/10."

This happens because Drupal, during its bootstrap, initialize $_GET['q'] with the following code:

  // Drupal 6.
  if (!empty($_GET['q'])) {
    $_GET['q'] = drupal_get_normal_path(trim($_GET['q'], '/'));
  }
  else {
    $_GET['q'] = drupal_get_normal_path(variable_get('site_frontpage', 'node'));
  }
  // Drupal 7.
  if (!empty($_GET['q'])) {
    $_GET['q'] = drupal_get_normal_path($_GET['q']);
  }
  else {
    $_GET['q'] = drupal_get_normal_path(variable_get('site_frontpage', 'node'));
  }

If you what you are checking is a path alias, then you should use the following code:

// Drupal 6.
$arg = explode('/', drupal_get_path_alias($_GET['q']);
return (arg[0] == 'video' && !empty($arg[1]) && !is_numeric(arg[1]) && empty($arg[2]));
// Drupal 7.
$arg = explode('/', drupal_get_path_alias();
return (arg[0] == 'video' && !empty($arg[1]) && !is_numeric(arg[1]) && empty($arg[2]));


Dont know, what your arguments looks like, but I assume you mixed up two kinds of types. is_nan() only works with numbers. If you want to test, if a value is a number,

var_dump(is_numeric(arg(1));

is_nan() tests, if a "numeric" value is a concrete value or "not a number" like "infinite" or the result of "0/0" or such.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜