0

I'd essentially like to alter the values of a specific custom field of a few of nodes before they're displayed in a view on the 'page' of a specific content type.

The content type is that will be displayed is a park. When a park is viewed, one of the blocks shown is a view that fetches and shows several nodes that are also 'parks'.

This park content type has a field called proximity. I'd like to change the value of proximity (based on a mathematical formula) for all parks before the view is processed, so that I can use proximity in the view.

From what I understand so far, I should use a park_preprocess function, which calls some sort of hook to change the proximity values. Is this correct, and how would I do it?

3 Answers 3

0

hook_node_view() will be used in this scenario.

function hook_node_view($node, $view_mode, $langcode) {
  if($node->type == 'park' && $view_mode == 'full')
   {
    /* change the value of proximity here after applying your formula. 
      * Here you have complete node array, so you alter the result easily here.
    */

   }
 }
0

you can use hook_node_view as @Pushpinder Rana said.but if you want to modify a field you should save your node.here i simply plus one my proximity field on my hook.

/**
 * Impelement hook_node_view
 * @param type $node
 * @param type $view_mode
 * @param type $langcode
 */
function MODULENAME_node_view($node, $view_mode, $langcode) {
    //plus one proximity field and save a node
    $new_value = $node->field_proximity[LANGUAGE_NONE][0]['value'] + 1;
    $node->field_proximity[LANGUAGE_NONE][0]['value'] = $new_value;
    node_submit($node);
    node_save($node);
    // here is to show user updated proximity value;    
    $node->content['field_proximity'][0]['#markup'] = $new_value;
    }
2
  • Can this be called from within template.php in my theme, or must I create a module to implement this? Commented Jun 22, 2014 at 9:30
  • Drupal themes can only implement theme functions (which include template preprocess and process functions) or alter hooks[drupal.org/node/254940#theme_alter]. You should create a module to call this hook. Commented Jun 22, 2014 at 11:10
0

Thanks to Pushinder and Shirin for responding, and providing the start I needed! However both answers were lacking the detail I needed. I won't paste my entire code here, as it's too long, but I'll describe what I used and why:

  • hook_node_view was the right hook to call my function at the right time - before page load
  • $refN = $node->field_location_n[LANGUAGE_NONE][0]['value']; and the same for East were required to get the reference coordinates
  • Because I have several hundred nodes with coordinates, I couldn't load each one individually using load_node or collectively using load_node_multiple since this used far too many resources for every page load
  • Instead, I used db_query and field_attach_load together, as described in this answer: https://drupal.stackexchange.com/a/30906/20264 to effectively load just the values I needed
  • I then used foreach( $nodes as $nodei ) { to loop through all the results
  • $distance = sqrt( pow( $refN - $nodeiN, 2 ) + pow( $refE - $nodeiE, 2 ) ); was my proximity calculation
  • Then, only for results with a specified proximity would I update $node
  • Since I was using the Entity Reference module to record 'Nearby places' in my nodes, I used the following to update the entity references:

    $node->field_nearby[LANGUAGE_NONE][$i] = array( "target_id" => $nodei->nid );

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.