Error: Drupal 7 Core Concepts
In the 3. chapter "How Hooks Work and How to Use Them"
video: "Displaying a block with hook_block_view() and checking permissions with user_access()"
After enabling the module I get this error.
Notice: Undefined variable: output in trails_block_view() (line 146 of /var/www/drupal7/sites/all/modules/custom/trails/trails.module).
The code in the "resource-pack-drupal-7-core-concepts" is not the same as on the video.
I'm not far enough in the learning-curve to know what the message want's to tell me :)
I realize this is an old post, but I wanted to point out that the first error on line 129 still occurs in Step 8's code. It has been fixed in Step 9. This was confusing for me, but I did manage to solve it ON MY OWN after changing the switch to $delta instead of $type. As a complete novice to Drupal and PHP, I was proud of myself. :)
Congratulations, Max! I'll get that error fixed, but it's great you were able to figure out the problem, nice work. :)
Cheers,
Chris
I noticed I kept getting offset error if I was browsing when I had less items in the trails array/variable than what was set in the block configuration. So if number of items is set to 5 (default), and there less than that in the trails array variable, you get the error.
I got around this by checking count of $trails var and if that is less than $num_items, reset $num_items to that. Added after line 142:
$trail_count = count($trail);
// If current trail count is LESS than $num_items, use that instead, to prevent error
if ($trail_count < $num_items) {
$num_items = $trail_count;
}
You can duplicate the error by configuring the block to show a high number of history items, I selected 18. Then, manually run cron. When you view a page that displays the block, mulitple errors will be thrown by...
if($item = $reverse_trail[$i])
I'm assuming this is because you are trying to set $items to an undefinded element of an associative array.
I solved this problem by using an isset check then assigning $item.
for($i =0; $i < $num_items; $i++) {
if($item = $reverse_trail[$i]) {
$output .= '<li>' . l($item['title'], $item['path']) . ' - ' . format_interval(REQUEST_TIME - $item['timestamp']) . ' ' . t('ago') . '</li>';
}
}
//My solution
for($i =0; $i < $num_items; $i++) {
if(isset($reverse_trail[$i])) {
$item = $reverse_trail[$i];
$output .= '<li>' . l($item['title'], $item['path']) . ' - ' . format_interval(REQUEST_TIME - $item['timestamp']) . ' ' . t('ago') . '</li>';
}
}
I'm simply posting this as an alternative solution for others that run into the same error.
Hey there,
Thank you for pointing this out, there have been a couple of minor code fixes in the source code since the videos were shot, but most of the code should be the same. It looks like a couple other fixes need to go in. I'll post back here when they've been fixed, but you should be able to move forward with these.
Just in case you're curious, the first error,
Notice: Undefined offset: 1 in trails_block_view()
, refers to a place where a key is being used in an associative array that doesn't exist. So, using something likemy_array['somekey']
whensomekey
hasn't yet been defined.The second notice,
Notice: Undefined variable
can be fixed by defining the$output
variable before content gets added to it, by doing something like$output = ''
.I'd like to get this code error-free, but the errors also present a good opportunity to learn. ;)
Thanks again for mentioning this!
Cheers,
Chris