Current context in drupal input filters? - drupal

I want (need) to write an input filter, which replaces tokens with the values of other fields of the current node (images, for example). I thought about hacking with global variables, but perhaps there is a cleaner solution to access the current context inside a input filter?
Simply loading the node with arg(1) works in node pages, but in lists I'm getting into trouble.

As far as I know you can't.
Filters are supposed to modify only the received text to be printed.
Drupal developers discussed about the possibility to add more context to hook_filter but they decide to avoid that because too hackish and beyond the filter action range.
Besides, filters are used not only for nodes but also to comments, etc. so the context would have been more complicated to manage.
It is suggested to use nodeapi in that case.
Here's a discussion (about drupal 5, but I think it's still valid):
http://drupal.org/node/106249

Related

node_load or direct query?

What rule of thumb do you use for deciding to use node_load() or just writing a direct db_query()?
In a situation I'm looking at right now I need to get some node data and resolve data on two nodereference fields. So that would be 3 calls to node_load(). At some point here, would it be more efficient to construct the query with Joins directly?
This is for use in a self contained module that won't be distributed or used anywhere else, so I don't believe I need to worry about subverting node modification hooks (or do I?).
Edit:
Thinking about my question more, node_load() is only really applicable when you have one node to grab (and then maybe drilling down further into nodereferences like in my example). But as soon as you need to return more than one node based on some criteria, you're pretty much forced to use db_query right? Does Drupal have any abstracted API for writing queries like this?
Not a full answer (Not sure myself), just some hints.
node_load() is using a static cache (in Drupal 7, you can even use the entity_cache module to make it a permanent cache). If the nodes you are loading are being used a second time on the same page, that call will be free.
Querying CCK-tables is tricky. The schema structure can change completely based on configuration, for example when using a single or multiple values.
The reasoning behind using API methods for DB calls over direct DB calls is to provide a DB abstraction layer so that your app could move between supported database engines etc, also it enables your app to gracefully handle any schema changes (however unlikely) that core/module may make to the tables in question. It's also likely easier as #Berdir says for CCK fields and Node_Ref fields, but that depends on which you are more confident with Drupal API& PHP or MySQL...the payoff of doing it the Drupal way is increased future productivity and understanding of the codebase and what is possible :)
Oh and my rule of thumb is - Do it the Drupal way if at all possible (possible being variable depending on app time/cost/performance/whatever requirements)

