I have a node template that grabs fields from a content type.
In the node--[contenttype].tpl.php file I have two sections (The second is html/php and grabs variables from the first section or from the content type and simply displayed it). The first is strictly php and does three things:
- Grabs numbers from one of the content type fields and rearranges them according to some criteria. Setting this output as a variable to be used later in the page.
- Grabs two taxonomy terms and sets them as variables to be used later in the page. Also compares these variables to an array as the key in order to set the value as another variable to be used later in the page.
- Creates a function for converting stdClass Objects to Arrays and uses it in one of the above two actions.
My question is, should this php even go in the node--[contenttype].tpl.php file? The numbers output and taxonomy output is only ever going to be used in this node (the arrays set for comparison are only relevant with this content type), but the Class function could be used on other pages.
I'm not getting how I can leverage the Drupal system in order to get a strong and easily upgradable code base (for going to Drupal 8 in the future).
How should my mind be processing executable challenges. Should I be asking certain questions in order to determine if something goes into template.php, into a new module, or where its css goes? Should all that css be going into style.css? Can I break it out somehow?
EDIT: Here is my code for node--[contenttype].tpl.php
<?php
/////**** set link and text for Category and Location section ****////////
$category_to_nid = array(
"Professional Development" => 34,
"Curriculum" => 35,
"Study Labs" => 36,
"Stage Performances" => 37,
);
$location_to_nid = array(
"Visit Examples" => 38,
"We Come To You" => 39);
// to array (from stdClass object)
// for field_location_category to get name
// without it, get white screen of death because ['taxonomy_term'] is an object not array
// Credit: http://www.if-not-true-then-false.com/2009/php-tip-convert-stdclass-object-to-multidimensional-array-and-convert-multidimensional-array-to-stdclass-object/
function objectToArray($d) {
if (is_object($d)) {
// Gets the properties of the given object
// with get_object_vars function
$d = get_object_vars($d);
}
if (is_array($d)) {
/*
* Return array converted to object
* Using __FUNCTION__ (Magic constant)
* for recursive call
*/
return array_map(__FUNCTION__, $d);
}
else {
// Return array
return $d;
}
}
$location_text_array = objectToArray($node->field_location_category['und'][0]['taxonomy_term']);
$location_text = $location_text_array['name'];
$category_text_array = objectToArray($node->field_location_category['und'][1]['taxonomy_term']);
$category_text = $category_text_array['name'];
$location_link = "http://www.server.com/node/" . $location_to_nid[$location_text];
$category_link = "http://www.server.com/node/" . $category_to_nid[$category_text];
/////**** Check if new category or location ****///////
$display_category_tab = TRUE;
$display_location_tab = TRUE;
if ($category_text != $category_text_previous){
$display_category_tab = TRUE;
}
else{ $display_category_tab = FALSE;}
if ($location_text != $location_text_previous){
$display_location_tab = TRUE;
}
else{ $display_location_tab = FALSE;}
/*print $location_text . "<br />";
print $location_text_previous . "<br />";
print $category_text . "<br />";
print $category_text_previous . "<br />";
if($display_location_tab){print "Location TAB" . "<br />";}
if($display_category_tab){print "Category TAB" . "<br />";}
*/
$location_text_previous = $location_text;
$category_text_previous = $category_text;
/////**** set Taxonomy Term Color ****///////
$color_category_array = array(
196 => "orange", // "Inspiration for Users"
177 => "green", // "Visit Examples"
178 => "blue", // "We Come to You"
197 => "red", // "Content for Homeschoolers"
198 => "grey" // "School Partnerships"
);
$tid_array = array();
for ($i=0; $i < count($node->field_location_category['und']); $i++):
$tid_array[] = $node->field_location_category['und'][$i]['tid'];
endfor;
$taxonomy_terms = array_intersect($tid_array, array(196, 177, 178, 197, 198));
$taxonomy_color_class = $color_category_array[$taxonomy_terms[0]];
// Use the following for setting class for color: $color_category_array[$taxonomy_term];
/////**** set Grades $display variable ****///////
if(!empty($content['field_grades'])):
// Grab Grades string and create into array
// note: because not able to grab as array
$grades_string = render($content['field_grades']);
$grades_string = str_replace("Adult", "13", $grades_string);
$grades_string = str_replace("K", "00", $grades_string);
$new_grades_array = str_split($grades_string, 2);
// loop for creating $display string based on consecutives
for ($i = 0; $i < count($new_grades_array); $i++):
// set grade digit for K and Adult
switch ($new_grades_array[$i]):
case "K":
$currentgrade_dig = 0;
break;
case "Adult":
$currentgrade_dig = 13;
break;
default:
$currentgrade_dig = $new_grades_array[$i];
endswitch;
// concatenates string to display variable based on situation
if ($i == 0): // if the first grade listed
$display = $new_grades_array[$i];
elseif ($i +1 == count($new_grades_array)): //if the last grade listed
if ($currentgrade_dig - $last ==1):
$display .= " - " . $new_grades_array[$i];
else:
if ($conseq != FALSE):
$display .= " - " . $last . ", " . $new_grades_array[$i];
else:
$display .= ", " . $new_grades_array[$i];
endif;
endif;
$conseq = FALSE;
elseif ($currentgrade_dig - $last ==1): // if consecutive number from previous
$conseq = TRUE;
else: // if not a consecutive number
if ($conseq != FALSE):
$display .= " - " . $last . ", " . $new_grades_array[$i];
$conseq = FALSE;
else:
$display .= ", " . $new_grades_array[$i];
endif;
endif;
$last = $new_grades_array[$i];
endfor;
$display = str_replace("13", "Adult", $display);
$display = str_replace("00", "K", $display);
$display = str_replace(" 0", " ", $display);
if($display[0]=="0"):
$display = substr($display, 1, strlen($display));
endif;
$display = "Grades " . $display;
else:
$display = "All Ages";
endif;
?>
<?php
/**
* <div id="node-<?php print $node->nid; ?>" class="<?php print $classes; ?> block_1_column clearfix"<?php print $attributes; ?>>
*/
// Check if published
if($content['body']['#object']->status || $user->uid > 0):
?>
<!--<?php if(isset($content['field_3column_name'])): ?>
<h2 class="title-main"><?php print render($content['field_3column_name']); ?></h2>
<?php endif; ?>-->
<div <?php if(!isset($content['field_3column_icons'])): ?>class="no-icons"<?php endif; ?>>
<div class="query-item">
<div class="query-title big_header_dark-red query-<?php print $taxonomy_color_class; ?>"><?php print $title; ?></div>
<div class="query-where">
<a href="<?php print $location_link; ?>" class=""><?php print $location_text; ?></a>
>
<a href="<?php print $category_link; ?>" class=""><?php print $category_text; ?></a>
</div>
<div class="query-basics-group">
<span><?php print $display; ?></span>
<?php if(!empty($content['field_duration'])): ?><span><?php print render($content['field_duration']); ?></span><?php endif; ?>
<?php if(!empty($content['field_capacity'])): ?><span><?php print render($content['field_capacity']); ?></span><?php endif; ?>
<?php if(!empty($content['field_availability'])): ?><span><?php print render($content['field_availability']); ?></span><?php endif; ?>
</div>
<?php if(isset($content['field_cost']) || isset($content['field_subject_area']) || isset($content['field_note'])): ?>
<div class="query-basics-group">
<?php if(!empty($content['field_cost'])): ?><span><?php print render($content['field_cost']); ?></span><?php endif; ?>
<?php if(!empty($content['field_subject_area'])): ?><span><?php print render($content['field_subject_area']); ?></span><?php endif; ?>
<?php if(!empty($content['field_note'])): ?><span><?php print render($content['field_note']); ?></span><?php endif; ?>
</div><?php endif; ?>
<div class="query-description"><?php
// render(field_view_filed()); instead of just render($content['body']); kept the WYSIWYG formatting
print render(field_view_field('node', $node, 'body')); ?>
</div>
<?php if(!empty($content['field_register_link'])): ?>
<div class="query-link"><?php
// render(field_view_filed()); instead of just render(); kept the WYSIWYG formatting
print render($content['field_register_link']); ?>
</div><?php endif; ?>
</div>
<div class="clear"></div>
</div>
<?php if(isset($content['field_gallery'])): ?>
<?php print render($content['field_gallery']); ?>
<?php endif; ?>
<?php if(isset($content['field_3column_icons'])): ?>
<div class="icons"><div class="icons-highlight"><?php print render($content['field_3column_icons']); ?></div></div>
<?php endif; ?>
<?php
else:
endif;
//dsm("Charity:<pre>" . print_r($content['field_location_category'], TRUE) . '</pre>');
//dsm("Charity:<pre>" . print_r($taxonomy_terms, TRUE) . '</pre>');
?>