Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
I can not find ANY documentation for viewsphpfilter.
Is there any? Where?
Comments
Comment #1
gnassar CreditAttribution: gnassar commentedIt's in the README.
Front page updated to reflect this.
Comment #2
drupalfan2 CreditAttribution: drupalfan2 commentedThis is not enough documentation, everbody needs examples for this, because otherwise nobody know how to code it!
Comment #3
OFF CreditAttribution: OFF commentedCan I ask you to at least one example to show?
Comment #4
Feet CreditAttribution: Feet commentedI added a (poor) example to the handbook http://drupal.org/node/763980
I too could have used an example or two to get going faster but got there in the end.
Hopefully a library of examples/snippets will develop in the handbook as gnassar suggested.
Comment #5
pumablues CreditAttribution: pumablues commentedI agree completely. Some basic examples would help tremendously... not because I'm not familiar with PHP, but because the page here and the "readme" file doesn't explain things in the context of Drupal. Last I checked, basic examples were nearly always part of documentation in open-source projects to help along beginners.
Comment #6
AvalancheOfLlamas CreditAttribution: AvalancheOfLlamas commentedI'd like to submit a basic example that I use for the documentation page of this project. Please edit it any way you like.
Basic Filter Example
Sometimes, I create my own weight fields, so that users can order nodes themselves.
I've often create views of nodes that have a higher weight or lower weight that the currently viewed node.
This requires a filter that compares the weight of the current node to the weight of all other possibly selected nodes.
For this example, we'll create that filter.
My field in CCK is called "field_report_order" as its machine name.
Notes on DB queries: as of CCK 6.x-2.8, fields are stored in different tables/columns based on a few factors. Use this PHP to determine how to phrase your query:
For more reference on sql syntax, see http://www.1keydata.com/sql/sql-syntax.html
Comment #7
rbrownellHere is a good version of the script from #6 that essentually allows one to run a simple custom SQL query without node related retrieving variables involved.
The function will pass a series of numbers separated by commas which will be added to the master SQL of the view you are working in. These numbers are the node id numbers.
It is important to NOT include the
<?php ... ?>
tags. (It is this way throughout most of Drupal's front end.)I only added this so as to further simplify the example in #6. I had a bit of trouble starting off understanding it so I've included my modifications to it here.
Comment #8
gnassar CreditAttribution: gnassar commentedFeel free to submit those to the handbook. I don't moderate that stuff, and don't have any control over it.
I will say this: when you submit, #7 is probably the better "simple example." Anything using CCK is automatically not simple for most users. I think #6 is useful, but as an advanced example (or perhaps one dealing specifically with CCK, which is useful unto itself).
Comment #9
rbrownellThe latest release broke the example in number 7... I don't know what happened. I've selected "filter to these IDs" and "PHP code"
Can anyone else confirm?
Comment #10
abaddon CreditAttribution: abaddon commentedi think what lacks here is what variables are available to the php code.. using sql queries might not always be best.. everything might already be available in the view result and you just need to filter some outseems you can use $this->view for the current view, im guessing the query object is accessible in the same manner or from the view object, i dont understand the comment about using $query and clobbering the current scope because theres no query or other views objects available directly
silly me, the filter is ran before any results are in the view object.. so no point in using it that way.. one could still modify the views object ala hook_views_query_alter()
Comment #11
alibama CreditAttribution: alibama commentedwell i finally got something to work, however that $nodeids[1] = -1; thing broke my query... finally got some traction with this
quick tutorial with screenshots here
http://blog.hsl.virginia.edu/drupalpress/views-php-filter-drupal-6/
thanks for the great mod, wish it were more obvious in its usage... maybe would be if i were smarter ;)
Comment #12
antoniotorres CreditAttribution: antoniotorres commentedIs there any way to access the current view result and tweak the current result as opposed to doing the whole operation inside of a filter??
Comment #13
sudev.pradhan CreditAttribution: sudev.pradhan commentedSomething I noticed while using PHP filters is that we can also return a comma separated list of node Ids instead of returning an array.
Comment #14
gnassar CreditAttribution: gnassar commentedLots of people seem to have had a lot of trouble with the $nodeids[1]=-1 line in the above code... I'll go ahead and state for the record that that shouldn't be there. Passing an empty array shouldn't cause an error; it should cause a view with no nodes (empty). That's not an error. Just dropping that line (so "$nodeids = array()" just leaves you with an empty array) should work fine. If there's any place in the docs I wrote where I explicitly condoned using anything other than a node ID (which -1 isn't) or starting an array with 1 instead of 0, I apologize -- that is flatly incorrect.
Also, I don't know why people post stuff here instead of in the handbook where it belongs, but I guess I'll just copy the stuff over there and close this issue again. Please forgive any mistakes in copying or attribution; I'll try my best here.
Comment #15
gnassar CreditAttribution: gnassar commentedOK, I copied all examples from here (except #11; I hope you don't mind, alibama, but yours was similar enough to #7 where I thought including that one was enough -- if you feel I'm wrong, feel free to add yours to the handbook too). They're over as child pages from http://drupal.org/node/763980. Any new examples can be added over there.
Also changed the project page to note the documentation and examples on the handbook page.