Skip to content

Instantly share code, notes, and snippets.

@pixelwhip
Created July 14, 2012 16:20
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save pixelwhip/3111975 to your computer and use it in GitHub Desktop.
Save pixelwhip/3111975 to your computer and use it in GitHub Desktop.
Modular Stylesheets - Design 4 Drupal
<?php
/**
* @file
* Default theme implementation to display a block.
*
* Available variables:
* - $block->subject: Block title.
* - $content: Block content.
* - $block->module: Module that generated the block.
* - $block->delta: An ID for the block, unique within each module.
* - $block->region: The block region embedding the current block.
* - $classes: String of classes that can be used to style contextually through
* CSS. It can be manipulated through the variable $classes_array from
* preprocess functions. The default values can be one or more of the
* following:
* - block: The current template type, i.e., "theming hook".
* - block-[module]: The module generating the block. For example, the user
* module is responsible for handling the default user navigation block. In
* that case the class would be 'block-user'.
* - $title_prefix (array): An array containing additional output populated by
* modules, intended to be displayed in front of the main title tag that
* appears in the template.
* - $title_suffix (array): An array containing additional output populated by
* modules, intended to be displayed after the main title tag that appears in
* the template.
*
* Helper variables:
* - $classes_array: Array of html class attribute values. It is flattened
* into a string within the variable $classes.
* - $block_zebra: Outputs 'odd' and 'even' dependent on each block region.
* - $zebra: Same output as $block_zebra but independent of any block region.
* - $block_id: Counter dependent on each block region.
* - $id: Same output as $block_id but independent of any block region.
* - $is_front: Flags true when presented in the front page.
* - $logged_in: Flags true when the current user is a logged-in member.
* - $is_admin: Flags true when the current user is an administrator.
* - $block_html_id: A valid HTML ID and guaranteed unique.
*
* @see template_preprocess()
* @see template_preprocess_block()
* @see template_process()
*
* @ingroup themeable
*/
?>
<div id="<?php print $block_html_id; ?>" class="<?php print $classes; ?>"<?php print $attributes; ?>>
<?php print render($title_prefix); ?>
<?php if ($block->subject): ?>
<h2<?php print $title_attributes; ?>><?php print $block->subject ?></h2>
<?php endif;?>
<?php print render($title_suffix); ?>
<div <?php print $content_attributes; ?>>
<?php print $content ?>
</div>
</div>
<?php
/**
* !IMPORTANT: Replace 'modularstyle' with your theme name.
*/
/**
* BLOCKS
*/
/**
* Implements hook_preprocess_block()
*/
function modularstyle_preprocess_block(&$vars) {
/* Set shortcut variables. Hooray for less typing! */
$block_id = $vars['block']->module . '-' . $vars['block']->delta;
$classes = &$vars['classes_array'];
$title_classes = &$vars['title_attributes_array']['class'];
$content_classes = &$vars['content_attributes_array']['class'];
/* Add global classes to all blocks */
$title_classes[] = 'block-title';
$content_classes[] = 'block-content';
/* Uncomment the line below to see variables you can use to target a block */
// print $block_id . '<br/>';
/* Add classes based on the block delta. */
switch ($block_id) {
/* Main Menu block */
case 'menu_block-1':
$title_classes[] = 'element-invisible';
break;
/* Super Awesome block */
case 'boxes-super_awesome':
$content_classes[] = 'block-inverse';
$content_classes[] = 'block-rounded';
$title_classes[] = 'block-title-label';
break;
}
}
/**
* MENUS
*/
/**
* Overrides theme_menu_tree() by default.
*/
function modularstyle_menu_tree($variables) {
return '<ul class="nav nav-inline">' . $variables['tree'] . '</ul>';
}
/**
* Overrides theme_menu_tree() for the main menu.
*/
function modularstyle_menu_tree__main_menu($variables) {
return '<ul class="nav nav-stacked">' . $variables['tree'] . '</ul>';
}
/**
* Overrides theme_menu_tree() for the Main Menu Block.
*/
function modularstyle_menu_tree__menu_block__1($variables) {
return '<ul class="nav nav-inline nav-buttons">' . $variables['tree'] . '</ul>';
}
/**
* Implements hook_preprocess_menu_link()
*/
function modularstyle_preprocess_menu_link(&$vars) {
/* Set shortcut variables. Hooray for less typing! */
$menu = $vars['element']['#original_link']['menu_name'];
$mlid = $vars['element']['#original_link']['mlid'];
$item_classes = &$vars['element']['#attributes']['class'];
$link_classes = &$vars['element']['#localized_options']['attributes']['class'];
/* Add global classes to all menu links */
$item_classes[] = 'nav-item';
$link_classes[] = 'nav-link';
}
/**
* FIELDS
*/
/**
* Overrides theme_field()
* Remove the hard coded classes so we can add them in preprocess functions.
*/
function modularstyle_field($variables) {
$output = '';
// Render the label, if it's not hidden.
if (!$variables['label_hidden']) {
$output .= '<div ' . $variables['title_attributes'] . '>' . $variables['label'] . ':&nbsp;</div>';
}
// Render the items.
$output .= '<div ' . $variables['content_attributes'] . '>';
foreach ($variables['items'] as $delta => $item) {
$output .= '<div ' . $variables['item_attributes'][$delta] . '>' . drupal_render($item) . '</div>';
}
$output .= '</div>';
// Render the top-level DIV.
$output = '<div class="' . $variables['classes'] . '"' . $variables['attributes'] . '>' . $output . '</div>';
return $output;
}
/**
* Custom implementation of theme_field()
* Turns multivalued fields into a comma separated list.
* USAGE: $vars['theme_hook_suggestions'][] = 'field__custom_separated';
*/
function modularstyle_field__custom_separated($variables) {
$output = '';
// Render the label, if it's not hidden.
if (!$variables['label_hidden']) {
$output .= '<label ' . $variables['title_attributes'] . '>' . $variables['label'] . ':&nbsp;</label>';
}
// Render the items.
//$output .= '<div ' . $variables['content_attributes'] . '>';
$count = 1;
foreach ($variables['items'] as $delta => $item) {
$output .= '<span ' . $variables['item_attributes'][$delta] . '>' . drupal_render($item) . '</span>';
if ($count < count($variables['items'])) { $output .= ', '; }
$count++;
}
//$output .= '</div>';
// Render the top-level DIV.
$output = '<div class="' . $variables['classes'] . '"' . $variables['attributes'] . '>' . $output . '</div>';
return $output;
}
/**
* Implements hook_preprocess_field()
*/
function modularstyle_preprocess_field(&$vars) {
/* Set shortcut variables. Hooray for less typing! */
$field = $vars['element']['#field_name'];
$bundle = $vars['element']['#bundle'];
$mode = $vars['element']['#view_mode'];
$classes = &$vars['classes_array'];
$title_classes = &$vars['title_attributes_array']['class'];
$content_classes = &$vars['content_attributes_array']['class'];
$item_classes = array();
$base_class = drupal_clean_css_identifier($field);
/* Global field styles */
$classes = array($base_class);
$title_classes[] = $base_class . '-label';
$content_classes[] = $base_class . '-items';
$item_classes[] = $base_class . '-item';
/* Uncomment the lines below to see variables you can use to target a field */
// print '<strong>Field:</strong> ' . $field . '<br/>';
// print '<strong>Bundle:</strong> ' . $bundle . '<br/>';
// print '<strong>Mode:</strong> ' . $mode .'<br/>';
switch ($field) {
/* Example: Using an alternative theme function */
case 'field_tags':
$vars['theme_hook_suggestions'][] = 'field__custom_separated';
break;
}
// Apply odd or even classes along with our custom classes to each item */
foreach ($vars['items'] as $delta => $item) {
$item_classes[] = $delta % 2 ? 'odd' : 'even';
$vars['item_attributes_array'][$delta]['class'] = $item_classes;
}
}
/**
* NODES
*/
/**
* Implements hook_preprocess_node()
*/
function modularstyle_preprocess_node(&$vars) {
/* Set shortcut variables. Hooray for less typing! */
$type = $vars['type'];
$mode = $vars['view_mode'];
$classes = &$vars['classes_array'];
$title_classes = &$vars['title_attributes_array']['class'];
$content_classes = &$vars['content_attributes_array']['class'];
/* Example: Adding a classes base on View Mode */
// switch ($mode) {
// case 'photo_teaser':
// $classes[] = 'bg-white gutters-half l-space-trailing';
// break;
// }
}
/**
* FORMS
*/
/**
* Implements hook_form_alter
*/
function modularstyle_form_alter(&$form, &$form_state, $form_id) {
/* Add placeholder text to a form */
if ($form_id == 'search_block_form') {
$form['search_block_form']['#attributes']['placeholder'] = "Enter a search term…";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment