blockify.module 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. /**
  3. * @file
  4. * Exposes a number of core Drupal elements as blocks.
  5. */
  6. /**
  7. * Implements hook_help().
  8. */
  9. function blockify_help($path, $arg) {
  10. if ($path == 'admin/help#blockify') {
  11. return '<p>' . t('This module exposes a number of core Drupal elements as blocks.') . '</p>';
  12. }
  13. }
  14. /**
  15. * Implements hook_menu()
  16. */
  17. function blockify_menu() {
  18. $items = array();
  19. $items['admin/config/user-interface/blockify'] = array(
  20. 'title' => 'Blockify',
  21. 'description' => 'Settings for the Blockify module.',
  22. 'page callback' => 'drupal_get_form',
  23. 'page arguments' => array('blockify_admin_settings'),
  24. 'access arguments' => array('administer blockify'),
  25. 'type' => MENU_NORMAL_ITEM,
  26. 'file' => 'blockify.admin.inc',
  27. );
  28. return $items;
  29. }
  30. /**
  31. * Implements hook_permission().
  32. */
  33. function blockify_permission() {
  34. return array(
  35. 'administer blockify' => array(
  36. 'title' => t('Administer Blockify'),
  37. 'description' => t('Manage settings for Blockify module'),
  38. ),
  39. );
  40. }
  41. /**
  42. * Implements hook_block_info().
  43. */
  44. function blockify_block_info() {
  45. $block_list = _blockify_get_blocks();
  46. foreach ($block_list as $delta => $name) {
  47. if (_blockify_is_enabled($delta)) {
  48. $blocks[$delta] = array(
  49. 'info' => $name,
  50. 'cache' => DRUPAL_CACHE_GLOBAL,
  51. );
  52. }
  53. }
  54. $cache_per_page_blocks = array(
  55. 'blockify-page-title',
  56. 'blockify-breadcrumb',
  57. 'blockify-feed-icons',
  58. );
  59. foreach ($cache_per_page_blocks as $delta) {
  60. if (!empty($blocks[$delta])) {
  61. $blocks[$delta]['cache'] = DRUPAL_CACHE_PER_PAGE;
  62. }
  63. }
  64. $no_cache_blocks = array(
  65. 'blockify-tabs',
  66. 'blockify-actions',
  67. 'blockify-messages',
  68. );
  69. foreach ($no_cache_blocks as $delta) {
  70. if (!empty($blocks[$delta])) {
  71. $blocks[$delta]['cache'] = DRUPAL_NO_CACHE;
  72. }
  73. }
  74. if (empty($blocks)) {
  75. $blocks = array();
  76. }
  77. return $blocks;
  78. }
  79. /**
  80. * Implements hook_block_view().
  81. */
  82. function blockify_block_view($delta = '') {
  83. $blocks = _blockify_get_blocks();
  84. foreach ($blocks as $block_delta => $block_name) {
  85. if ($delta == $block_delta && _blockify_is_enabled($block_delta)) {
  86. return array(
  87. 'subject' => '',
  88. 'content' => blockify_get_content($delta),
  89. );
  90. }
  91. }
  92. }
  93. /**
  94. * Provides individual block content.
  95. */
  96. function blockify_get_content($delta) {
  97. $variables = array();
  98. switch ($delta) {
  99. case 'blockify-logo':
  100. $variables['logo_path'] = theme_get_setting('logo');
  101. return theme('blockify_logo', $variables);
  102. case 'blockify-site-name':
  103. $variables['site_name'] = filter_xss_admin(variable_get('site_name', 'Drupal'));
  104. return theme('blockify_site_name', $variables);
  105. case 'blockify-site-slogan':
  106. $variables['site_slogan'] = filter_xss_admin(variable_get('site_slogan', ''));
  107. return theme('blockify_site_slogan', $variables);
  108. case 'blockify-page-title':
  109. $variables['page_title'] = drupal_get_title();
  110. return theme('blockify_page_title', $variables);
  111. case 'blockify-breadcrumb':
  112. $variables['breadcrumb'] = drupal_get_breadcrumb();
  113. return theme('blockify_breadcrumb', $variables);
  114. case 'blockify-messages':
  115. return theme('status_messages');
  116. case 'blockify-tabs':
  117. $variables['primary'] = menu_primary_local_tasks();
  118. $variables['secondary'] = menu_secondary_local_tasks();
  119. return theme('blockify_menu_local_tasks', $variables);
  120. case 'blockify-actions':
  121. $variables['menu_local_actions'] = menu_local_actions();
  122. return theme('blockify_local_actions', $variables);
  123. case 'blockify-feed-icons':
  124. $variables['feed_icons'] = drupal_get_feeds();
  125. return theme('blockify_feed_icons', $variables);
  126. }
  127. }
  128. /**
  129. * Returns a list of blockify blocks.
  130. */
  131. function _blockify_get_blocks($enabled_blocks_only = TRUE) {
  132. return array(
  133. 'blockify-logo' => t('Logo'),
  134. 'blockify-site-name' => t('Site name'),
  135. 'blockify-site-slogan' => t('Site slogan'),
  136. 'blockify-page-title' => t('Page title'),
  137. 'blockify-breadcrumb' => t('Breadcrumb'),
  138. 'blockify-tabs' => t('Tabs'),
  139. 'blockify-messages' => t('Messages'),
  140. 'blockify-actions' => t('Actions'),
  141. 'blockify-feed-icons' => t('Feed icons'),
  142. );
  143. }
  144. /**
  145. * Verify if a given blockify block is enabled in the blockify admin settings.
  146. */
  147. function _blockify_is_enabled($delta) {
  148. $blocks = variable_get('blockify_blocks', array());
  149. if (!empty($blocks[$delta])) {
  150. return TRUE;
  151. }
  152. return FALSE;
  153. }
  154. /**
  155. * Implements hook_theme().
  156. */
  157. function blockify_theme() {
  158. return array(
  159. 'blockify_logo' => array(
  160. 'variables' => array('logo_path' => NULL),
  161. 'file' => 'blockify.theme.inc',
  162. ),
  163. 'blockify_site_name' => array(
  164. 'variables' => array('site_name' => NULL),
  165. 'file' => 'blockify.theme.inc',
  166. ),
  167. 'blockify_site_slogan' => array(
  168. 'variables' => array('site_slogan' => NULL),
  169. 'file' => 'blockify.theme.inc',
  170. ),
  171. 'blockify_page_title' => array(
  172. 'variables' => array('page_title' => NULL),
  173. 'file' => 'blockify.theme.inc',
  174. ),
  175. 'blockify_breadcrumb' => array(
  176. 'variables' => array('breadcrumb' => NULL),
  177. 'file' => 'blockify.theme.inc',
  178. ),
  179. 'blockify_local_actions' => array(
  180. 'variables' => array('menu_local_actions' => NULL),
  181. 'file' => 'blockify.theme.inc',
  182. ),
  183. 'blockify_feed_icons' => array(
  184. 'variables' => array('feed_icons' => NULL),
  185. 'file' => 'blockify.theme.inc',
  186. ),
  187. 'blockify_menu_local_tasks' => array(
  188. 'variables' => array('primary' => NULL, 'secondary' => NULL),
  189. 'file' => 'blockify.theme.inc',
  190. ),
  191. );
  192. }
  193. /**
  194. * Implements hook_menu_contextual_links_alter().
  195. */
  196. function blockify_menu_contextual_links_alter(&$links, $router_item, $root_path) {
  197. $block_id = array_pop($router_item['map']);
  198. $site_information_pages = array(
  199. 'blockify-site-slogan',
  200. 'blockify-site-name',
  201. );
  202. if (in_array($block_id, $site_information_pages)) {
  203. $links['site-information'] = array(
  204. 'title' => t('Site information'),
  205. 'href' => 'admin/config/system/site-information',
  206. 'localized_options' => array(),
  207. );
  208. }
  209. if ($block_id == 'blockify-logo') {
  210. $links['logo-settings'] = array(
  211. 'title' => t('Logo settings'),
  212. 'href' => 'admin/appearance/settings/' . $GLOBALS['theme'],
  213. 'localized_options' => array('fragment' => 'edit-logo'),
  214. );
  215. }
  216. }