1

I have a block in view that is displaying the user details, i want to display that block in a nodes created by the respective users.

i am successfully using this block in user page now i want it on node page.

please help me.

For another block which is also coming from view is worked fine with this code Show block on specific pages:

<?php

//Read URL
$path=$_GET['q'];

//If URL is node page

if ( strpos($path,'node')===0){
//Parse URL to get nid
$links=explode("/",$_GET['q']);
$nid=$links[1];

//Load node

$node=node_load($nid);

//Display block only if node is of certain content type
if($node->type=='user_art'){
return TRUE;
}
}

return FALSE;
?>

Thanks

  • Previously for another block i have tried this code below:<?php //Read URL $path=$_GET['q']; //If URL is node page if ( strpos($path,'node')===0){ //Parse URL to get nid $links=explode("/",$_GET['q']); $nid=$links[1]; //Load node $node=node_load($nid); //Display block only if node is of certain content type if($node->type=='user_art'){ return TRUE; } } return FALSE; ?>........It works fine for that block.. – asalug Mar 25 '14 at 16:25
  • What is it that is not working then? If the block is not appearing then your code is not returning true so debug whats going wrong. For example is the node type 'user_art'? – Felix Eve Mar 25 '14 at 16:40
0

It is better to check the arguments from arg() function, this way:

<?php
// PHP VISIBILITY CODE:
// If URL is node page

if (arg(0) == 'node' && is_numeric(arg(1))){
  $nid = arg(1);

  // Load node:
  $node=node_load($nid);

  // Display block only if node is of certain content type
  if ($node->type=='user_art') {
    return TRUE;
  }
}

return FALSE;
?>

Maybe the problem is with the views argument to get $node->uid. Try this:

More info: https://groups.drupal.org/node/129979#comment-751473

1 - Create a new Block View to view Users (not Content)

2 - Filter Criteria should say User: Active (Yes)

3 - There is a default sort Criteria item. Remove it since it doesn't matter.

4 - Add a Relationship: Choose "User: Content authored"

5 - Add a Contextual Filter: Chose "Content: Nid"

6 - In the configuration options for the newly added Content: Nid, choose these options:
WHEN THE FILTER VALUE IS NOT AVAILABLE:
Provide default value
In the Select box, choose Content ID From URL.

WHEN THE FILTER VALUE IS AVAILABLE OR A DEFAULT IS PROVIDED
Specify validation criteria
Validator select box: Content
Content Types: choose the content types you want the view to work with.
Filter Value Format: Node ID
Action to take if the filter value does not validate: Hide View

7 - Save the View.

8 - Go to Structure -> Blocks and configure the block to show on the types of content you want it to show on and position it wherever you want it to appear.

9 - Done.
  • Thanks a lot..It works only for the articular node page..but its not working on user page where previously i have created one block which is diplaying on user's page..so is this possible that i only need to creat one single view block which works on both places.. – asalug Mar 25 '14 at 17:09
  • Views arguments are not the same so the easy way is to create two separate view blocks. I recommend using "context" module to set the views block visibility, this is better than PHP for this case. – jorgetutor Mar 25 '14 at 17:18
  • k..Thanks..i will try to use this module and will give you a feedback..thanks.. – asalug Mar 25 '14 at 17:26
  • Hi, i want a link which direct to that individual user's profile.content authored profiles. – asalug Apr 2 '14 at 6:06
  • How can i add total number of node created by particular user on a same block which has been created above. – asalug Apr 3 '14 at 17:35
0

I think what you are trying to accomplish is something like this:

<?php

  global $user;

  $node=menu_get_object();

  if (isset($node->uid) && $node->uid==$user->uid) {
    return TRUE;
  } 

  return FALSE;

?>

The above uses the menu_get_object() function to look at what, if any, node page you are currently on, and if the user who created that node is the same as the current user, it returns TRUE. Otherwise, if you aren't on such a node page, it returns FALSE.

I might add this may be a better way to accomplish your logic for displaying a block on only a certain type of node, eg something like this:

<?php

  $node=menu_get_object();

  if (isset($node->type) && $node->type=='WHATEVER') {
    return TRUE;
  } 

  return FALSE;

?>
  • I added both the code individually but its still not working.. – asalug Mar 25 '14 at 16:49
  • You will most probably need to provide some screen shots then of the various UI screens (Views, Blocks) to try to figure out what's just subtly off/wrong/different – Jimajamma Mar 25 '14 at 16:53

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.