I only just had a similar issue which is why google brought me to this page:
my node preprocess function was growing so enormously big, that i would rather split the function into multiple files.
I've already done a similar approach in my template.php file which contains all the alter functions, and since the same method works perfectly well here, I thought I'd share my approach:
file setup inside the folder MYTHEME/preprocess
:
- node.preprocess.inc
- node--blog-post.preprocess.inc
- node--device-variation.preprocess.inc
- (...)
you should already have node.preprocess.inc
, the others you can create yourself. how you call them really is rather arbitrary, but better give them names that nicely identify them and fit the whole drupal naming system.
onwards to the content of these files!
node.preprocess.inc
, here I'm doing doing something like this:
<?php
function MYTHEME_preprocess_node(&$variables) {
switch($variables['type']) {
case 'blog_post':
// if the type of the node is a Blog Post, include this:
include 'node--blog-post.preprocess.inc';
break;
case 'device_variation':
// if Device Variation, include this:
include 'node--device-variation.preprocess.inc';
break;
case 'foo':
// ...
break;
}
// additional stuff for all nodes
}
we basically switch through the type of the current node. what you switch through is up to you; #id
, #view_mode
, all depending on your exact needs.
once there is a match, it will load the specified file and act upon its content just as if it was written right inside this function.
the content of these included
files looks exactly like you'd put it in the node.preprocess.inc
file, except we don't call the preprocess function again:
node--device-variation.preprocess.inc
<?php
// Device Name
$device = drupal_clean_css_identifier(strtolower($variables['title']));
// Determine whether only Device Version is of type 'N/A' and set ppvHasVariations accordingly
$deviceHasVariations = true;
if( $variables['content']['product:field_model_variation'][0]['#options']['entity']->weight == 0 ) {
$deviceHasVariations = false;
}
//...
you can basically do this with as many files as you want and even cascade multiple switches, for example further splitting up specific node preprocess files depending on the #view_mode
, having one file for the full
view mode and another for the teaser
hope this helps, should someone ever stumble upon this question again (: