0

I have a View of nodes, rendered using a Display Suite view mode. I need for users to either view the page with or without an argument in the path. If they don't enter an argument, I want the nodes in the View to render normally, but if they do, like someview/10, I simply want to add a CSS class for that node in preprocess. I have the Context module installed, so I'd like to be able to use that, but it doesn't seem like I can specify path arguments that I maybe set a context for that I can check for in the node preprocess. Or if I can do it in Views somehow, that would be great, but I can't seem to figure that out.

This functionality is for a nojs user. For js users, I just send the ajax commands back and set the CSS, but for nojs, I need to route them (I guess with drupal_goto()) to the View with the nid as an argument.

1
+50

Here's a sketch of what you could do, using a custom module:

  1. Define a menu entry for someview/%node.
  2. Define a callback function for the menu entry with a node argument.
  3. Store the node ID for later use. This could be done using drupal_static or the context API.
  4. In your preprocess function, retrieve the node ID and add a CSS class if required.

You could also use Panels to achieve a similar end, I think, but this is a fairly lightweight, if specific solution.

Let me know if you need a fuller explanation. I realise not everyone is familiar with Drupal APIs.

So your menu callback might look like:
function mymodule_callback($node) {
 context_set("mymodule", "active_nid", $node->nid);
 return views_embed_view((... ...);
}

and in the preprocess:

$active_nid= context_get("mymodule", "active_nid");
 if($active_nid==$node->nid) {
 ...
}
  • Ah, yes. That is what I was thinking, with the context API. I haven't used it yet; what is the best way to go about it? – vintorg Oct 23 '13 at 22:39
  • I haven't used it either, but I looked into it a while back and I believe you can use context_set() and context_get() for this purpose. So your menu callback might look like: function mymodule_callback($node) { context_set("mymodule", "active_nid", $node->nid); return views_embed_view((... ...); } and in the preprocess: $active_nid= context_get("mymodule", "active_nid"); if($active_nid==$node->nid) { ... } – Alfred Armstrong Oct 24 '13 at 7:49
  • I have added what I was trying to say to my answer. Code is hard to format in comments! – Alfred Armstrong Oct 24 '13 at 7:56
  • Thanks! That is exactly what I was wanting to do. I really need to get up to speed on context. – vintorg Oct 24 '13 at 18:36

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.