Created
September 18, 2013 06:50
-
-
Save fieke/6605414 to your computer and use it in GitHub Desktop.
Make custom block in custom module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Implementation of hook_block_info(). | |
*/ | |
function custom_block_info() { | |
$blocks = array(); | |
// Products overview | |
$blocks['product-overview'] = array( | |
'info' => t('Product overview'), | |
); | |
return $blocks; | |
} | |
/** | |
* Implementation of hook_block_view(). | |
*/ | |
function custom_block_view($delta='') { | |
$block = array(); | |
global $language; | |
switch ($delta) { | |
case 'product-overview': | |
$block['subject'] = t('Product overview'); | |
$block['content'] = _product_overview(); | |
break; | |
} | |
return $block; | |
} | |
/* Product overview */ | |
function _product_overview() { | |
global $language; | |
$query = db_select('node', 'n'); | |
$node_results = $query->condition('language', $language->language, '=') | |
->condition('type', 'product', '=') | |
->fields('n', array('nid')) | |
->execute(); | |
if ($node_results) { | |
$nids = array(); | |
foreach ($node_results as $node_result) { | |
$nids[] = $node_result->nid; | |
} | |
if (!empty($nids)) { | |
$nodes = node_load_multiple($nids); | |
$node_view = node_view_multiple($nodes, 'teaser'); | |
return array( | |
array( | |
'#theme' => 'overview_product', | |
'#nodes' => $node_view['nodes'], | |
), | |
); | |
; | |
} | |
} | |
return NULL; | |
} | |
function custom_theme() { | |
return array( | |
'overview_product' => array( | |
'variables' => array( | |
'nodes' => NULL | |
), | |
'template' => 'overview-product' | |
), | |
); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$max = count(element_children($nodes)); | |
// Set up striping values. | |
$count = 0; | |
?> | |
<ul> | |
<?php foreach (element_children($nodes) as $node) { | |
$count++; | |
$attributes = array(); | |
$attributes['class'][] = '' . ($count % 2 ? 'odd' : 'even'); | |
if ($count % 3 == 0) { | |
$attributes['class'][] = 'third'; | |
} | |
if ($count % 4 == 0) { | |
$attributes['class'][] = 'fourth'; | |
} | |
?> | |
<li <?php print drupal_attributes($attributes); ?>><?php print render($nodes[$node]); ?></li> | |
<?php } ?> | |
</ul> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment