Views Calc is a handy module that computes aggregate functions on Views results. The way it works is by creating a new SQL statement that adds the aggregate functions to the existing Views SQL query. Unfortunately, this approach means that custom fields cannot be aggregated, since they don't exist in the database.

Attached is a patch for Views Custom Field that allows Views Calc to calculate custom fields. Please apply the patch and try it before I commit the changes to the 6.x-1.x-dev branch. To try it, you must be familiar with Views Calc. Then:
* Clear your views cache
* Instead of the "Views Calc Table" view style that it normally used with Views Calc, use the new "Views Custom Field Calc Table" style
* Using this new style, you can choose custom fields to be aggregated as you would do with normal fields

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

infojunkie’s picture

FileSize
10.65 KB

And here's the patch :-)

obrienmd’s picture

Did this patch get committed? I have a bunch of failures when trying to run patch against current (as of today) 6.x-1.x-dev... And after patch was run, tons of errors in any views_calc view. Because I'm an idiot, I didn't attempt to just use dev and see if it was in there...

web3@web2:~/web/sites/all/modules/views_customfield$ patch -p0 < views_calc.patch
patching file views_customfield.views.inc
Hunk #1 FAILED at 68.
1 out of 1 hunk FAILED -- saving rejects to file views_customfield.views.inc.rej
patching file includes/views_customfield_calc_table.inc
patching file includes/views_customfield_handler_field_phpcode.inc
Hunk #1 FAILED at 26.
Hunk #2 succeeded at 33 with fuzz 2 (offset -8 lines).
Hunk #3 FAILED at 133.
Hunk #4 succeeded at 80 (offset -91 lines).
Hunk #5 succeeded at 93 (offset -91 lines).
Hunk #6 FAILED at 109.
3 out of 6 hunks FAILED -- saving rejects to file includes/views_customfield_handler_field_phpcode.inc.rej

I'm heading out of office now, but would be happy to test further when I get back this afternoon.

obrienmd’s picture

OK - back in office. I'm officially an idiot - everything is functional. However, I'm getting an odd MYSQL warning:

user warning: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 query: SELECT nid FROM comments WHERE cid= in /var/www/clients/client1/web3/web/sites/all/modules/views_customfield/includes/views_customfield_handler_field_phpcode.inc(205) : eval()'d code on line 2.

When my computed field is running the php:

<?php
$a = db_result(db_query("SELECT nid FROM comments WHERE cid=" . $data->cid));
$b = db_result(db_query("SELECT case_type_id FROM casetracker_case WHERE nid=" . $a));
$c = db_result(db_query("SELECT case_state_name FROM casetracker_case_states WHERE csid=" . $b));
print $c;
?>

This happens everywhere else that $data->cid is called (many different views custom fields), but they all DO produce the expected output, so $data->cid is obviously not returning null, as the SQL warning seems to suggest.

Thoughts?

infojunkie’s picture

I'll review the patch for latest versions of Views Custom Field + Views Calc.

Concerning your query warnings, can you dsm($data->cid) to make sure the values are what you're expecting? And wouldn't it be better to make just one query with a couple of JOINs ?

obrienmd’s picture

Hehe... I know nothing about databases, but if you wanted to help me w/ the joins... :)

The table actually shows the correct data, which makes the warnings even stranger.

infojunkie’s picture

Status: Needs review » Reviewed & tested by the community

The patch applies to the latest dev version of Views Custom Field without warnings. Please confirm.

As for the joined query, something like:

<?php
$name = db_result(db_query("SELECT ctcs.case_state_name FROM {comment} c LEFT JOIN {casetracker_case} ctc ON c.nid = ctc.nid LEFT JOIN {casetracker_case_states} ctcs ON ctc.case_type_id = ctcs.csid WHERE c.cid = %d", $data->cid));
?>

But that, of course, doesn't solve the weird warning that you got :-)

In any case, as this issue is concerned with the Views Calc patch, we can mark this as RTBC unless anyone else encounters a problem.

obrienmd’s picture

