Question

I'm trying to make a block display on a specific page and inside a node type; I got this code from drupal.org, but I keep getting this error.

Warning: array_flip() [function.array-flip]: Can only flip STRING and INTEGER values! in DrupalDefaultEntityController->load() (line 178 of /home/content/45/6861545/html/includes/entity.inc).
Warning: array_flip() [function.array-flip]: Can only flip STRING and INTEGER values! in DrupalDefaultEntityController->cacheGet() (line 354 of /home/content/45/6861545/html/includes/entity.inc).

This is the code I used.

$match = FALSE;
$types = array('middle_content' => 1);
if (arg(0) == 'node' && is_numeric(arg(1))) {
  $nid = arg(1);
  $node = node_load(array('nid' => $nid));
  $type = $node->type;
  if (isset($types[$type])) {
    $match = TRUE;
  }
}

if (substr($_SERVER["REQUEST_URI"], 0, 10) == '/test'){
  $match = TRUE;
}

if (substr($_SERVER["REQUEST_URI"], 0) == "/node/add/middle_content") {
  $match = TRUE;
}

return $match;
Was it helpful?

Solution

To actually fix that error, see my answer to a stackoverflow question.

However, in that case, your code can be simplified a lot using menu_get_object():

$types = array('middle_content' => 1);
if ($node = menu_get_object()) {
  if (isset($types[$node->type])) {
    return TRUE;
  }
}

And anyway, you should avoid PHP in blocks etc (See What are the downsides of using PHP Filter code in blocks, nodes, views-args, etc?), check out Context. It allows you to configure your blocks (among other things) for conditions exactly like this without having to write code. It is also much faster.

OTHER TIPS

The actual error is occurring because node_load() either takes an integer or a numeric array of nids, not an associative array. $node = node_load($nid); is perfectly valid.

That said, Berdir's answer is a better solution.

Licensed under: CC-BY-SA with attribution
Not affiliated with drupal.stackexchange
scroll top