Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
A template.php file for a responsive Drupal custom theme, showing some data processing magic to make coding easier on the front-end.
<?php
function clearesult_subsolar_preprocess_html(&$variables) {
drupal_add_css(drupal_get_path('theme', 'clearesult_subsolar') .'/build/scripts/vendor/pure.css', array('group' => CSS_SYSTEM));
drupal_add_css('//fonts.googleapis.com/css?family=Cabin:400,600', array('group' => CSS_SYSTEM));
}
function clearesult_subsolar_preprocess_page(&$variables) {
drupal_add_js(drupal_get_path('theme', 'clearesult_subsolar') .'/build/scripts/vendor/modernizr-2.8.3.min.js', array("group" => JS_LIBRARY, "weight" => 115));
drupal_add_js(drupal_get_path('theme', 'clearesult_subsolar') .'/build/scripts/vendor/jquery-maskedinput.min.js', array("group" => JS_LIBRARY, "weight" => 115));
drupal_add_js(drupal_get_path('module', 'subsolar_form') . '/js/plugins/jquery.slidercalculate.js', array("group" => JS_LIBRARY, "weight" => 115));
$variables['theme_path'] = drupal_get_path('theme', 'clearesult_subsolar');
}
function clearesult_subsolar_preprocess_node(&$variables) {
if (empty($variables['nid'])) {
return;
}
//Store the NID and node type for later
$nid = $variables['nid'];
$type = $variables['type'];
//store waitlist value
$waitlist = false;
if(theme_get_setting('waitlist_settings') == 'enabled'){
$waitlist = true;
}
$variables['waitlist'] = $waitlist;
// Wrap node with Entity API
$node_wrapper = entity_metadata_wrapper('node', $nid);
switch($type) {
case 'home_page':
//First the carousel
// Get the first item from the muli-value field collection
$raw_collection = $node_wrapper->field_hero_carousel[0]->value();
// Wrap field collection with Entity API
$carousel_collection = entity_metadata_wrapper('field_collection_item', $raw_collection);
//Hero carousel elements in variables for printing in the template
$variables['c_heading'] = $carousel_collection->field_heading->value();
$variables['c_desc'] = $carousel_collection->field_descripton[0]->value();
$images = $carousel_collection->field_image->value();
$i = 0;
foreach ($images as $img) {
$img_path = image_style_url('hero', $img['uri']);
$variables['heroes'][$i] = $img_path;
$i++;
}
//Next Call to Action Section
if($waitlist == true) {
$raw_collection_cta = $node_wrapper->field_call_to_action[1]->value();
} else {
$raw_collection_cta = $node_wrapper->field_call_to_action[0]->value();
}
$cta_collection = entity_metadata_wrapper('field_collection_item', $raw_collection_cta);
$variables['cta_heading'] = $cta_collection->field_heading->value();
$variables['cta_desc'] = $cta_collection->field_descripton[0]->value();
$buttons = $raw_collection_cta->field_button['und'];
$b = 0;
foreach ($buttons as $btn) {
$variables['btns'][$b] = $btn['value'];
$variables['btn_titles'][$b] = $btn['title'];
$b++;
}
break;
case 'landing_page':
//Residential or commercial
$customer_type = $node_wrapper->field_customer_type->value();
$variables['customer_type'] = $customer_type;
//solar benefits icons
$raw_collection_icon = $node_wrapper->field_icon_set->value();
$icon_collection = entity_metadata_wrapper('field_collection_item', $raw_collection_icon);
$variables['icon_heading'] = $icon_collection->field_heading->value();
$icons = $icon_collection->field_image->value();
$i = 0;
foreach ($icons as $icon) {
$img_path = image_style_url('medium', $icon['uri']);
$variables['icons'][$i] = $img_path;
$variables['icon_title'][$i] = $icon['title'];
$i++;
}
$descs = $icon_collection->field_descripton->value();
$b = 0;
foreach ($descs as $desc) {
$variables['icon_descs'][$b] = $desc;
$b++;
}
//Next Call to Action Section
$raw_collection_cta = $node_wrapper->field_call_to_action[0]->value();
$cta_collection = entity_metadata_wrapper('field_collection_item', $raw_collection_cta);
$variables['cta_heading'] = $cta_collection->field_heading->value();
$variables['cta_desc'] = $cta_collection->field_descripton[0]->value();
$buttons = $raw_collection_cta->field_button[LANGUAGE_NONE];
if($buttons) {
$b = 0;
foreach ($buttons as $btn) {
$variables['btns'][$b] = $btn['value'];
$variables['btn_titles'][$b] = $btn['title'];
$b++;
}
}
break;
case 'confirmation_page':
//more ways to support icons
$raw_collection_icon = $node_wrapper->field_icon_set->value();
$icon_collection = entity_metadata_wrapper('field_collection_item', $raw_collection_icon);
$variables['icon_heading'] = $icon_collection->field_heading->value();
$icons = $icon_collection->field_image->value();
$i = 0;
foreach ($icons as $icon) {
$img_path = image_style_url('medium', $icon['uri']);
$variables['icons'][$i] = $img_path;
$variables['icon_title'][$i] = $icon['title'];
$i++;
}
$descs = $icon_collection->field_descripton->value();
$b = 0;
foreach ($descs as $desc) {
$variables['icon_descs'][$b] = $desc;
$b++;
}
break;
}
}
/**
* Implements hook_css_alter().
* Forces style.css to be included with <link> tag instead of drupals default @import
* - because browserSync doesn't support @import.
* Code from: https://www.drupal.org/project/link_css
*/
function clearesult_subsolar_css_alter(&$css) {
$exclude = array(
'modules/comment/comment.css' => FALSE,
'modules/node/node.css' => FALSE,
'modules/search/search.css' => FALSE,
'modules/system/system.menus.css' => FALSE,
'modules/system/system.messages.css' => FALSE,
'modules/system/system.theme.css' => FALSE,
'modules/taxonomy/taxonomy.css' => FALSE,
'modules/user/user.css' => FALSE,
'modules/shortcut/shortcut.css' => FALSE,
'modules/field/theme/field.css' => FALSE,
'sites/all/modules/ctools/css/ctools.css' => FALSE,
);
$css = array_diff_key($css, $exclude);
$count = 0;
foreach ($css as $key => $value) {
// Skip core files.
$is_core = (strpos($value['data'], 'misc/') === 0 || strpos($value['data'], 'modules/') === 0);
if (!$is_core) {
// This option forces embedding with a link element.
$css[$key]['preprocess'] = FALSE;
$count++;
}
}
}
//helper function to add classes to form elements
function _clearesult_subsolar_form_add_wrapper(&$element, $classes = array(), $type = array('form-item', 'form-type-textfield'), $removeTitle = FALSE){
$element['#prefix'] = '<div class="' . implode(" ", $classes) . '">';
$element['#suffix'] = '</div>';
}
function clearesult_subsolar_form_alter(&$form, &$form_state, $form_id){
//classes to style forms
$form['#attributes']['class'][] = 'pure-form';
$form['#attributes']['class'][] = 'custom-form';
$form['#attributes']['id'][] = 'content';
//adding classes to submit buttons to make them pretty
$form['actions']['submit']['#attributes']['class'][] = 'pure-button';
$form['actions']['submit']['#attributes']['class'][] = 'button-custom';
//more helper classes for pretty forms, specifically for the waitlist and commercial sign up forms
if($form_id == 'webform_client_form_4' || $form_id == 'webform_client_form_6') {
$form['#attributes']['class'][] = 'pure-form-stacked';
$form['#prefix'] = '<div class="pure-g">';
$form['#suffix'] = '</div>';
//Add classes to individual form fields for theming
$fields = $form['submitted'];
foreach($fields as $key => $value) {
if(is_array($value)) {
_clearesult_subsolar_form_add_wrapper($form['submitted'][$key], array('cl-u1', 'cl-uxl-12-12'));
}
}
//Adds a value to the webform that will show up in exports if waitlist is enabled
if(theme_get_setting('waitlist_settings') == 'enabled'){
$form['submitted']['waitlist_enabled']['#default_value'] = t('On Waitlist');
}
}
}
//Converts search box form input field from text to HTML5 search
function clearesult_subsolar_preprocess_search_block_form(&$vars) {
$vars['search_form'] = str_replace('type="text"', 'type="search"', $vars['search_form']);
}
//Add a custom template for the subsolar residential sign up form for easier styling
function clearesult_subsolar_theme($existing, $type, $theme, $path) {
$items['subsolar_form'] = array(
'render element' => 'form',
'template' => 'subsolar_form',
'path' => drupal_get_path('module', 'subsolar_form') . '/templates',
);
$items['subsolar_seasonal_form'] = array(
'render element' => 'form',
'template' => 'subsolar_seasonal_form',
'path' => drupal_get_path('module', 'subsolar_seasonal_form') . '/templates',
);
return $items;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment