drupaldrupal-7drupal-preprocess

Drupal 7 preprocess single node


Is possible create a preprocess for single node? For example, this is generic for all node:

function template_preprocess_node(&$variables) {}

we can also call something for single node?like this? i have tried various solution but not work.

function template_preprocess_node__mynode(&$variables) {}

Solution

  • I would accomplish this using the same task that zen uses.

    function mytheme_preprocess_node(&$variables, $hook) {
    
      // Optionally, run node-specific preprocess functions, like
      // mytheme_preprocess_node_1().
      $function = __FUNCTION__ . '_' . $variables['node']->nid;
      if (function_exists($function)) {
        $function($variables, $hook);
      }
    }
    

    Thanks to the if (function_exists($function)) bit you can simply implement a function for whichever specific nids you wish and it will find and execute them. Just make sure that you pass in the &$variables parameter as a reference (with the &) or else none of your changes will find their way out of the function.