I can not find ANY documentation for viewsphpfilter.
Is there any? Where?


Status:Active» Closed (fixed)

It's in the README.

Front page updated to reflect this.

A new filter, labeled "Node: Node ID", will appear as addablein your Views filter lists.  If you add that filter, you'll note the "Options" dropdown will have two choices: "PHP code" and "ID list".  "PHP code" will take a snippet of PHP as its value (without the <?php and ?> bracket tags), which should return an array of integers representing node IDs.  The filter will evaluate the PHP, and filter on the numbers in the return array.  "ID list" will simply take a list of numbers, separated by commas, and filter those node IDs.  This is currently used mainly for testing, but may survive to release if someone finds it useful.

This is not enough documentation, everbody needs examples for this, because otherwise nobody know how to code it!

Can I ask you to at least one example to show?

I 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.

I 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.

Status:Closed (fixed)» Needs review

I'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.

= (int)arg(1); // Gets the node id of the current page
$node = node_load($nid);  // Loads the node with this nid
$order = $node->field_report_order[0]['value'];  // Sets the variable "$order" to the current node's weight field
$nodeids = array();  // Creates the array we'll use to pass back to the filter
$my_result = db_query("SELECT nid FROM content_type_workspace_report WHERE field_report_order_value > $order");  // This is the query from the database.  See notes below for more.
$nodeids[1] = -1; // I added a dummy value to the array.  This ensures that if there are no results from the database query, you won't get an error from passing back an empty array.
while ($my_row = db_fetch_array($my_result))  // Adds the node ids to our array
$nodeids[] = $my_row['nid'];  // The blank [] next to $nodeids will add each 'nid' from the query sequentially in the array
$nodeids// Passes the filter back

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:

// Obtain information about a field attached to a node type.
$field = content_fields('field_name', 'node_type');
// Obtain database information about the field.
$db_info = content_database_info($field);
// $db_info['table'] contains the name of the table where the field is stored.
// $db_info['columns'] contains information about the database columns of the field.

For more reference on sql syntax, see http://www.1keydata.com/sql/sql-syntax.html

Super Simple Example

Here 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.

= date('l');  // Any special variables should be first declared. In this case I am declaring the current day of week. Wash, rinse and repeat with as many variables as you need.
$nodeids = array();  // Creates the array that will be passed back to the filter.
$my_result = db_query("SELECT nid FROM node WHERE type LIKE 'ec_forecast_weather_data' AND title NOT LIKE '%$todaydayofweek%'");  // This is the query from the database. Place your custom SQL Query here between the set of quotation marks and brackets. Note how the custom variable is included in the SQL query.
$nodeids[1] = -1; // This ensures that if there are no results from the database query, you won't get an error from passing back an empty array.
while ($my_row = db_fetch_array($my_result))  // Adds the node ids to the array.
$nodeids[] = $my_row['nid'];
$nodeids// Passes the filter back

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.

Status:Needs review» Closed (works as designed)

Feel 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).

Status:Closed (works as designed)» Active

The 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?

i 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 out
seems 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()

well i finally got something to work, however that $nodeids[1] = -1; thing broke my query... finally got some traction with this

= array();
$my_result = db_query(“SELECT nid FROM content_type_event WHERE field_event_capacity_value > field_event_chairs_left_value”);
while (
$my_row = db_fetch_array($my_result))
$nodeids[] = $my_row['nid'];

quick tutorial with screenshots here

thanks for the great mod, wish it were more obvious in its usage... maybe would be if i were smarter ;)

Is 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??

Something I noticed while using PHP filters is that we can also return a comma separated list of node Ids instead of returning an array.

Status:Active» Closed (fixed)

Lots 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.

OK, 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.