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.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

neurojavi’s picture

Priority: Normal » Critical

Hi:

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

neurojavi’s picture

Priority: Critical » Normal

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

chris55’s picture

Thanks 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

hliljegren’s picture

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

sdsheridan’s picture

FileSize
8.52 KB

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

/**
 * Implementation of cck hook_field
 */
function computed_field_field($op, &$node, $field, &$node_field, $teaser, $page) {
  switch ($op) {
    case 'load':
      // compute field on load if it isn't stored in the database
      if (!$field['store']) {
        _computed_field_compute_value($node, $field, $node_field);
        return array($field['field_name'] => $node_field);
      }
      break;
    case 'view':
      _computed_field_compute_value($node, $field, $node_field);    // Added 2007-11-07 by SDSheridan to solve non-compute on View.
      if ($field['display']) {
        $items = array();
        foreach ($node_field as $delta => $item) {
          $items[$delta]['view'] = content_format($field, $item, 'default', $node);
        }
        return theme('field', $node, $field, $items, $teaser, $page);
      }
      break;
    case 'validate':
      break;
    case 'insert':
    case 'update':
      _computed_field_compute_value($node, $field, $node_field);
      break;
  }
}

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:

global $user;

if (!$node->nid) node_save($node);

$sql = "SELECT u.dst FROM {url_alias} u WHERE u.src = '%s'";
$src = "user/" . $node->uid;

$profilePath = db_result( db_query( $sql, $src ) );

$node_field[0]['value'] = 'http://www.yoursite.com/' . $profilePath;

and in the Display format just:

$display = $node_field_item['value'];

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.

sin’s picture

Version: 5.x-1.1 » 5.x-1.2
Status: Active » Needs review
FileSize
568 bytes

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

blackdog’s picture

Status: Needs review » Reviewed & tested by the community

Patch applies with fuzz, works great!

sykic’s picture

the above patch works good for me

Thanks sdsheridan

this is one great module

Justin Freeman’s picture

Worth reviewing also is this patch, #202384: 'view' cell not populated

moritzz’s picture

Works great for me as well, thanks a lot. I can now even calculate on $node->taxonomy values. What a relief.

KarlM’s picture

Hi

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

Moonshine’s picture

@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 :

$display = rand();

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.

sin’s picture

Nice 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! :)

PawelPohl’s picture

Thanks 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!

xjm’s picture

Tracking; I am not sure whether this will solve my issue or not (http://drupal.org/node/358306 #1), but it's worth a shot.

deekayen’s picture

Status: Reviewed & tested by the community » Closed (won't fix)

There's no DRUPAL-5 branch to apply this patch to, so I'm going through closing 5.x issues.

sin’s picture

5.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?