欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

How to exclude current node from a block view? 从区块视图中排除当前节点

shiping1 的头像
 

I created a block view listing nodes of the content type: cars.

I added the block to the second sidebar.

After, I created 2 nodes of this type with the titles: car-1 and car-2.

Then the view block shows these 2 nodes. So far so good.

If I visit the page example.com/car-1 I see the block listing both the car 1 and car 2 nodes. I want to exclude the car-1 node from the block since I'm at: example.com/car1

This is what I did in the view:

1.  Click on add under "contextual filters".    
2.  Choose Content:nid.
3.  Under "when the filter variable is not available, choose "provide default value".
4.  From the drop down menu select "content id from url".
5.  Scroll all the way down to the bottom of the window and click on the
6.  "More" link.
7.  Click "Exclude"

I works fines if I visit a node, but the block is not shown in any Views (i.e.: in the frontpage view )

I know that the reason is that the Content nid is not present in a view, but don't know how to workaround this.

 

正确答案  One option is to clone the block, make it show everything (remove the contextual filter and the exclude setting) and in block configuration (via admin/structure/block) configure it to show on all pages except node/*.

Another option, if you want to have everything in one display and don't mind using php, is to use Provide default value > PHP code:

if (arg(0) == 'node' && is_numeric(arg(1))) {
  return arg(1);
} else {
  return 0;
}

The exclude checkbox must be on.

So this is giving the sql the node id to be excluded, normally the current node id, and when there is no node id, the node id to be excluded is 0, in other words don't exclude anything as there is no nid 0.

Without checking that the second argument is numeric, this wouldn't work on the default front page as it would have 'node' but not have nid.

shareimprove this answer
 
   
thanks I'll give it a try. –  chefnelone Feb 17 '14 at 13:11
   
The php code works very well. thanks –  chefnelone Feb 17 '14 at 14:26

A use case for Page Manager / Panelizer. This is ment to be for this type op content page creation.

An other way is the context module. In that case you can define on which pages which context is used. Good example of this is in the julio distribution (www.drupal.org/project/julio)

If you are interested in the Page Manager way, watch this screencast http://nodeone.se/sv/node/702

shareimprove this answer
来自    http://drupal.stackexchange.com/questions/103461/how-to-exclude-current-node-from-a-block-view
普通分类: