delta_blocks.module 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. <?php
  2. /**
  3. * @file
  4. * Exposes a number of core Drupal elements as blocks.
  5. */
  6. /**
  7. * Implements hook_help().
  8. */
  9. function delta_blocks_help($path, $arg) {
  10. if ($path == 'admin/help#delta_blocks') {
  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 delta_blocks_menu() {
  18. $items = array();
  19. $items['admin/config/user-interface/delta-blocks'] = array(
  20. 'title' => 'Delta blocks',
  21. 'description' => 'Settings for the Delta blocks module.',
  22. 'page callback' => 'drupal_get_form',
  23. 'page arguments' => array('delta_blocks_admin_settings'),
  24. 'access arguments' => array('administer delta blocks'),
  25. 'type' => MENU_NORMAL_ITEM,
  26. 'file' => 'includes/delta_blocks.admin.inc',
  27. );
  28. return $items;
  29. }
  30. /**
  31. * Implements hook_permission().
  32. */
  33. function delta_blocks_permission() {
  34. return array(
  35. 'administer delta blocks' => array(
  36. 'title' => t('Administer Delta blocks'),
  37. 'description' => t('Manage settings for Delta blocks module'),
  38. ),
  39. );
  40. }
  41. /**
  42. * Implements hook_block_info().
  43. */
  44. function delta_blocks_block_info() {
  45. $blocks = array();
  46. foreach (_delta_blocks_get_blocks() as $delta => $info) {
  47. if (_delta_blocks_is_enabled($delta)) {
  48. $blocks[$delta] = array(
  49. 'info' => $info['title'],
  50. 'cache' => $info['cache'],
  51. );
  52. }
  53. }
  54. return $blocks;
  55. }
  56. /**
  57. * Implements hook_block_view().
  58. */
  59. function delta_blocks_block_view($delta = '') {
  60. $blocks = _delta_blocks_get_blocks();
  61. if (isset($blocks[$delta]) && _delta_blocks_is_enabled($delta)) {
  62. return array(
  63. 'subject' => '',
  64. 'content' => delta_blocks_get_content($delta),
  65. );
  66. }
  67. }
  68. /**
  69. * Implements hook_block_save().
  70. */
  71. function delta_blocks_block_save($delta = '', $edit = array()) {
  72. switch ($delta) {
  73. case 'branding':
  74. variable_set('delta_blocks_branding_site_name_hidden', $edit['delta_blocks_branding_site_slogan_hidden']);
  75. variable_set('delta_blocks_branding_site_slogan_hidden', $edit['delta_blocks_branding_site_slogan_hidden']);
  76. variable_set('delta_blocks_branding_logo_linked', $edit['delta_blocks_branding_logo_linked']);
  77. variable_set('delta_blocks_branding_logo_render', $edit['delta_blocks_branding_logo_render']);
  78. variable_set('delta_blocks_branding_site_name_linked', $edit['delta_blocks_branding_site_name_linked']);
  79. break;
  80. case 'site-name':
  81. variable_set('delta_blocks_site_name_linked', $edit['delta_blocks_site_name_linked']);
  82. variable_set('delta_blocks_site_name_hidden', $edit['delta_blocks_site_name_hidden']);
  83. break;
  84. case 'site-slogan':
  85. variable_set('delta_blocks_site_slogan_hidden', $edit['delta_blocks_site_slogan_hidden']);
  86. break;
  87. case 'page-title':
  88. variable_set('delta_blocks_page_title_hidden', $edit['delta_blocks_page_title_hidden']);
  89. break;
  90. case 'logo':
  91. variable_set('delta_blocks_logo_linked', $edit['delta_blocks_logo_linked']);
  92. break;
  93. case 'breadcrumb':
  94. variable_set('delta_blocks_breadcrumb_title_hidden', $edit['delta_blocks_breadcrumb_title_hidden']);
  95. variable_set('delta_blocks_breadcrumb_current', $edit['delta_blocks_breadcrumb_current']);
  96. break;
  97. }
  98. }
  99. /**
  100. * Implements hook_block_configure().
  101. */
  102. function delta_blocks_block_configure($delta = '') {
  103. $form = array();
  104. switch ($delta) {
  105. case 'branding':
  106. $form['delta_blocks_settings']['delta_blocks_branding_site_name_linked'] = array(
  107. '#type' => 'checkbox',
  108. '#title' => t('Link the site name to your front page'),
  109. '#default_value' => variable_get('delta_blocks_branding_site_name_linked', TRUE),
  110. );
  111. $form['delta_blocks_settings']['delta_blocks_branding_site_name_hidden'] = array(
  112. '#type' => 'checkbox',
  113. '#title' => t('Hide the site name via CSS'),
  114. '#default_value' => variable_get('delta_blocks_branding_site_name_hidden', FALSE),
  115. );
  116. $form['delta_blocks_settings']['delta_blocks_branding_site_slogan_hidden'] = array(
  117. '#type' => 'checkbox',
  118. '#title' => t('Hide site slogan via CSS'),
  119. '#default_value' => variable_get('delta_blocks_branding_site_slogan_hidden', FALSE),
  120. );
  121. $form['delta_blocks_settings']['delta_blocks_branding_logo_render'] = array(
  122. '#type' => 'checkbox',
  123. '#title' => t('Show the logo'),
  124. '#default_value' => variable_get('delta_blocks_branding_logo_render', TRUE),
  125. );
  126. $form['delta_blocks_settings']['delta_blocks_branding_logo_linked'] = array(
  127. '#type' => 'checkbox',
  128. '#title' => t('Link the logo to your front page'),
  129. '#default_value' => variable_get('delta_blocks_branding_logo_linked', TRUE),
  130. '#states' => array(
  131. 'visible' => array(
  132. ':input[name="delta_blocks_branding_logo_render"]' => array('checked' => TRUE),
  133. ),
  134. ),
  135. );
  136. break;
  137. case 'site-name':
  138. $form['delta_blocks_settings']['delta_blocks_site_name_linked'] = array(
  139. '#type' => 'checkbox',
  140. '#title' => t('Link the site name to your front page'),
  141. '#default_value' => variable_get('delta_blocks_site_name_linked', TRUE),
  142. );
  143. $form['delta_blocks_settings']['delta_blocks_site_name_hidden'] = array(
  144. '#type' => 'checkbox',
  145. '#title' => t('Hide the site name via CSS'),
  146. '#default_value' => variable_get('delta_blocks_site_name_hidden', TRUE),
  147. );
  148. break;
  149. case 'site-slogan':
  150. $form['delta_blocks_settings']['delta_blocks_site_slogan_hidden'] = array(
  151. '#type' => 'checkbox',
  152. '#title' => t('Hide the site slogan via CSS'),
  153. '#default_value' => variable_get('delta_blocks_site_slogan_hidden', TRUE),
  154. );
  155. break;
  156. case 'page-title':
  157. $form['delta_blocks_settings']['delta_blocks_page_title_hidden'] = array(
  158. '#type' => 'checkbox',
  159. '#title' => t('Hide page title via CSS'),
  160. '#default_value' => variable_get('delta_blocks_page_title_hidden', FALSE),
  161. );
  162. break;
  163. case 'logo':
  164. $form['delta_blocks_settings']['delta_blocks_logo_linked'] = array(
  165. '#type' => 'checkbox',
  166. '#title' => t('Link the logo to your front page'),
  167. '#default_value' => variable_get('delta_blocks_logo_linked', TRUE),
  168. );
  169. break;
  170. case 'breadcrumb':
  171. $form['delta_blocks_settings']['delta_blocks_breadcrumb_title_hidden'] = array(
  172. '#type' => 'checkbox',
  173. '#title' => t('Hide the block title via CSS'),
  174. '#default_value' => variable_get('delta_blocks_breadcrumb_title_hidden', TRUE),
  175. );
  176. $form['delta_blocks_settings']['delta_blocks_breadcrumb_current'] = array(
  177. '#type' => 'checkbox',
  178. '#title' => t('Append the current page to the breadcrumb navigation'),
  179. '#default_value' => variable_get('delta_blocks_breadcrumb_current', TRUE),
  180. );
  181. break;
  182. }
  183. if (!empty($form)) {
  184. $form['delta_blocks_settings'] += array(
  185. '#type' => 'fieldset',
  186. '#title' => t('Block settings'),
  187. );
  188. }
  189. return $form;
  190. }
  191. /**
  192. * Implements hook_theme().
  193. */
  194. function delta_blocks_theme() {
  195. return array(
  196. 'delta_blocks_logo' => array(
  197. 'variables' => array('logo' => NULL, 'logo_link' => NULL, 'site_name' => NULL),
  198. 'file' => 'includes/delta_blocks.theme.inc',
  199. ),
  200. 'delta_blocks_site_name' => array(
  201. 'variables' => array('site_name' => NULL),
  202. 'file' => 'includes/delta_blocks.theme.inc',
  203. ),
  204. 'delta_blocks_site_slogan' => array(
  205. 'variables' => array('site_slogan' => NULL),
  206. 'file' => 'includes/delta_blocks.theme.inc',
  207. ),
  208. 'delta_blocks_branding' => array(
  209. 'variables' => array('site_name' => NULL, 'site_slogan' => NULL, 'logo' => NULL, 'logo_link' => NULL, 'site_name_hidden' => NULL, 'site_slogan_hidden' => NULL),
  210. 'file' => 'includes/delta_blocks.theme.inc',
  211. ),
  212. 'delta_blocks_page_title' => array(
  213. 'variables' => array('page_title' => NULL),
  214. 'file' => 'includes/delta_blocks.theme.inc',
  215. ),
  216. 'delta_blocks_breadcrumb' => array(
  217. 'variables' => array('breadcrumb' => NULL),
  218. 'file' => 'includes/delta_blocks.theme.inc',
  219. ),
  220. 'delta_blocks_action_links' => array(
  221. 'variables' => array('action_links' => NULL),
  222. 'file' => 'includes/delta_blocks.theme.inc',
  223. ),
  224. 'delta_blocks_feed_icons' => array(
  225. 'variables' => array('feed_icons' => NULL),
  226. 'file' => 'includes/delta_blocks.theme.inc',
  227. ),
  228. 'delta_blocks_tabs' => array(
  229. 'variables' => array('primary' => NULL, 'secondary' => NULL),
  230. 'file' => 'includes/delta_blocks.theme.inc',
  231. ),
  232. );
  233. }
  234. /**
  235. * Implements hook_menu_contextual_links_alter().
  236. */
  237. function delta_blocks_menu_contextual_links_alter(&$links, $router_item, $root_path) {
  238. $block = array_pop($router_item['map']);
  239. if (in_array($block, array('site-slogan', 'site-name', 'branding'))) {
  240. $links['site-information'] = array(
  241. 'title' => t('Site information'),
  242. 'href' => 'admin/config/system/site-information',
  243. 'localized_options' => array(),
  244. );
  245. }
  246. if (in_array($block, array('logo', 'branding'))) {
  247. $links['logo-settings'] = array(
  248. 'title' => t('Logo settings'),
  249. 'href' => module_exists('delta_ui') && !empty($GLOBALS['delta']) ? 'admin/appearance/delta/layouts/configure/' . $GLOBALS['delta']->machine_name : 'admin/appearance/settings/' . $GLOBALS['theme_key'],
  250. 'localized_options' => array('fragment' => 'edit-logo'),
  251. );
  252. }
  253. }
  254. /**
  255. * Implements hook_preprocess_block().
  256. */
  257. function delta_blocks_preprocess_block(&$vars) {
  258. if ($vars['block']->delta == 'breadcrumb') {
  259. if(variable_get('delta_blocks_breadcrumb_title_hidden', TRUE)) {
  260. $vars['title_attributes_array']['class'][] = 'element-invisible';
  261. }
  262. }
  263. }
  264. /**
  265. * Provides individual block content.
  266. */
  267. function delta_blocks_get_content($delta) {
  268. $variables = array();
  269. switch ($delta) {
  270. case 'logo':
  271. $variables['logo'] = delta_blocks_logo();
  272. $variables['logo_linked'] = variable_get('delta_blocks_logo_linked', TRUE);
  273. $variables['site_name'] = filter_xss_admin(variable_get('site_name', 'Drupal'));
  274. return theme('delta_blocks_logo', $variables);
  275. case 'site-name':
  276. $variables['site_name'] = filter_xss_admin(variable_get('site_name'));
  277. $variables['site_name_linked'] = variable_get('delta_blocks_site_name_linked', TRUE);
  278. $variables['site_name_hidden'] = variable_get('delta_blocks_site_name_hidden', FALSE);
  279. return theme('delta_blocks_site_name', $variables);
  280. case 'site-slogan':
  281. $variables['site_slogan'] = filter_xss_admin(variable_get('site_slogan'));
  282. $variables['site_slogan_hidden'] = variable_get('delta_blocks_site_slogan_hidden', FALSE);
  283. return theme('delta_blocks_site_slogan', $variables);
  284. case 'branding':
  285. $variables['site_name'] = filter_xss_admin(variable_get('site_name', 'Drupal'));
  286. $variables['site_name_linked'] = variable_get('delta_blocks_branding_site_name_linked', TRUE);
  287. $variables['site_name_hidden'] = variable_get('delta_blocks_branding_site_name_hidden', FALSE);
  288. $variables['site_slogan'] = filter_xss_admin(variable_get('site_slogan', ''));
  289. $variables['site_slogan_hidden'] = variable_get('delta_blocks_branding_site_slogan_hidden', FALSE);
  290. $variables['logo'] = variable_get('delta_blocks_branding_logo_render', TRUE) ? delta_blocks_logo() : NULL;
  291. $variables['logo_linked'] = variable_get('delta_blocks_branding_logo_linked', TRUE);
  292. return theme('delta_blocks_branding', $variables);
  293. case 'page-title':
  294. $variables['page_title'] = drupal_get_title();
  295. $variables['page_title_hidden'] = variable_get('delta_blocks_page_title_hidden', FALSE);
  296. return theme('delta_blocks_page_title', $variables);
  297. case 'breadcrumb':
  298. $variables['breadcrumb_title_hidden'] = variable_get('delta_blocks_breadcrumb_title_hidden', TRUE);
  299. $variables['breadcrumb_current'] = variable_get('delta_blocks_breadcrumb_current', TRUE);
  300. $variables['breadcrumb'] = drupal_get_breadcrumb();
  301. return theme('delta_blocks_breadcrumb', $variables);
  302. case 'messages':
  303. return theme('status_messages');
  304. case 'tabs':
  305. $variables['primary'] = menu_primary_local_tasks();
  306. $variables['secondary'] = menu_secondary_local_tasks();
  307. return theme('delta_blocks_tabs', $variables);
  308. case 'action-links':
  309. $variables['action_links'] = menu_local_actions();
  310. return theme('delta_blocks_action_links', $variables);
  311. case 'feed-icons':
  312. $variables['feed_icons'] = drupal_get_feeds();
  313. return theme('delta_blocks_feed_icons', $variables);
  314. }
  315. }
  316. /**
  317. * @todo
  318. */
  319. function delta_blocks_options() {
  320. $output = array();
  321. foreach (_delta_blocks_get_blocks() as $block => $info) {
  322. $output[$block] = $info['title'];
  323. }
  324. return $output;
  325. }
  326. /**
  327. * @todo
  328. */
  329. function delta_blocks_logo() {
  330. $theme = $GLOBALS['theme_key'];
  331. if (theme_get_setting('default_logo', $theme)) {
  332. $themes = list_themes();
  333. $theme_object = $themes[$theme];
  334. return file_create_url(dirname($theme_object->filename) . '/logo.png');
  335. }
  336. else if ($path = theme_get_setting('logo_path', $theme)) {
  337. return file_create_url($path);
  338. }
  339. }
  340. /**
  341. * Returns a list of delta_blocks blocks.
  342. */
  343. function _delta_blocks_get_blocks() {
  344. return array(
  345. 'logo' => array(
  346. 'title' => t('Logo'),
  347. 'cache' => DRUPAL_CACHE_GLOBAL,
  348. ),
  349. 'site-name' => array(
  350. 'title' => t('Site name'),
  351. 'cache' => DRUPAL_CACHE_GLOBAL,
  352. ),
  353. 'site-slogan' => array(
  354. 'title' => t('Site slogan'),
  355. 'cache' => DRUPAL_CACHE_GLOBAL,
  356. ),
  357. 'branding' => array(
  358. 'title' => t('Branding'),
  359. 'cache' => DRUPAL_CACHE_GLOBAL,
  360. ),
  361. 'page-title' => array(
  362. 'title' => t('Page title'),
  363. 'cache' => DRUPAL_CACHE_PER_PAGE,
  364. ),
  365. 'breadcrumb' => array(
  366. 'title' => t('Breadcrumb'),
  367. 'cache' => DRUPAL_CACHE_PER_PAGE,
  368. ),
  369. 'tabs' => array(
  370. 'title' => t('Tabs'),
  371. 'cache' => DRUPAL_CACHE_PER_PAGE,
  372. ),
  373. 'messages' => array(
  374. 'title' => t('Messages'),
  375. 'cache' => DRUPAL_NO_CACHE,
  376. ),
  377. 'action-links' => array(
  378. 'title' => t('Action links'),
  379. 'cache' => DRUPAL_CACHE_GLOBAL,
  380. ),
  381. 'feed-icons' => array(
  382. 'title' => t('Feed icons'),
  383. 'cache' => DRUPAL_CACHE_GLOBAL,
  384. ),
  385. );
  386. }
  387. /**
  388. * Verify if a given block is enabled in the admin settings.
  389. */
  390. function _delta_blocks_is_enabled($delta) {
  391. $blocks = variable_get('delta_blocks_toggle', array());
  392. if (!empty($blocks[$delta]) || !isset($blocks[$delta])) {
  393. return TRUE;
  394. }
  395. return FALSE;
  396. }