Drupal 7: How can I create a key/value field(or field group, if that's even possible)?

Let's say I'm creating some app documentation. In creating a content type for functions, I have a text field for name, a box for a general description, and a couple other basic things. Now I need something for storing arguments to the function. Ideally, I'd like to input these as key-value pairs, or just two related fields, which can then be repeated as many times as needed for the given function. But I can't find any way to accomplish this.
The closest I've gotten is an abandonded field multigroup module that says to wait for CCK3, which hasn't even produced an alpha yet as far as I can tell and whose project page makes no obvious mention of this multi-group functionality. I also checked the CCK issue queue and don't think I saw it in there, either.
Is there a current viable way of doing this I'm not seeing? Viable includes "you're thinking of this the wrong way and do X instead." I've considered using a "Long text and summary" field, but that smells hackish and I don't know if I'd be setting myself up for side-effects. I'm new to Drupal.
There is the http://drupal.org/project/field_collection module but it's not yet ready. Right now you would need to implement your entity alas to do this :( not easy.
Not sure how well it would work, because it currently does a bit more (eg, forces to group pairs into categories and the keys need to be predefined) but you might want to have a look at http://drupal.org/project/properties.
You could create a these key-value fields on their own: create 2 regular fields that that can be added as often as needed.
So you have a x fields for the keys and x for the values. If this is only for you or other people it might work OK but usability wise, it's very ugly.
If you need to extract the fields from the function, to display it properly in a page template, you should propably use a different approach. Write the function with its arguemnts in a CCK field and in the template extract them as needed. The arguments are always (depending on language) in () and the different arguments are seperated by , so splitting them would by pretty easy.

How to filter custom content type nodes using ajax in Drupal?

I'm in a situation where I think I need to create my own custom search module. What I'm trying to do is make a page with a list of all my nodes in the node type - let's call it 'Beer'. So I want to be able to filter through the beers in a fashion similar to the one you find on the Apple Trailers page ( http://trailers.apple.com/ ).
I tried using Views 2 but ran it to a few problems:
I can't make the filter links like in the top of the trailers page (exclusive, just HD etc.)
The search function will only search one field (Exposed field "Beer title" but I also want it to search for manufacturer and other things.
I'm aware of a couple of solutions:
I could fix the last problem by using the Computed Field Module where I could combine the fields I want to search through. I just don't see this as a very elegant solution.
I could make my own module and create my own database queries where I apply the relevant filters (I just don't know how).
I could somehow use my already installed Solr module.
So the first solution - the easiest I guess but also kind of bad with duplicate content in my database.
The second solution - the best (maybe) - problem: I'm too dumb.
The third solution - Solr seems pretty cool but would I be able to present my beer nodes with just the title and a picture?
So I guess my question is. Which one of the three would you use? Or what other solutions could I potentially use (I'm confident there are things I haven't thought of :))?
Sounds like this could be a good use for Taxonomy not different node types. Also, Have you considered http://drupal.org/project/quicktabs ?
You could set up each "filter" as a tab that passes an argument down to a view. Then don't expose any views filters to the user.

Is there a way to get distinct taxonomy terms in drupal views?

I am trying to get a list of distinct taxonomy terms in drupal using views2.
It seems it shouldnt be that big of a problem, however when i select the taxonomy:all terms, and select what vocabulary to limit to i get duplicates.
The "distinct" option in drupal does nothing, and i cant find anything else that groups it together.
If anyone knows anything that would be great.
Thank you.
/Anders
In case of taxonomy terms, views2 applies the distinct clause to the term id, which is rather useless in your case. So afaik there is no way to do what you want in views2 without manipulating the view via custom coding.
So you might want to take a look into hook_views_query_alter() for an option to manipulate the query used by the view. Alternatively, you could execute the view via code and filter the resultset found in $view->result after execution of the view.
But depending on what you need the list for, those methods might be a bit overkill compared to just ignoring the views module for this task and doing the query directly in code from a custom module.
The problem you have having is what you actually are doing is to get all the taxonomies that match on every node on your site. A quick hack would be to create a node (doesn't need to actually be published) that contains all of the taxonomy terms, then you could limit the nodes to only that node and you would get a list of all taxonomy terms you want.
Else I would go for one of the options that Henrik Opel suggested, personally I would just make a little custom module for this, and make the queries myself.
Views is a very powerful module, but in some cases, it's actually a lot easier and faster to just do the queries yourself. Especially when the display is not so complicated. You could even use the templates that views has to render your data, if you don't want/need to create your own.

Drupal question: Views, arguments and nodequeues

Hello :) I posted this same question on a drupal-oriented site, but didn't get any replies at all. I grumbled to myself and wished that the site was more like StackOverflow, so I thought, why not try asking it here :)
I'm playing around with a view that displays nodes belonging to a taxonomy term. The vocabulary also has a taxonomy nodequeue with subqueues for all the terms.
So far the view has one argument, taxonomy term ID, and is sorted by post date. But what if I wanted to display all of the nodes of a particular term, with all the nodequeue nodes on top, and all the non-nodequeue nodes (but still under this particular taxonomy term) below, sorted by date?
To clarify, say this is my vocabulary, we'll call it 'living stuff'
Plant
--Fruit
--Vegetable
Animal
--Fish
--Dinosaurs
The following nodes are found under Dinosaurs:
Tyrannosaurus Rex (added 2009-01-01)
Megalosaurus (added 2009-01-02)
Velociraptor (added 2009-01-03)
Brachiosaurus (added 2009-01-04)
Since tyrannosauruses and velociraptors are extra awesome dinosaurs, they're also added to the nodequeue living stuff, subqueue dinosaurs:
The subqueue:
Velociraptor
Tyrannosaurus rex
The final view should display them in this order:
Velociraptor (it's first in the NQ)
Tyrannosaurus Rex (2nd in NQ)
Brachiosaurus (of the remaining dinosaurs, this is the newest)
Megalosaurus (oldest non-queue dinosaur)
I created a relationship to a nodequeue, but it wouldn't let me pick a subqueue, I could only limit to the 'living stuff' nodequeue.
My first view argument is term ID, so I thought that if I added "Nodequeue: subqueue reference" as the second argument, I'd get the expected behavior, but this only shows the dinosaurs listed in the nodequeue.
Any help or suggestions on this problem would be highly appreciated. Thanks!
I haven't really tried much with nodequeues' subqueues, so I'm not completely certain of this. But from my experience with nodequeues, it seems like when using views, you are limited to the basic things they support and can't really do the type of customization you are looking for. I think your best bet, would be to create your own views sort handler, where you can sort it like this. It will probably be quite tricky to make such a handler, since you have to figure out both views and nodequeues in order to make it work. You should really give it some thought if it would be worth it before venturing down that path, unless you have done this sort of things with views before.
More hacks:
A work-around for the behavior your trying to accomplish might be to forgo using nodequeues at all. I'm not sure the entire impetus for using the nodequeues nor the importance of dates, but faced with similar issues before, I've been able to tackle it using the following:
Sticky
Modified dates
If you sticky your super-cool dinosaurs, and modify the published dates of the elements so that they match your order, you could produce what you're looking for in a single view. It's sorta hokey, and it's predicated on not really caring about publishing dates (something that always depends on situation) nor having a more pressing reason for using a nodequeue. That said, if you don't need the nodequeue or the dates, it's a workable solution.
The 2-view solution by Jeremy should be workable, too, and I'd say that's another common way to handle the given scenario.
Hacky solution warning!
Have your primary view in your page with the nodequeue items.
Create another view which is exposed as a block for the non nodequeue items. Put this block in the main content region and limit it to only show on URLs which are the same as the first view.
You may have to do some fiddling with the url variables but I think it will work.
Why don't you concatenate the views behind each nodequeue? (each nodequeue generates a view)
You can add a header (see 'Basic Settings' in view edit page) to the second nodequeue that contains php code that invokes views_embed_view('first nodequeue') (you just need to change the header's input format to 'php'). Or rather, create a custom view that includes each nodequeue by invoking views_embed_view(). This would effectively place one nodequeue on top of another, and if they are of the same format/content type you don't even need to mess around with fields: you can use Row Style == Node. As far as your arguments, they can be passed to views_embed_view, as the third parameter (the docs don't say that AFAICR, but I found a post in the forums (http://drupal.org/node/99721) that indicated args can be sent as '$current_view->args' to the view being embedded).
HTH

Resources