sephiith
sephiith

Reputation: 1197

How do I fix a undefined index error in Drupal 7?

Problem

I am getting a "Undefined index: node in include()" notice in Drupal for the below line of code. I've tried the below solution but I am still receiving the error. Any ideas?

Code

$url = drupal_lookup_path('alias', 'node/' . $related['node']->nid);

The solution I tried

isset($related['node']->nid) ? $related['node']->nid : "";

Question

Does anyone know why this error continues to occur?

Upvotes: 0

Views: 416

Answers (1)

osman
osman

Reputation: 635

The error suggests the $related array variable doesn't have a node index.

First, make sure you're retrieving the node object correctly.

Then, perhaps try it like this to avoid errors:

<?php

$url = '';

if (isset($related['node']) && is_object($related['node'])) {
  $nid = $related['node']->nid;
  $url = drupal_lookup_path('alias', "node/$nid");
}

Upvotes: 3

Related Questions