dr821.theme 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /**
  3. * Implements hook_block_view_alter().
  4. *
  5. * Convert blocks within the footer region to Bootstrap panels.
  6. *
  7. * @param array $build
  8. * The block view render array to alter.
  9. *
  10. * @see dr821m_block_view_alter()
  11. */
  12. function _dr821_block_view_alter(array &$build) {
  13. /** @var \Drupal\block\Entity\Block $blockEntity */
  14. $blockEntity = $build['#block'];
  15. $region = $blockEntity->getRegion();
  16. if ($region !== 'footer') {
  17. return;
  18. }
  19. // Request wrapping the block title and content in appropriate classes.
  20. $build['#pre_render'][] = '_dr821_prerender_block';
  21. // Wrap the overall block in panel classes.
  22. if (!isset($build['#attributes']['class'])) {
  23. $build['#attributes']['class'] = [];
  24. }
  25. $build['#dr821_panelize_title_va'] = TRUE;
  26. $build['#attributes']['class'] += ['panel', 'panel-default', 'col-xs-6'];
  27. }
  28. /**
  29. * Pre-render function to request wrapping of block parts.
  30. *
  31. * @param array $block
  32. *
  33. * @return mixed
  34. */
  35. function _dr821_prerender_block($block) {
  36. // Wrap the block body in a panel-body container div.
  37. if (!empty($block['content'])) {
  38. $block['content']['#theme_wrappers'] = [
  39. 'container' => [
  40. '#attributes' => [
  41. 'class' => ['panel-body'],
  42. ],
  43. ],
  44. ];
  45. }
  46. // Tell the preprocess function to wrap the block title as a panel heading
  47. $block['#dr821_panelize_title'] = TRUE;
  48. return $block;
  49. }
  50. /**
  51. * Wrap the block title as a panel heading with title.
  52. *
  53. * @param array $elements
  54. */
  55. function _dr821_panelize_block_title(&$elements) {
  56. // Do not just assign prefix/suffix: it would lose contextual information.
  57. $elements['title_prefix']['#markup'] = '<div class="panel-heading">';
  58. $elements['title_suffix']['#markup'] = '</div>';
  59. $elements['title_attributes']['class'][] = 'panel-title';
  60. }
  61. /**
  62. * Implements hook_preprocess_block().
  63. *
  64. * Wrap the block title as a panel heading if required.
  65. */
  66. function dr821_preprocess_block(&$elements, $hook, $info) {
  67. if ($elements['elements']['#dr821_panelize_title']) {
  68. _dr821_panelize_block_title($elements);
  69. }
  70. }