Error: Drupal 7 Core Concepts

5 replies [Last post]
LaTulipeBlanche
Offline
Last seen: 7 years 6 weeks ago
Joined: 2011-05-13

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 offset: 1 in trails_block_view() (line 129 of /var/www/drupal7/sites/all/modules/custom/trails/trails.module).
    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 :)

Chris Shattuck
Offline
Last seen: 3 weeks 4 days ago
Joined: 2009-08-16
Hey there, Thank you for

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 like my_array['somekey'] when somekey 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

maxricketts
Offline
Last seen: 7 years 12 weeks ago
Joined: 2011-09-03
Error Still Exists

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. :)

Chris Shattuck
Offline
Last seen: 3 weeks 4 days ago
Joined: 2009-08-16
Congratulations, Max! I'll

Congratulations, Max! I'll get that error fixed, but it's great you were able to figure out the problem, nice work. :)

Cheers,
Chris

thund3rbox
Offline
Last seen: 6 years 31 weeks ago
Joined: 2012-05-09
Error if array less than $num_items

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:

// Grab number of items in trail array currently
$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;
}

trevor1
Offline
Last seen: 4 years 25 weeks ago
Joined: 2014-01-01
I too ran into this error

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.

//Old Code
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.

Add to playlist

Add to , or

Add to new playlist:

Add to playlist
This is a member-only feature
But, it's easy to become a member

Add to cart:


Loading...
Contact us

Below are the supported keyboard shortcuts for our video player.

space
Start / stop video
shift
Go to next video
shift
Go to previous video
Increase or decrease player speed by 20%
Skip ahead or go back by 15 seconds
1...9
Jump to a percentage (2 is 20%, for example)
f
Toggle full screen
Videos Pricing More Search
BuildAModule Logo Icon BuildAModule Logo Icon
Become a member!
$29
1 month
$156
6 months
10% off
$276
12 months
20% off
All memberships come with the following:
24 / 7 access to the entire 1800+ video library
An innovative video player with a rolling transcript
Extensive Drupal and cross-platform training
A thorough and thoughtful training style
Progress tracking
Take notes on specific video sections
Intuitive multi-user management
How many users?
user
Go solo with a single user, or give multiple team members access through one account and get discounts by bundling multiple users into a single membership.