2

I have a view block listing nodes. Let's say I have the following nodes (it's urls)

node/first-node
node/second-node
node/third-node
node/fourth-node

Then I have the view block listing this 4 nodes. This block is shown in the First sidebar in all pages.

What I need: when the block is shown at the page node/fourth-node, I need to arbitrarily exclude from the view block from the following nodes.

   node/second-node
   node/third-node

I need to do this in a way so that then after I can do the same thing with other nodes.

I thought of adding a field to the node called: "Exclude from view block at:" to be filled with the paths (one per line) where the node will not be listed in the view block (similar to blocks settings). Then I will need to modify the view.

Is this a correct approach? If so, what do I have to do in the view?

5
  • do you want to exclude the content inside the views block, based on url ?
    – Bala
    Feb 26, 2014 at 8:50
  • @Bala Yes. I need to exclude node/second-node from the block if the url is node/fourth-node.
    – chefnelone
    Feb 26, 2014 at 8:57
  • how do you added the content inside the views, meaning its a title field or what? still not get your point
    – Bala
    Feb 26, 2014 at 9:05
  • I think the correct approach is this solution posted by Matt: '"1 - If you're only working with nodes, then you can create Node Reference field, instead of listing the URIs, so if the URIs changes, they will be updated. You can then access the "NID" that you want to exclude from the page loading the current node. Then you make a hook_views_query_alter and add a filter to the listed node ids."'
    – chefnelone
    Feb 26, 2014 at 9:27
  • But I don't know how to build the hook_views_query_alter
    – chefnelone
    Feb 26, 2014 at 9:28

2 Answers 2

2
+25

What you could do if you could use either Views Filters and use the view Filter options to set which URL's to exclude based on tokens.

You could also maybe use Views Rule to try and do something similar.

I think the Views Filters may be an easier option. But if you want to do your suggestion (add a field to the node) then what you can do is pretty much exactly what you said, you would add a filter to the view based upon the available tokens and field data.

1
  • Could you elaborate for the Views Filter options?
    – chefnelone
    Feb 26, 2014 at 8:55
1

I think this sentence is confusing

I need to do this in a way so that then after I can do the same thing with other nodes and pages.

Pages and nodes could means different things, like if you create a "Page" with Views, how do you want to store the list of excluded node? You can't store it in a "field". Also, if there are different nodes displayed on the same page, which page are you going to exclude from your views? Both of them? The first one?

Well, with your description, I see two options:

1 - If you're only working with nodes, then you can create Node Reference field, instead of listing the URIs, so if the URIs changes, they will be updated. You can then access the "NID" that you want to exclude from the page loading the current node. Then you make a hook_views_query_alter and add a filter to the listed node ids.

2 - You could create a mapping URI to URIs programmatically (it's also possible to store it as variable or somewhere else, but needs more coding). Then you need to run your view, get the results, check the URIs and exclude some based on your mapping.

I really suggest you don't do the second solution, because it's uggly :) You'll load a lot more nodes that you actually display. In the first solution, you only fetch the nodes you want. Also in the second solution, everytime your URIs changes, you need to change the mapping...

But the first solution might not match all your needs...

Hope this helps !

4
  • You are right. I meant nodes.
    – chefnelone
    Feb 25, 2014 at 18:02
  • Then definitely go for the first solution, if you have other content types that need to exclude some nodes, you just need to add the same Node Reference field, and it should be working!
    – Matt
    Feb 25, 2014 at 18:05
  • The first option is what I need. I already created the entity reference field. I also added a relationship in my view: Entity Reference: Referenced Entity (A bridge to the Content entity that is referenced via field_entity_my_nodes). I know I have to add now a Contextual Filter, but I get lost here...
    – chefnelone
    Feb 25, 2014 at 18:05
  • I would stay away from doing 2, but 1 wouldn't be too bad of a idea.
    – Steven
    Feb 25, 2014 at 21:28

Your Answer

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

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