Jump to content

Odd PHP notice in Safari but not Chrome?


a-ok
 Share

Recommended Posts

I'm getting an odd issue where Safari isn't displaying a certain element based on an IF statement but Chrome is. I know... this sounds bizarre... and it's freaking me out.

I have turned on debug and above where the element is meant to be inserted, on Safari ONLY, I receive this notice...

Notice: Trying to get property of non-object in /Users/john/Sites/website/site/templates/article-overview.php on line 130

This line is as follows...

<?php if (($layout->article_overview_flexible_layout_type->id === 1 || $layout->article_overview_flexible_layout_type->id === 2) && ($layout->next->article_overview_flexible_layout_type->id === 3 || $layout->next->article_overview_flexible_layout_type->id === 4)) : ?>
	<div class="article-overview-layout-sep"></div>
<?php endif; ?>

I have set up a data-type tag to output the 'article_overview_flexible_layout_type->id' and you can see in the screenshot that both Chrome (left) and Safari (right) is outputting this correctly.

Any help or ideas? Why is it saying it is trying to get property on non-object?

Thanks.

Screen Shot 2016-07-22 at 11.25.48.png

Link to comment
Share on other sites

Even I test, within the loop:

<?php echo $layout->next->article_overview_flexible_layout_type->id; ?>

It outputs the correct id in Chrome but in Safari it doesn't and I get the notice (line 23 is the echo above):

Notice: Trying to get property of non-object in /Users/john/Sites/website/site/templates/article-overview.php on line 23

Link to comment
Share on other sites

27 minutes ago, Martijn Geerts said:

When next doesn't exists, then the next page is probably a null page. 
The null page definitely doesn't have a field called 'article_overview_flexible_layout_type',
thus the property id doesn't exist.

 

Why does this only show in Safari and not Chrome? And why does it show this notice every time even when the next page does exist?

Link to comment
Share on other sites

53 minutes ago, Martijn Geerts said:

Don't know? Cache, loggedin state? 
Never-the-less, the syntax looks incorrect.

I'm not sure because it's throwing a Notice error even when there is a next item. I've wrapped it in a check now; which seems to have removed the Notice (weirdly) but where PHP should be returning the element when 'article_overview_flexible_layout_type->id' is either 1 or 2 and the next is either 3 or 4... in Chrome it does... and in Safari it doesn't.

Safari:

Screen Shot 2016-07-22 at 15.48.12.png

Google Chrome:

Screen Shot 2016-07-22 at 15.49.59.png

Link to comment
Share on other sites

It would appear ->next() is the better thing to use here BUT it still isn't returning correctly.

I tried some tests.

<pre>
    This: <?php echo $layout->article_overview_flexible_layout_type->id; ?><br/>
    Next: <?php echo $layout->next()->article_overview_flexible_layout_type->id; ?>
</pre>

In Chrome, I got, for one of the items in the repeater loop...

This: 1
Next: 2

In Safari, for the very same item, I got:

This: 1
Next: Notice: Trying to get property of non-object in /Users/john/Sites/website/site/templates/article-overview.php on line 130

Link to comment
Share on other sites

1 minute ago, Martijn Geerts said:

Your ID's look suspicious to:
ID 1 = Home page
ID 2 = Home page of Admin
ID 3 = The ProcessWire Pagelist
ID 4 = Doesn't exist in ProcessWire

Also want to mention that those admin pages by default only have a process and a title field.

Sorry these are ids of the option field 'article_overview_flexible_layout_type'.

1=Side by side (image on right)
2=Side by side (image on left)
3=Full image (text on top)
4=Small

Link to comment
Share on other sites

I'm actually thinking it's because I am trying to get the next()-> item of a repeater, which is admin/logged in accessible only...? 

Update

This is exactly the issue. I wasn't logged in on Safari and thus it couldn't get the next() element because the repeater is stored within the admin. How do I allow this?

Link to comment
Share on other sites

4 minutes ago, Martijn Geerts said:

$layout->next($layout->parent()->children())->article_overview_flexible_layout_type->id;

Same issue. I'm stumped to why you cannot get the next repeater element without being logged in...

Link to comment
Share on other sites

What is $layout?

Well if you use a next() , PW will search for the next repeater page and while they're hidden in /admin they're protected. 

You could try next("check_access=0") to get around that.

  • Like 2
Link to comment
Share on other sites

3 minutes ago, Soma said:

What is $layout?

Well if you use a next() , PW will search for the next repeater page and while they're hidden in /admin they're protected. 

You could try next("check_access=0") to get around that.

Thanks, Soma.

$layout is each repeater row ($page->article_overview_flexible_layout as $layout) with 'article_overview_flexible_layout' being the repeater.

Link to comment
Share on other sites

I have a repeater and when doing

$firstRepeater = $page->carousel->first();
$content .= $firstRepeater->next();

I can't get the next when I'm not logged in. But with 

$content .= $firstRepeater->next("check_access=0");

It works. Don't now of anything else that might do the trick.

Link to comment
Share on other sites

7 minutes ago, Soma said:

I have a repeater and when doing


$firstRepeater = $page->carousel->first();
$content .= $firstRepeater->next();

I can't get the next when I'm not logged in. But with 


$content .= $firstRepeater->next("check_access=0");

It works. Don't now of anything else that might do the trick.

Thanks, Soma. I wonder if it makes a difference as I am doing this within the loop? It's weird that it would work logged in though (surely if it was wrong, it would be wrong regardless of the logged in state).

Link to comment
Share on other sites

It doesn't matter it's in a loop or not. And it definately works, I've used it many times with repeaters. Then your problem may lays somewhere else. 

You also can't assume that next() always returns a page, thus doing something like $page->next()->title is not real world code.

Link to comment
Share on other sites

40 minutes ago, Soma said:

You also can't assume that next() always returns a page, thus doing something like $page->next()->title is not real world code.

I understand. It was merely a test to prove/test whether or not it was getting the next repeater row when logged out.

Doing exactly as your code does, when logged out, doesn't work for me. Logged in it does. I'll go hunting.

Thanks anyway for your help.

Link to comment
Share on other sites

Soma's code should work for your situation (it works for me), but a couple of other possibilities...

Use "include=all", but this would include unpublished repeater items which may be undesirable,

foreach($page->my_repeater as $repeater_item) {
    $next_repeater = $repeater_item->next("include=all");
    if($next_repeater->id) {
        // do something with $next_repeater
    }
}

 

Use getNext()

foreach($page->my_repeater as $repeater_item) {
    $next_repeater = $page->my_repeater->getNext($repeater_item);
    if($next_repeater && $next_repeater->id) {
        // do something with $next_repeater
    }
}

 

  • Like 1
Link to comment
Share on other sites

I still have NO idea why I couldn't get the next repeater. Nonetheless, this is what I landed on. I've just seen your post @Robin S and your second example is what I went with.

$page->article_overview_flexible_layout->getNext($layout);

'article_overview_flexible_layout' being the repeater and $layout being the value from the repeater (foreach $article_overview_flexible_layout as $layout).

Thanks for all your help. It's bizarre it never worked when being logged out but alas.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...