Skip to content

Instantly share code, notes, and snippets.

@joetower
Last active May 23, 2018 12:46
Show Gist options
  • Save joetower/5463126 to your computer and use it in GitHub Desktop.
Save joetower/5463126 to your computer and use it in GitHub Desktop.
Preprocess and template snippets related to front-end theming stuff.
Preprocess
// Slideshow wrapper to allow absolute placement of slideshow content//
function theme-name_preprocess_views_view_fields(&$vars, $hook){
if ($vars['view']->name == 'slideshow') {
$vars['fields']['title']->wrapper_prefix = '<div class="slideshow-content-wrapper">' . $vars['fields']['title']->wrapper_prefix;
$vars['fields']['field_slide_button_link']->wrapper_suffix .= '</div>';
}
}
Do the same thing in a views-row-style template:
<?php foreach ($fields as $id => $field) :
// only print the image, version number and body here
if($id == 'field_slide_image' ) :
if (!empty($field->separator)):
print $field->separator;
endif;
print $field->wrapper_prefix;
print $field->label_html;
print $field->content;
print $field->wrapper_suffix;
endif;
endforeach; ?>
<?php foreach ($fields as $id => $field) :
// only print the image, version number and body here
if($id == 'field_display_title' || $id == 'body' || $id == 'field_slide_link' || $id == 'edit_node' ) :
if (!empty($field->separator)):
print $field->separator;
endif;
print $field->wrapper_prefix;
print $field->label_html;
print $field->content;
print $field->wrapper_suffix;
endif;
endforeach; ?>
// Hide the page title on Consulting Services nodes, so we can print it in the node template
function THEME_NAME_preprocess_page(&$vars) {
$vars['show_title'] = !isset($vars['node']) || (isset($vars['node']) && $vars['node']->type !== 'consulting_services');
}
//Print in Page template
<?php if ($title && $show_title == true): ?>
<h1 class="title" id="page-title">
<?php print $title; ?>
</h1>
<?php endif; ?>
//Print in Node Template normally
<?php if ($title): ?>
<h1 class="title" id="page-title">
<?php print $title; ?>
</h1>
<?php endif; ?>
/** Preprocess Entities **/
function THEMENAME_preprocess_entity(&$variables) {
foreach($variables['theme_hook_suggestions'] as &$suggestion) {
$suggestion = 'entity__' . $suggestion;
}
}
/**
*********************************
*********************************
*********************************
Preprocess Beans - enable bean block template **/
/**
* Implements template_preprocess_block
*
* enable bean blocks to be themed by bean-type:
* i.e. block__bean__BEAN-TYPE.tpl
*/
function THEMENAME_preprocess_block(&$variables) {
$block = $variables['block'];
$block_module = $block->module;
$elements = $variables['elements'];
// Add Bean Type to Theme Hook Suggestions
// enable bean blocks to be themed by bean-type: i.e. block__bean__BEAN-TYPE.tpl
// select Bean Blocks and ignore other Blocks
if (!empty($block_module) && ($block_module == 'bean')) {
// the location of the Bean array within $variables is a moving target
// e.g. use of the Context Module will change it's location to within $elements['content']
// try the likely locations first before recursively iterating through $elements
if (array_key_exists('bean', $elements)) {
$bean_array = $elements['bean'];
} else if (array_key_exists('bean', $elements['content'])) {
$bean_array = $elements['content']['bean'];
} else {
$bean_array = array_find_first_value('bean', $elements);
}
// test that the Bean array has been found
if ($bean_array) {
$mystery_key_array = element_children($bean_array);
if ($mystery_key_array) {
$bean = $bean_array[$mystery_key_array[0]];
if (!empty($bean['#bundle'])) {
$variables['theme_hook_suggestions'][] = 'block__' . $block_module . '__' . $bean['#bundle'];
}
}
}
}
/***
*********************************
*********************************
*********************************
Print custom block content in a .tpl file
***/
$block = module_invoke('block','block_view','1');
print render($block['content']);
/***
*********************************
*********************************
*********************************
Print Views block content in a .tpl file
***/
<div class="block related-content">
<h2 class="block-title">You may also enjoy reading:</h2>
<?php print views_embed_view('blog','block_3'); ?>
</div><!--/.related-content-->
@jessehs
Copy link

jessehs commented Apr 28, 2014

Thanks for the bean-handling in preprocess_block! The function array_find_first_value() is not defined, however.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment