block_everything.module 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <?php
  2. // $Id:$
  3. /**
  4. * @file
  5. * Module that creates blocks for common page template elements so they can be
  6. * moved around in the admin interface.
  7. *
  8. * @todo: This needs an alteration to the default site information form for
  9. * configuration outside of the theme's info file.
  10. */
  11. /**
  12. * Implements hook_theme()
  13. */
  14. function block_everything_theme() {
  15. return array(
  16. 'block__block_everything' => array(
  17. 'render element' => 'elements',
  18. ),
  19. 'block_everything_site_name' => array(
  20. 'render element' => 'element',
  21. ),
  22. 'block_everything_site_slogan' => array(
  23. 'render element' => 'element',
  24. ),
  25. 'block_everything_logo' => array(
  26. 'render element' => 'element',
  27. ),
  28. );
  29. }
  30. /**
  31. * Implements hook_block_info()
  32. */
  33. function block_everything_block_info() {
  34. $blocks = array();
  35. global $user;
  36. $theme = !empty($user->theme) ? $user->theme : variable_get('theme_default');
  37. $settings = theme_get_setting('block_everything', $theme);
  38. // Only display the blocks if the default theme supports them
  39. if (isset($settings['site_name'])) {
  40. $blocks['site_name'] = array(
  41. 'info' => 'Site name',
  42. 'cache' => DRUPAL_CACHE_GLOBAL,
  43. 'status' => 1,
  44. 'region' => $settings['site_name'],
  45. );
  46. }
  47. if (isset($settings['site_slogan'])) {
  48. $blocks['site_slogan'] = array(
  49. 'info' => 'Site slogan',
  50. 'cache' => DRUPAL_CACHE_GLOBAL,
  51. 'status' => 1,
  52. 'region' => $settings['site_slogan'],
  53. );
  54. }
  55. if (isset($settings['logo'])) {
  56. $blocks['logo'] = array(
  57. 'info' => 'Site logo',
  58. 'cache' => DRUPAL_CACHE_GLOBAL,
  59. 'status' => 1,
  60. 'region' => $settings['logo'],
  61. );
  62. }
  63. return $blocks;
  64. }
  65. /**
  66. * Implements hook_themes_enabled()
  67. *
  68. * The purpose of this is to properly set the default regions when a theme is
  69. * updated and enabled.
  70. */
  71. function block_everything_themes_enabled($theme_list) {
  72. foreach($theme_list as $theme) {
  73. $settings = theme_get_setting('block_everything', $theme);
  74. $theme_setting_name = array(
  75. 'site_name' => 'toggle_name',
  76. 'site_slogan' => 'toggle_slogan',
  77. 'logo' => 'default_logo_path',
  78. );
  79. // Get the block_everything blocks for this theme
  80. $result = db_query("SELECT * FROM {block} WHERE theme = :theme and module = 'block_everything'", array(':theme' => $theme), array('fetch' => PDO::FETCH_ASSOC));
  81. foreach ($result as $block) {
  82. // If the block is disabled, check to make sure the theme supports it and
  83. // enable it in the default region. If the theme does not support
  84. // block_everything, disable the blocks.
  85. if (!$block['status'] || !$settings) {
  86. $region = (theme_get_setting($theme_setting_name[$block['delta']], $theme) && isset($settings[$block['delta']])) ? $settings[$block['delta']] : BLOCK_REGION_NONE;
  87. $status = isset($settings[$block['delta']]) ? 1 : 0;
  88. db_update('block')
  89. ->fields(array(
  90. 'region' => $region,
  91. 'status' => $status,
  92. ))
  93. ->condition('theme', $theme)
  94. ->condition('delta', $block['delta'])
  95. ->condition('module', 'block_everything')
  96. ->execute();
  97. // Invoke a hook to inform other modules that the block was just
  98. // modified.
  99. module_invoke_all('block_everything_modified_block', $block);
  100. }
  101. }
  102. }
  103. }
  104. /**
  105. * Implements hook_block_configure()
  106. */
  107. function block_everything_block_configure($delta = '') {
  108. $form = array();
  109. switch ($delta) {
  110. case 'site_name':
  111. $form['block_everything'] = array(
  112. '#type' => 'textfield',
  113. '#title' => t('Site name'),
  114. '#default_value' => variable_get('site_name', 'Drupal'),
  115. );
  116. break;
  117. case 'site_slogan':
  118. $form['block_everything'] = array(
  119. '#type' => 'textfield',
  120. '#title' => t('Site slogan'),
  121. '#default_value' => variable_get('site_slogan', ''),
  122. );
  123. break;
  124. }
  125. return $form;
  126. }
  127. /**
  128. * Implements hook_block_save().
  129. */
  130. function block_everything_block_save($delta = '', $edit = array()) {
  131. global $user;
  132. $theme = !empty($user->theme) ? $user->theme : variable_get('theme_default');
  133. $settings = variable_get('theme_settings', array());
  134. switch ($delta) {
  135. case 'site_name':
  136. variable_set('site_name', $edit['block_everything']);
  137. $settings['toggle_name'] = $edit['regions'][$theme] == BLOCK_REGION_NONE ? 0 : 1;
  138. break;
  139. case 'site_slogan':
  140. variable_set('site_slogan', $edit['block_everything']);
  141. $settings['toggle_slogan'] = $edit['regions'][$theme] == BLOCK_REGION_NONE ? 0 : 1;
  142. break;
  143. }
  144. variable_set('theme_settings', $settings);
  145. }
  146. /**
  147. * Implements hook_block_view()
  148. */
  149. function block_everything_block_view($delta = '') {
  150. $block = array();
  151. global $theme;
  152. switch ($delta) {
  153. case 'site_name':
  154. $block['content'] = filter_xss_admin(variable_get('site_name', 'Drupal'));
  155. break;
  156. case 'site_slogan':
  157. $block['content'] = filter_xss_admin(variable_get('site_slogan', ''));
  158. break;
  159. case 'logo':
  160. $path = theme_get_setting('default_logo_path', $theme) ? drupal_get_path('theme', $theme) . '/' . theme_get_setting('default_logo_path', $theme) : '';
  161. $block['content'] = $path;
  162. break;
  163. }
  164. return $block;
  165. }
  166. /**
  167. * Alters the block configuration form to hide the title
  168. */
  169. function block_everything_form_block_admin_configure_alter(&$form, &$form_state, $form_id) {
  170. if ($form['module']['#value'] == 'block_everything') {
  171. $form['settings']['title']['#access'] = FALSE;
  172. }
  173. }
  174. /**
  175. * Alters the main block admin page to add a submit handler for block_everything
  176. */
  177. function block_everything_form_block_admin_display_form_alter(&$form, &$form_state, $form_id) {
  178. $form['#submit'][] = 'block_everything_admin_display_form_submit';
  179. }
  180. /**
  181. * Updates the theme settings depending on the region for block_everything blocks.
  182. */
  183. function block_everything_admin_display_form_submit($form, &$form_state) {
  184. foreach ($form_state['values']['blocks'] as $block) {
  185. if ($block['module'] == 'block_everything') {
  186. $delta = $block['delta'];
  187. $settings = variable_get('theme_settings', array());
  188. switch ($delta) {
  189. case 'site_name':
  190. $settings['toggle_name'] = $block['region'] == BLOCK_REGION_NONE ? 0 : 1;
  191. break;
  192. case 'site_slogan':
  193. $settings['toggle_slogan'] = $block['region'] == BLOCK_REGION_NONE ? 0 : 1;
  194. break;
  195. }
  196. variable_set('theme_settings', $settings);
  197. }
  198. }
  199. }
  200. /**
  201. * Implements hook_process_page()
  202. *
  203. * Unsets the items that would otherwise be printed to the page
  204. */
  205. function block_everything_process_page(&$vars) {
  206. global $theme;
  207. $settings = theme_get_setting('block_everything', $theme);
  208. if ($settings) {
  209. foreach($settings as $key => $region) {
  210. $vars[$key] = NULL;
  211. }
  212. }
  213. }
  214. /**
  215. * Outputs the theming for the blocks.
  216. */
  217. function theme_block__block_everything($vars) {
  218. global $theme;
  219. if (!theme_get_setting('block_everything', $theme) || $vars['content'] == '') {
  220. return '';
  221. }
  222. // Add necessary CSS.
  223. drupal_add_css(drupal_get_path('module', 'block_everything') . '/block_everything.css');
  224. // Create a new class array
  225. $classes = array('page-element', strtr($vars['block']->delta, '_', '-') . '-wrapper');
  226. // Iterate through the old class array and add classes that do not include
  227. // block in their name. e.g. block and block-block-everything
  228. foreach($vars['classes_array'] as $key => $class) {
  229. if (strpos($class, 'block') === FALSE) {
  230. $classes[] = $vars['classes_array'][$key];
  231. }
  232. }
  233. $output = '<div class="' . implode(' ', $classes) . '">';
  234. $output .= render($vars['title_prefix']);
  235. $output .= render($vars['title_suffix']);
  236. $output .= theme('block_everything_' . $vars['block']->delta, $vars);
  237. $output .= '</div>';
  238. return $output;
  239. }
  240. /**
  241. * Returns markup for the site name.
  242. *
  243. * The site name is within either a <h1> or a <p> tag dependent on $is_front for
  244. * SEO purposes.
  245. */
  246. function theme_block_everything_site_name($vars) {
  247. $name = $vars['content'];
  248. $is_front = $vars['is_front'];
  249. $output = $is_front ? '<h1' : '<p';
  250. $output .= ' id="site-name"';
  251. $output .= ' class="site-name ' . ($is_front ? 'site-name-front' : '') . '">';
  252. $output .= l('<span>' . $name . '</span>', '', array('attributes' => array('title' => t('Home'), 'rel' => 'home'), 'html' => TRUE));
  253. $output .= $is_front ? '</h1>' : '</p>';
  254. return $output;
  255. }
  256. /**
  257. * Returns markup for the site's slogan
  258. */
  259. function theme_block_everything_site_slogan($vars) {
  260. $slogan = $vars['content'];
  261. $output = '<p id="site-slogan" class="site-slogan">' . $slogan . '</p>';
  262. return $output;
  263. }
  264. /**
  265. * Returns markup for the site's logo
  266. */
  267. function theme_block_everything_logo($vars) {
  268. $logo_path = $vars['content'];
  269. $home = t('Home');
  270. $image_vars = array(
  271. 'path' => $logo_path,
  272. 'alt' => $home,
  273. 'title' => $home,
  274. );
  275. $image = theme('image', $image_vars);
  276. $output = l($image, '', array('attributes' => array('rel' => 'home', 'id' => 'logo', 'class' => 'logo'), 'html' => TRUE));
  277. return $output;
  278. }