block.install 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. <?php
  2. /**
  3. * @file
  4. * Install, update and uninstall functions for the block module.
  5. */
  6. /**
  7. * Implements hook_schema().
  8. */
  9. function block_schema() {
  10. $schema['block'] = array(
  11. 'description' => 'Stores block settings, such as region and visibility settings.',
  12. 'fields' => array(
  13. 'bid' => array(
  14. 'type' => 'serial',
  15. 'not null' => TRUE,
  16. 'description' => 'Primary Key: Unique block ID.',
  17. ),
  18. 'module' => array(
  19. 'type' => 'varchar',
  20. 'length' => 64,
  21. 'not null' => TRUE,
  22. 'default' => '',
  23. 'description' => "The module from which the block originates; for example, 'user' for the Who's Online block, and 'block' for any custom blocks.",
  24. ),
  25. 'delta' => array(
  26. 'type' => 'varchar',
  27. 'length' => 32,
  28. 'not null' => TRUE,
  29. 'default' => '0',
  30. 'description' => 'Unique ID for block within a module.',
  31. ),
  32. 'theme' => array(
  33. 'type' => 'varchar',
  34. 'length' => 64,
  35. 'not null' => TRUE,
  36. 'default' => '',
  37. 'description' => 'The theme under which the block settings apply.',
  38. ),
  39. 'status' => array(
  40. 'type' => 'int',
  41. 'not null' => TRUE,
  42. 'default' => 0,
  43. 'size' => 'tiny',
  44. 'description' => 'Block enabled status. (1 = enabled, 0 = disabled)',
  45. ),
  46. 'weight' => array(
  47. 'type' => 'int',
  48. 'not null' => TRUE,
  49. 'default' => 0,
  50. 'description' => 'Block weight within region.',
  51. ),
  52. 'region' => array(
  53. 'type' => 'varchar',
  54. 'length' => 64,
  55. 'not null' => TRUE,
  56. 'default' => '',
  57. 'description' => 'Theme region within which the block is set.',
  58. ),
  59. 'custom' => array(
  60. 'type' => 'int',
  61. 'not null' => TRUE,
  62. 'default' => 0,
  63. 'size' => 'tiny',
  64. 'description' => 'Flag to indicate how users may control visibility of the block. (0 = Users cannot control, 1 = On by default, but can be hidden, 2 = Hidden by default, but can be shown)',
  65. ),
  66. 'visibility' => array(
  67. 'type' => 'int',
  68. 'not null' => TRUE,
  69. 'default' => 0,
  70. 'size' => 'tiny',
  71. 'description' => 'Flag to indicate how to show blocks on pages. (0 = Show on all pages except listed pages, 1 = Show only on listed pages, 2 = Use custom PHP code to determine visibility)',
  72. ),
  73. 'pages' => array(
  74. 'type' => 'text',
  75. 'not null' => TRUE,
  76. 'description' => 'Contents of the "Pages" block; contains either a list of paths on which to include/exclude the block or PHP code, depending on "visibility" setting.',
  77. ),
  78. 'title' => array(
  79. 'type' => 'varchar',
  80. 'length' => 64,
  81. 'not null' => TRUE,
  82. 'default' => '',
  83. 'description' => 'Custom title for the block. (Empty string will use block default title, <none> will remove the title, text will cause block to use specified title.)',
  84. 'translatable' => TRUE,
  85. ),
  86. 'cache' => array(
  87. 'type' => 'int',
  88. 'not null' => TRUE,
  89. 'default' => 1,
  90. 'size' => 'tiny',
  91. 'description' => 'Binary flag to indicate block cache mode. (-2: Custom cache, -1: Do not cache, 1: Cache per role, 2: Cache per user, 4: Cache per page, 8: Block cache global) See DRUPAL_CACHE_* constants in ../includes/common.inc for more detailed information.',
  92. ),
  93. ),
  94. 'primary key' => array('bid'),
  95. 'unique keys' => array(
  96. 'tmd' => array('theme', 'module', 'delta'),
  97. ),
  98. 'indexes' => array(
  99. 'list' => array('theme', 'status', 'region', 'weight', 'module'),
  100. ),
  101. );
  102. $schema['block_role'] = array(
  103. 'description' => 'Sets up access permissions for blocks based on user roles',
  104. 'fields' => array(
  105. 'module' => array(
  106. 'type' => 'varchar',
  107. 'length' => 64,
  108. 'not null' => TRUE,
  109. 'description' => "The block's origin module, from {block}.module.",
  110. ),
  111. 'delta' => array(
  112. 'type' => 'varchar',
  113. 'length' => 32,
  114. 'not null' => TRUE,
  115. 'description' => "The block's unique delta within module, from {block}.delta.",
  116. ),
  117. 'rid' => array(
  118. 'type' => 'int',
  119. 'unsigned' => TRUE,
  120. 'not null' => TRUE,
  121. 'description' => "The user's role ID from {users_roles}.rid.",
  122. ),
  123. ),
  124. 'primary key' => array('module', 'delta', 'rid'),
  125. 'indexes' => array(
  126. 'rid' => array('rid'),
  127. ),
  128. );
  129. $schema['block_custom'] = array(
  130. 'description' => 'Stores contents of custom-made blocks.',
  131. 'fields' => array(
  132. 'bid' => array(
  133. 'type' => 'serial',
  134. 'unsigned' => TRUE,
  135. 'not null' => TRUE,
  136. 'description' => "The block's {block}.bid.",
  137. ),
  138. 'body' => array(
  139. 'type' => 'text',
  140. 'not null' => FALSE,
  141. 'size' => 'big',
  142. 'description' => 'Block contents.',
  143. 'translatable' => TRUE,
  144. ),
  145. 'info' => array(
  146. 'type' => 'varchar',
  147. 'length' => 128,
  148. 'not null' => TRUE,
  149. 'default' => '',
  150. 'description' => 'Block description.',
  151. ),
  152. 'format' => array(
  153. 'type' => 'varchar',
  154. 'length' => 255,
  155. 'not null' => FALSE,
  156. 'description' => 'The {filter_format}.format of the block body.',
  157. ),
  158. ),
  159. 'unique keys' => array(
  160. 'info' => array('info'),
  161. ),
  162. 'primary key' => array('bid'),
  163. );
  164. $schema['cache_block'] = drupal_get_schema_unprocessed('system', 'cache');
  165. $schema['cache_block']['description'] = 'Cache table for the Block module to store already built blocks, identified by module, delta, and various contexts which may change the block, such as theme, locale, and caching mode defined for the block.';
  166. return $schema;
  167. }
  168. /**
  169. * Implements hook_install().
  170. */
  171. function block_install() {
  172. // Block should go first so that other modules can alter its output
  173. // during hook_page_alter(). Almost everything on the page is a block,
  174. // so before block module runs, there will not be much to alter.
  175. db_update('system')
  176. ->fields(array('weight' => -5))
  177. ->condition('name', 'block')
  178. ->execute();
  179. }
  180. /**
  181. * Implements hook_update_dependencies().
  182. */
  183. function block_update_dependencies() {
  184. // block_update_7005() needs to query the {filter_format} table to get a list
  185. // of existing text formats, so it must run after filter_update_7000(), which
  186. // creates that table.
  187. $dependencies['block'][7005] = array(
  188. 'filter' => 7000,
  189. );
  190. return $dependencies;
  191. }
  192. /**
  193. * @addtogroup updates-6.x-to-7.x
  194. * @{
  195. */
  196. /**
  197. * Set system.weight to a low value for block module.
  198. *
  199. * Block should go first so that other modules can alter its output
  200. * during hook_page_alter(). Almost everything on the page is a block,
  201. * so before block module runs, there will not be much to alter.
  202. */
  203. function block_update_7000() {
  204. db_update('system')
  205. ->fields(array('weight' => '-5'))
  206. ->condition('name', 'block')
  207. ->execute();
  208. }
  209. /**
  210. * Rename {blocks} table to {block}, {blocks_roles} to {block_role} and
  211. * {boxes} to {block_custom}.
  212. */
  213. function block_update_7002() {
  214. db_drop_index('blocks', 'list');
  215. db_rename_table('blocks', 'block');
  216. db_rename_table('blocks_roles', 'block_role');
  217. db_rename_table('boxes', 'block_custom');
  218. }
  219. /**
  220. * Change the weight column to normal int.
  221. */
  222. function block_update_7003() {
  223. db_change_field('block', 'weight', 'weight', array(
  224. 'type' => 'int',
  225. 'not null' => TRUE,
  226. 'default' => 0,
  227. 'description' => 'Block weight within region.',
  228. ), array(
  229. 'indexes' => array(
  230. 'list' => array('theme', 'status', 'region', 'weight', 'module'),
  231. ),
  232. ));
  233. }
  234. /**
  235. * Add new blocks to new regions, migrate custom variables to blocks.
  236. */
  237. function block_update_7004() {
  238. // Collect a list of themes with blocks.
  239. $themes_with_blocks = array();
  240. $result = db_query("SELECT s.name FROM {system} s INNER JOIN {block} b ON s.name = b.theme WHERE s.type = 'theme' GROUP by s.name");
  241. $insert = db_insert('block')->fields(array('module', 'delta', 'theme', 'status', 'weight', 'region', 'pages', 'cache'));
  242. foreach ($result as $theme) {
  243. $themes_with_blocks[] = $theme->name;
  244. // Add new system generated help block.
  245. $insert->values(array(
  246. 'module' => 'system',
  247. 'delta' => 'help',
  248. 'theme' => $theme->name,
  249. 'status' => 1,
  250. 'weight' => 0,
  251. 'region' => 'help',
  252. 'pages' => '',
  253. 'cache' => DRUPAL_CACHE_PER_ROLE,
  254. ));
  255. // Add new system generated main page content block.
  256. $insert->values(array(
  257. 'module' => 'system',
  258. 'delta' => 'main',
  259. 'theme' => $theme->name,
  260. 'status' => 1,
  261. 'weight' => 0,
  262. 'region' => 'content',
  263. 'pages' => '',
  264. 'cache' => DRUPAL_NO_CACHE,
  265. ));
  266. }
  267. $insert->execute();
  268. // Migrate blocks from left/right regions to first/second regions.
  269. db_update('block')
  270. ->fields(array('region' => 'sidebar_first'))
  271. ->condition('region', 'left')
  272. ->execute();
  273. db_update('block')
  274. ->fields(array('region' => 'sidebar_second'))
  275. ->condition('region', 'right')
  276. ->execute();
  277. // Migrate contact form information.
  278. $default_format = variable_get('filter_default_format', 1);
  279. if ($contact_help = variable_get('contact_form_information', '')) {
  280. $bid = db_insert('block_custom')
  281. ->fields(array(
  282. 'body' => $contact_help,
  283. 'info' => 'Contact page help',
  284. 'format' => $default_format,
  285. ))
  286. ->execute();
  287. $insert = db_insert('block')->fields(array('module', 'delta', 'theme', 'status', 'weight', 'region', 'visibility', 'pages', 'cache'));
  288. foreach ($themes_with_blocks as $theme) {
  289. // Add contact help block for themes, which had blocks.
  290. $insert->values(array(
  291. 'module' => 'block',
  292. 'delta' => $bid,
  293. 'theme' => $theme,
  294. 'status' => 1,
  295. 'weight' => 5,
  296. 'region' => 'help',
  297. 'visibility' => BLOCK_VISIBILITY_LISTED,
  298. 'pages' => 'contact',
  299. 'cache' => DRUPAL_NO_CACHE,
  300. ));
  301. }
  302. drupal_set_message('The contact form information setting was migrated to <a href="' . url('admin/structure/block/manage/block/' . $bid . '/configure') . '">a custom block</a> and set up to only show on the site-wide contact page. The block was set to use the default text format, which might differ from the HTML based format used before. Check the block and ensure that the output is right.');
  303. }
  304. $insert->execute();
  305. // Migrate user help setting.
  306. if ($user_help = variable_get('user_registration_help', '')) {
  307. $bid = db_insert('block_custom')->fields(array('body' => $user_help, 'info' => 'User registration guidelines', 'format' => $default_format))->execute();
  308. $insert = db_insert('block')->fields(array('module', 'delta', 'theme', 'status', 'weight', 'region', 'visibility', 'pages', 'cache'));
  309. foreach ($themes_with_blocks as $theme) {
  310. // Add user registration help block for themes, which had blocks.
  311. $insert->values(array(
  312. 'module' => 'block',
  313. 'delta' => $bid,
  314. 'theme' => $theme,
  315. 'status' => 1,
  316. 'weight' => 5,
  317. 'region' => 'help',
  318. 'visibility' => BLOCK_VISIBILITY_LISTED,
  319. 'pages' => 'user/register',
  320. 'cache' => DRUPAL_NO_CACHE,
  321. ));
  322. }
  323. drupal_set_message('The user registration guidelines were migrated to <a href="' . url('admin/structure/block/manage/block/' . $bid . '/configure') . '">a custom block</a> and set up to only show on the user registration page. The block was set to use the default text format, which might differ from the HTML based format used before. Check the block and ensure that the output is right.');
  324. $insert->execute();
  325. }
  326. // Migrate site mission setting.
  327. if ($mission = variable_get('site_mission')) {
  328. $bid = db_insert('block_custom')->fields(array('body' => $mission, 'info' => 'Site mission', 'format' => $default_format))->execute();
  329. $insert = db_insert('block')->fields(array('module', 'delta', 'theme', 'status', 'weight', 'region', 'visibility', 'pages', 'cache'));
  330. foreach ($themes_with_blocks as $theme) {
  331. // Add mission block for themes, which had blocks.
  332. $insert->values(array(
  333. 'module' => 'block',
  334. 'delta' => $bid,
  335. 'theme' => $theme,
  336. 'status' => 1,
  337. 'weight' => 0,
  338. 'region' => 'highlighted',
  339. 'visibility' => BLOCK_VISIBILITY_LISTED,
  340. 'pages' => '<front>',
  341. 'cache' => DRUPAL_NO_CACHE,
  342. ));
  343. }
  344. drupal_set_message('The site mission was migrated to <a href="' . url('admin/structure/block/manage/block/' . $bid . '/configure') . '">a custom block</a> and set up to only show on the front page in the highlighted content region. The block was set to use the default text format, which might differ from the HTML based format used before. Check the block and ensure that the output is right. If your theme does not have a highlighted content region, you might need to <a href="' . url('admin/structure/block') . '">relocate the block</a>.');
  345. $insert->execute();
  346. // Migrate mission to RSS site description.
  347. variable_set('feed_description', $mission);
  348. }
  349. // Migrate site footer message to a custom block.
  350. if ($footer_message = variable_get('site_footer', '')) {
  351. $bid = db_insert('block_custom')->fields(array('body' => $footer_message, 'info' => 'Footer message', 'format' => $default_format))->execute();
  352. $insert = db_insert('block')->fields(array('module', 'delta', 'theme', 'status', 'weight', 'region', 'pages', 'cache'));
  353. foreach ($themes_with_blocks as $theme) {
  354. // Add site footer block for themes, which had blocks.
  355. // Set low weight, so the block comes early (it used to be
  356. // before the other blocks).
  357. $insert->values(array(
  358. 'module' => 'block',
  359. 'delta' => $bid,
  360. 'theme' => $theme,
  361. 'status' => 1,
  362. 'weight' => -10,
  363. 'region' => 'footer',
  364. 'pages' => '',
  365. 'cache' => DRUPAL_NO_CACHE,
  366. ));
  367. }
  368. drupal_set_message('The footer message was migrated to <a href="' . url('admin/structure/block/manage/block/' . $bid . '/configure') . '">a custom block</a> and set up to appear in the footer. The block was set to use the default text format, which might differ from the HTML based format used before. Check the block and ensure that the output is right. If your theme does not have a footer region, you might need to <a href="' . url('admin/structure/block') . '">relocate the block</a>.');
  369. $insert->execute();
  370. }
  371. // Remove the variables (even if they were saved empty on the admin interface),
  372. // to avoid keeping clutter in the variables table.
  373. variable_del('contact_form_information');
  374. variable_del('user_registration_help');
  375. variable_del('site_mission');
  376. variable_del('site_footer');
  377. // Rebuild theme data, so the new 'help' region is identified.
  378. system_rebuild_theme_data();
  379. }
  380. /**
  381. * Update the {block_custom}.format column.
  382. */
  383. function block_update_7005() {
  384. // For an explanation of these updates, see the code comments in
  385. // user_update_7010().
  386. db_change_field('block_custom', 'format', 'format', array(
  387. 'type' => 'int',
  388. 'unsigned' => TRUE,
  389. 'not null' => FALSE,
  390. 'description' => 'The {filter_format}.format of the block body.',
  391. ));
  392. db_update('block_custom')
  393. ->fields(array('format' => NULL))
  394. ->condition('body', '')
  395. ->condition('format', 0)
  396. ->execute();
  397. $existing_formats = db_query("SELECT format FROM {filter_format}")->fetchCol();
  398. $default_format = variable_get('filter_default_format', 1);
  399. db_update('block_custom')
  400. ->fields(array('format' => $default_format))
  401. ->isNotNull('format')
  402. ->condition('format', $existing_formats, 'NOT IN')
  403. ->execute();
  404. }
  405. /**
  406. * Recreates cache_block table.
  407. *
  408. * Converts fields that hold serialized variables from text to blob.
  409. * Removes 'headers' column.
  410. */
  411. function block_update_7006() {
  412. $schema = system_schema_cache_7054();
  413. db_drop_table('cache_block');
  414. db_create_table('cache_block', $schema);
  415. }
  416. /**
  417. * Change {block_custom}.format into varchar.
  418. */
  419. function block_update_7007() {
  420. db_change_field('block_custom', 'format', 'format', array(
  421. 'type' => 'varchar',
  422. 'length' => 255,
  423. 'not null' => FALSE,
  424. 'description' => 'The {filter_format}.format of the block body.',
  425. ));
  426. }
  427. /**
  428. * @} End of "addtogroup updates-6.x-to-7.x".
  429. */
  430. /**
  431. * @addtogroup updates-7.x-extra
  432. * @{
  433. */
  434. /**
  435. * Update database to match Drupal 7 schema.
  436. */
  437. function block_update_7008() {
  438. db_drop_field('block', 'throttle');
  439. }
  440. /**
  441. * @} End of "addtogroup updates-7.x-extra".
  442. */