I have content type feature and i have field hit_count in this c.type, also i have db table - hit_count - example:

nid   hit_count

1     434
2     233
3     512

I want to output hit_count inside my field - hit_count on current node(also should appear on node edit). So for example if node id is 1, field hit_count = 434, and if admin want to edit this field i want to update field value to my table hit_count with current nid and field value hit_count.

To save value into my database table i will use hook_node_update and just get fields nid and hit_count values and update my table.

But i don't know what hook should i use or how to insert this value from database to my field. There are lots of nodes with content type feature.


My first thought is - why do you use a separate db table for this rather than simply using an integer field in the content type?

Hit count module was created long time ago, client wanted to disable/enable statistics for every content type, so default statistics module does not fit our needs. And now client want to manage this hit counts for certain nodes etc...

Also i managed to insert value to edit form on hook_exit updated custom hit count module.

function hit_count_exit($destination = NULL) {
    if ($node = menu_get_object()) {
        $selected_types = array_filter(variable_get('hit_count_content_types', array()));
        if (!empty($selected_types)) {
            if (in_array($node->type, $selected_types)) {
                        ->expression('totalcount', 'totalcount + 1')
                        ->condition('nid', $node->nid)
                if($node->type == 'feature') {
                    $val = db_query("SELECT * FROM {node_hit_counter} WHERE nid='$node->nid'")->fetchAll();
                        ->key(array('entity_id' => $node->nid))
                                'entity_type' => 'node',
                                'bundle' => 'feature',
                                'deleted' => 0,
                                'entity_id' => $node->nid,
                                'revision_id' => $node->nid,
                                'language' => 'und',
                                'delta' => 0,
                                'field_hit_count_node_value' => $val[0]->totalcount,


But right now i see next problem. I need to get rid of Drupal field cache, cuz i see cached value every time i reload page, and if i clear all caches i get current value of hit_count.

  • 2
    My first thought is - why do you use a separate db table for this rather than simply using an integer field in the content type?
    – mcaleaa
    Commented Mar 4, 2013 at 13:31
  • To update a field on your edit form, you can use hook_form_alter() api.drupal.org/api/drupal/modules%21system%21system.api.php/…
    – mcaleaa
    Commented Mar 4, 2013 at 13:32
  • updated my question, i solved one problem but got another) with drupal field cache i need to get rid of it Commented Mar 4, 2013 at 15:20

1 Answer 1


I also think you could use an integer field on the node for this - but keeping that aside, the problem you are facing (the inserted value not being refreshed by the cache) is caused by the fact that you are trying to write in the database table field_data_field_hit_count_node directly. This is definitely discouraged, and you should be using the proper API instead.

Instead of db_merge('field_data_field_hit_count_node')->...->execute() you would want to do something like:

$node->field_hit_count_node[LANGUAGE_NONE][0]['value'] = $val[0]->totalcount;

Doing it this way ensures that the saving is done by Drupal, the caches are cleared as required, hooks as called as necessary, etc.

In addition if you don't mind using the entity module, then you can do this with Entity Metadata Wrappers in a simpler way:

$nw = entity_metadata_wrapper('node', $node);
$nw->field_hit_count_node = $val[0]->totalcount;
  • 1
    gonna look at entity_metadata_wrappers Commented Mar 4, 2013 at 17:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.