Create a custom block that displays result of EFQ with custom templates

Public

myproject.module: Use this to setup theme functions and bundle everything else

Get raw version
php
  1. <?php
  2. /**
  3.  * All code assumes your module is named "myproject"
  4.  */
  5. define('TEMPLATES_PATH', drupal_get_path('module', 'myproject') . '/templates');
  6.  
  7. include_once 'myproject.blocks.inc';
  8.  
  9. /**
  10.  * Theme functions
  11.  */
  12.  
  13. /**
  14.  * Implementation of hook_theme()
  15.  * Note to reader:
  16.  * - in myproject_list_item, add each variable that you need to display in the item
  17.  * - after creating this function you will need to clear drupal's cache so that these new theme functions are registered
  18.  */
  19.  
  20. function myproject_theme() {
  21. return array(
  22. 'myproject_list' => array(
  23. 'path' => TEMPLATES_PATH,
  24. 'template' => 'myproject-list',
  25. 'variables' => array(
  26. 'items' => array(),
  27. ),
  28. ),
  29. 'myproject_list_item' => array(
  30. 'path' => TEMPLATES_PATH,
  31. 'template' => 'myproject-list-item',
  32. 'variables' => array(
  33. 'type' => array(),
  34. 'title' => array(),
  35. 'snippet' => array(),
  36. 'date_string' => array(),
  37. ),
  38. ),
  39. );
  40. }

myproject.blocks.inc: Keep block code separate for cleanliness

Get raw version
php
  1. <?php
  2.  
  3. /**
  4.  * Implements hook_block_info().
  5.  */
  6. function myproject_block_info() {
  7. $blocks['BLOCK_ID'] = array(
  8. 'info' => t('BLOCK ADMIN NAME'),
  9. 'cache' => DRUPAL_CACHE_PER_PAGE,
  10. );
  11. /** other blocks can be added here **/
  12.  
  13. return $blocks;
  14. }
  15.  
  16. /**
  17.  * Implements hook_block_view().
  18.  */
  19. function myproject_block_view($delta = '') {
  20. $block = array();
  21.  
  22. switch ($delta) {
  23. case 'BLOCK_ID':
  24. $block['title'] = "BLOCK TITLE";
  25. $block['content'] = _prepare_content();
  26. break;
  27. }
  28. /** add other cases for additional blocks **/
  29. return $block;
  30. }
  31.  
  32. /**
  33.  * Helper functions
  34.  */
  35.  
  36. /**
  37.  * Get the data from a entity field query so that we can use it later.
  38.  */
  39. function _get_data() {
  40. $query = new EntityFieldQuery();
  41. $node_types = array('article', 'page'); // the specific content types you want results for
  42.  
  43. $query->entityCondition('entity_type', 'node')
  44. ->entityCondition('bundle', $node_types, 'IN')
  45. ->propertyCondition('status', 1) // Only published nodes
  46. ->range(0, 10) // limit to 10 results
  47. ->addMetaData('account', user_load(1)); // Run the query as user 1.
  48.  
  49. $results = $query->execute();
  50. /**
  51.   * Get the data for events and webinars.
  52.   * we need to display
  53.   */
  54. return $results;
  55. }
  56.  
  57. /**
  58.  * Create a View Model and tell Drupal how to render it.
  59.  */
  60. function _prepare_content() {
  61. $results = _get_data();
  62. $nids = array_keys($results['node']);
  63. $nodes = node_load_multiple($nids);
  64.  
  65. // Prepare each node
  66. $render_items = array();
  67. foreach ($nodes as $node) {
  68. // Common loading to the View Model
  69. $item = array(
  70. '#theme' => 'myproject_list_item',
  71. '#title' => $node->title,
  72. '#snippet' => $node->body,
  73. );
  74.  
  75. // Content-type specific loading to the View Model
  76. if ($node->type == 'article') {
  77. $node_date = $node->field_date['und'][0]['value'];
  78. $item['#date_string'] = format_date($node_date, 'custom', 'M d, Y');
  79. $item['#type'] = "Article";
  80. }
  81.  
  82. // Content-type specific loading to the View Model
  83. if ($node->type == 'page') {
  84. dpm('you found a page');
  85. }
  86.  
  87. $render_items[] = $item;
  88. }
  89.  
  90. // Return a renderable myproject_list
  91. return array(
  92. '#theme' => 'myproject_list',
  93. '#items' => $render_items,
  94. );
  95. }

myproject-list.tpl.php

Get raw version
php
  1. <?php
  2. /**
  3.  * Provides markup that encapulates a list of items
  4.  *
  5.  * @var array $items;
  6.  */
  7. ?>
  8. <div class="myproject-list">
  9. <?php foreach ($items as $item): ?>
  10. <?php print render($item); ?>
  11. <?php endforeach; ?>
  12. </div>

myproject-list-item.tpl.php

Get raw version
php
  1. <?php
  2. /**
  3.  * Displays an individual item
  4.  *
  5.  * @var string $type
  6.  * @var string $title
  7.  * @var string $snippet
  8.  * @var string $date_string
  9.  */
  10. ?>
  11. <div class="myproject-list-item">
  12. <div class="myproject-list-item-type"><?php print render($type); ?></div>
  13. <ul class="myproject-list-item-content">
  14. <?php if($title): ?>
  15. <li class="myproject-list-item-title"><?php print render($title); ?></li>
  16. <?php endif; ?>
  17. <?php if(trim($snippet)): ?>
  18. <li class="myproject-list-item-snippet"><?php print render($snippet); ?></li>
  19. <?php endif; ?>
  20. <?php if($date_string): ?>
  21. <li class="myproject-list-item-date"><?php print render($date_string); ?></li>
  22. <?php endif; ?>
  23. </ul>
  24. </div>

Comments