Will confirm ASAP, probably tomorrow though. Thanks for the help w/ JOIN! Maybe I should actually learn how to make an efficient SQL query some day :)

obrienmd’s picture

Apologies for delay - this patch works great against current -dev.

Bilmar’s picture

subscribing

robby.smith’s picture

subscribing

Vacilando’s picture

Subscribing.

quinnhigurashi’s picture

I am trying this patch. However, I get this error on my testing environment. What went wrong?

user warning: Unknown column 'customfield.phpcode' in 'field list' query: SELECT MAX('') AS node_created, MAX('COUNT') AS TOTAL_COUNT, MAX('') AS nid, COUNT(node.title) AS node_title, COUNT(customfield.phpcode) AS customfield_phpcode_3 FROM node node WHERE node.type in ('activitystream') in 

I see no customfield table on my mysql database.

quinnhigurashi’s picture

Sorry, my bad. I didn't read the instruction. I selected "Views Custom Field Calc Table", then all is fine now.

YK85’s picture

subscribing

mpaler’s picture

Hi,
Strange behavior here. I successfully applied the patch to the latest dev version. When I go edit the style format, I can see the new "Views Custom Field Calc Table" option. I select it. Update. But it won't stick. It always reverts back to previous selection (eg. Views Calc Table). Any ideas here?

Mike

abaddon’s picture

i can confirm this works, you need views_calc stable-6.x-1.3 and the views_customfield-6.x-1.x-dev with the patch in #1 applied
besides the steps outline in the original post above, you might also need to edit your customfields, and separate the actual value from the formatted output for this to work (for eg. im outputting prices, and i needed to take the '$' out of the actual value)

with latest views_calc -DEV:
PHP Fatal error: Cannot use object of type stdClass as array in sites/all/modules/views_customfield/includes/views_customfield_calc_table.inc on line 14

i attached a re-roll of the patch because #1 applied with fuzz

abaddon’s picture

im actually hitting this item in the patch:

    // TODO This won't work right with relationships, need a fix here.
    if (!empty($this->view->views_calc_nids)) {
      $this->view->query->add_where(NULL, "node.nid IN (%s)", implode(',', $this->view->views_calc_nids));
    }

if i comment it out, i dont get this error in the logs and everything seems fine:

"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ',,,,,,,,,,,,,,,,,,,,,,,,,,)))... i have this in the sql that gives the error "AND (node.nid IN (,,,,,,,,,,,,,,,,,,,,,,,,,,,)))"

@infojunkie, any hints on what would be needed for the TODO item?

brigitte’s picture

Category: feature » support

Hi,
Thanks for this great module and this patch.
In my view, the total SUM doesn't display if equal to 0.
Any idea how to display 0 ?

Thanks for any help.

scott859’s picture

Greetings,

I am having the same issue as #15, I successfully applied the patch in #1, then when I select the "Views Custom Field Calc Table" option it reverts to the previous selection.

Any insight into what may be the issue here would be appreciated.

Thanks in advance.

Scott

SanDiego’s picture

I installed the patch and now I can see the new style option in the views. But no calculation is made, namely all calculation rows are blank/empty. "Views calc" computes correctly though. By the way, I don't get any error when selecting a customfield, which I did with "Views Calc Table". Do you have any idea what might go wrong?

krisrobinson’s picture

Subscribing

DarrellDuane’s picture

Category: support » feature

This patch worked perfectly for me against the dev version, I recommend this patch be committed into the module.

Sol Roth’s picture

Did you ever figure this out? I'm having this same issue as #15 and #19 - on drupal 6 site I'm doing some dev work on.

soulfroys’s picture

#16 works!!!
Views Calc 6.x-1.3
Views Custom Field 6.x-1.x-dev

Thank you!

jvieille’s picture

Not committed yet?

jvieille’s picture

#16 is OK.
I confirm that Views Calc DEV does not work with this patch.
I understand that this patch provides a new Views style that only works with custom fields, so one cannot mix database actual fields and computed custom field values in the same table.
I would have thought that this patch would better be part of Views Calc.