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.
Using computed field to access the database, its working great, except that it recomputes only on node saves, not on every view. "Store using the database settings below" is set to false, of course. Using devel I can see that the 'eval' query with my mysql query is ran only on node save / update, not on every view.
Comment | File | Size | Author |
---|---|---|---|
#6 | computed_field_recompute-on-view.patch.txt | 568 bytes | sin |
#5 | computed_field.module.txt | 8.52 KB | sdsheridan |
Comments
Comment #1
neurojavi CreditAttribution: neurojavi commentedHi:
I have the same problem.
I don't save the field in the database but it keeps being calculated in node updates, not in node views as stated in the documentation.
Thanks.-
Comment #2
neurojavi CreditAttribution: neurojavi commentedHi again:
I've done some debugging and have discovered that the problem is with caching...
It seems that hook_field with op=load isn't called... When I empty the cache it gets called.
This wouldn't be a problem if the computed field is only using form data but it would be a problem if it's used with other data.
For example:
- With the current date and time
- if you want (it's my case) to do a drupal_goto to a node stated in another form field, it would be done only one time...
I think that the only solution is to give and additional config option to make the field not cacheabl but I don't know if it's possible...
In any case, I've used a dirty hack: I've wrote the code in the display box of the computed field... This way I'm sure it will always be computed on view (I use the computed code box to get in $node_field[0]['value'] the values I'll need in the view box).
Bye.-
Thanks.-
Comment #3
chris55 CreditAttribution: chris55 commentedThanks neurjavi for that. Finally I get it working!
For clarity here's a simple example:
In the computed code box put:
$node_field[0]['value'] = date("Y-m-d H:i:s");
In the display box put:
$display = date("Y-m-d H:i:s");
It doesn't work any other way. Why one needs the entry in the computed code box I don't know.
Chris
Comment #4
hliljegren CreditAttribution: hliljegren commentedHaving the same problem...
...tried the work-around and put my query in the display field, which only partly helped.
If I make a computed field store it in db (so I can use it in a view) set up a view with a 'page' with view type to table, the query never runs. If I set the view to any other view type the query do run. (and of course it is the table view I want.)
Comment #5
sdsheridan...and I think I fixed the problem. Thanks to everyone else for doing chunks of the leg-work, which pointed me to a solution that is working for me. What I did was simply add
_computed_field_compute_value($node, $field, $node_field);
to the 'view' case of the computed_field_field function, right under the case statement, as follows:Now it's computing on each view. At least it is for me so far. That means that I now just have in the Computed Code section of the actual field definition:
and in the Display format just:
This is how I'm getting a "Link to your user profile" on the user profile page. I'm using node profile here, and pathauto as well, just FYI.
I've attached the computed_field.module (with a .txt extension) for those who don't like to edit or type. ;-) Drop the .txt extension and copy this over the existing computer_field.module.
Comment #6
sin CreditAttribution: sin commentedI have this issue with 5.x-1.2.
Computed value gets cached in cache_content table and not recalculated on node view/preview.
Thanks, sdsheridan, for your fix. It works.
Comment #7
blackdog CreditAttribution: blackdog commentedPatch applies with fuzz, works great!
Comment #8
sykic CreditAttribution: sykic commentedthe above patch works good for me
Thanks sdsheridan
this is one great module
Comment #9
Justin Freeman CreditAttribution: Justin Freeman commentedWorth reviewing also is this patch, #202384: 'view' cell not populated
Comment #10
moritzz CreditAttribution: moritzz commentedWorks great for me as well, thanks a lot. I can now even calculate on $node->taxonomy values. What a relief.
Comment #11
KarlM CreditAttribution: KarlM commentedHi
This patch doesn't seem to have been applied to the D6 version.
Is there any reason for that?
Because this is exactly what I am looking for.
Cheers
Karl
Comment #12
Moonshine CreditAttribution: Moonshine commented@KarlM
In the D6 version you should be able to duplicate your computed code logic in the "Display Format" section if you want it to be recalculated on every "view". For example if you have a Display Format of :
You should see a new number on every load. (That is *assuming* that you're not an anonymous user w/ page caching turned on in the admin, or using the "Computed Value" output format.)
To put a patch in like the one above would force *every* non-db computed field to be recalculated which would truly anger users who are using it to do more costly processing. Right now we have the best of both worlds.
Comment #13
sin CreditAttribution: sin commentedNice comment, Moonshine! I didn't know display is evaluated every time. I remember the reason for the patch above was a confusing description of CF settings field -- it was said that it is recomputed on view but it was not so and I considered it a bug rather then feature. I suggest CF settings field descriptions should be updated to contain information about which code computes when in stored and non-stored fields. Maybe a new option (checkbox) to recompute on every view is a nice feature to add.
Thank you for porting and maintaining D6 version! :)
Comment #14
PawelPohl CreditAttribution: PawelPohl commentedThanks for the discussion people - now I finally understand what the display field is for. I'm using computed fields to store big chunks of pre-computed HTML, so recalculating every time would actually slow my site down a lot!
Comment #15
xjmTracking; I am not sure whether this will solve my issue or not (http://drupal.org/node/358306 #1), but it's worth a shot.
Comment #16
deekayen CreditAttribution: deekayen commentedThere's no DRUPAL-5 branch to apply this patch to, so I'm going through closing 5.x issues.
Comment #17
sin CreditAttribution: sin commented5.x was originally maintained by module authors:
"Computed Field is being maintained by Agileware, http://www.agileware.net (Drupal 5 branch)"
What happened with the branch and original authors?