-1

I would like to combine these two php codes to display a block only on nodes of a special node-type and when the current logged in user is the author of the node. Any help very much appreciated!

$types = array('middle_content' => 1);
if ($node = menu_get_object()) {
  if (isset($types[$node->type])) {
    return TRUE;
  }
}

as found on Block on specific pages and node types

global $user;
if (arg(0) == 'node' && is_numeric(arg(1))) {
  // Get the nid
  $nid = arg(1);

  // Load the node if you need to
  $node = node_load($nid);
}

if($node->uid == $user->uid){
return TRUE;
} else {
return FALSE;
}

as found on How can I check if the author of the current node is the current logged in user?

0

Assuming Drupal 7, if you're using PHP Filter, go to the block's settings -> Visibility settings -> Pages -> Show block on specific pages. Select "Pages on which this PHP code returns TRUE (experts only)." Use the following code replacing my_content_type with the machine name of your content type.

<?php 
  $node = menu_get_object();
  global $user;
  if (isset($node) && $node->type == 'my_content_type' && $node->uid == $user->uid) {
    return TRUE;
  }
?>

Alternatively, you can select one or more content types under Visibility settings -> Content types. Then you'd only need to check for the node's author, thusly:

<?php 
  $node = menu_get_object();
  global $user;
  if (isset($node) && $node->uid == $user->uid) {
    return TRUE;
  }
?>

Note the security issues presented by the PHP filter module.

  • Thanks for your help! Unfortunately I get the following error for both codes: Notice: Trying to get property of non-object in eval() (line 4 of .../modules/php/php.module(80) : eval()'d code). – user3406538 Dec 14 '16 at 21:10
  • Hmm...OK, I'll investigate. This code worked on my site. – Labracadabrador Dec 15 '16 at 16:17
  • Ah, you're probably seeing that on non-node pages (i.e. a Views-generated page)? I've update my response to check if the node object exists. – Labracadabrador Dec 15 '16 at 17:24
  • Great, now it is working perfectly. Thank you so much for your help! I really appreciate it! – user3406538 Dec 15 '16 at 21:46
  • You're welcome! When you get a moment, could you mark it as the accepted answer? Thanks! – Labracadabrador Dec 15 '16 at 21:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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