0

I'm using a block of code that shouldn't be displayed on certain content types (not blocks, but actual code). When I use this approach, I'm getting undefined variable and trying to get property of non-object errors:

<?php
    if(function_exists('views_get_page_view') && views_get_page_view() ||  arg(0) == 'user') {
      $showpage = "yes";
    }
?>

<?php if (((!empty($node)) || (!empty($showpage))) && (($node->type != 'article') && ($node->type != 'basic_page'))): ?>

(Show all this)

<?php endif; ?>

It's the check if the visitor is on a user related page that causes the errors, but I have no idea how to fix this. Can someone help me out?

0

3 Answers 3

1

$node isn't available on user pages. But when $showpage is set, you are checking a property on the $node object. This gets the two errors you are describing.

I'm not sure what you are trying to do but you can troubleshoot by breaking out your booleans. I find it helpful to break logic onto multiple lines whenever possible, so that other people (and I) can read it later.

From:

if (((!empty($node)) || (!empty($showpage))) && (($node->type != 'article') && ($node->type != 'basic_page'))):

To:

if (!empty($node) || !empty($showpage)) {
  if ($node->type != 'article' && $node->type !='basic_page') {
    // do that stuff
  }
} 

So - when $showpage is not empty but $node is, as on a user page, $node is undefined.

I'm not exactly sure what you are trying to do but I think it is along the lines of "show this stuff on node pages that are not basic page and not article, or when the $showpage variable is present.

In which case:

if ((!empty($node) && !in_array($node->type, array('article', 'basic_page'))) || !empty($showpage))

Or more readably,

if(function_exists('views_get_page_view') && views_get_page_view() ||  arg(0) == 'user') {
  $showpage = 'yes';
}
elseif (!empty($node) && !in_array($node->type, array('article', 'basic_page'))) {
  $showpage = 'yes';
}

if (!empty($showpage)) {
  // do that stuff
}

It isn't a one-liner, but the longer code is easier to read, troubleshoot, and prevent errors.

2
  • Thanks! This is making sense to me now; I wasn't taking the fact into account that the $node is undefined on the user page (was focussing on the user-page itself). The line <?php if ((!empty($node) && !in_array($node->type, array('article', 'basic_page'))) || !empty($allowcontent)): ?> does the job, combined with if(function_exists('views_get_page_view') && views_get_page_view() || (arg(0) == 'user')){ . The !in_array formulation looks tidier as well. Thanks again. Commented Feb 5, 2015 at 10:00
  • Glad it is working. in_array is technically slower, if it matters, but also more versatile, say, if you need to have your options populated dynamically.
    – calebtr
    Commented Feb 5, 2015 at 17:22
0

The easiest way to find the user is in user page or not is as follows

$ARG0 = arg(0);
$ARG1 = arg(1);
if($ARG0 == "user" && is_numeric($ARG1) ){
echo "user page";
}else{
echo "not user page";
}

Check this link if any queries

How to check if this page is user Profile Page

1
  • Thanks, but it should be applied on all user-related pages (not profile pages specifically). Got my answer though. Commented Feb 5, 2015 at 12:24
0

The intention of this all is to hide the main-container div on those content types. The setup I was trying was actually a bit looked too far. In fact, other pages that weren't nodes, but no views pages either (user, 404, sent-pages, module-related pages, ...) got trapped in the hidden container as well.

By breaking up the conditions (thanks for the tip @calebtr), I came up with this working solution:

<?php if ((!empty($node) && in_array($node->type, array('article', basic_page')))): ?>

  (Nothingness)

  <?php else: ?>

  (Show all content)

<?php endif; ?>

I'm having no "undefined" errors so far.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.