mongodb_block_ui.module

Tracking 7.x-1.x branch
  1. drupal
    1. 7 contributions/mongodb/mongodb_block_ui/mongodb_block_ui.module

Controls the visual building mongodb_block_uis a page is constructed with.

Functions & methods

NameDescription
mongodb_block_ui_block_infoImplements hook_block_info().
mongodb_block_ui_block_viewImplements hook_block_view().
mongodb_block_ui_custom_block_formDefine the custom block form.
mongodb_block_ui_form_system_performance_settings_alterImplements hook_form_FORM_ID_alter().
mongodb_block_ui_helpImplements hook_help().
mongodb_block_ui_loadLoads a block object.
mongodb_block_ui_menuImplements hook_menu().
mongodb_block_ui_menu_deleteImplements hook_menu_delete().
mongodb_block_ui_mongodb_block_info_alterImplements hook_mongodb_block_info_alter().
mongodb_block_ui_permissionImplements hook_permission().
mongodb_block_ui_themeImplements hook_theme().
_mongodb_block_ui_themes_accessMenu item access callback - only admin or enabled themes can be accessed.

File

View source
  1. <?php
  2. /**
  3. * @file
  4. * Controls the visual building mongodb_block_uis a page is constructed with.
  5. */
  6. /**
  7. * Implements hook_help().
  8. */
  9. function mongodb_block_ui_help($path, $arg) {
  10. switch ($path) {
  11. case 'admin/help#mongodb_block':
  12. $output = '';
  13. $output .= '<h3>' . t('About') . '</h3>';
  14. $output .= '<p>' . t('The Block module allows you to create boxes of content, which are rendered into an area, or region, of one or more pages of a website. The core Seven administration theme, for example, implements the regions "Content", "Help", "Dashboard main", and "Dashboard sidebar", and a mongodb_block_ui may appear in any one of these regions. The <a href="@blocks">Blocks administration page</a> provides a drag-and-drop interface for assigning a mongodb_block_ui to a region, and for controlling the order of mongodb_block_uis within regions. For more information, see the online handbook entry for <a href="@mongodb_block_ui">Block module</a>.', array('@block' => 'http://drupal.org/handbook/modules/mongodb_block/', '@blocks' => url('admin/structure/mongodb_block'))) . '</p>';
  15. $output .= '<h3>' . t('Uses') . '</h3>';
  16. $output .= '<dl>';
  17. $output .= '<dt>' . t('Positioning content') . '</dt>';
  18. $output .= '<dd>' . t('When working with mongodb_block_uis, remember that all themes do <em>not</em> implement the same regions, or display regions in the same way. Blocks are positioned on a per-theme basis. Users with the <em>Administer mongodb blocks</em> permission can disable mongodb_block_uis. Disabled mongodb_block_uis are listed on the <a href="@mongodb_block_uis">Blocks administration page</a>, but are not displayed in any region.', array('@mongodb_block_ui' => 'http://drupal.org/handbook/modules/mongodb_block_ui/', '@mongodb_block_uis' => url('admin/structure/mongodb_block_ui'))) . '</dd>';
  19. $output .= '<dt>' . t('Controlling visibility') . '</dt>';
  20. $output .= '<dd>' . t('Blocks can be configured to be visible only on certain pages, only to users of certain roles, or only on pages displaying certain <a href="@content-type">content types</a>. Administrators can also allow specific mongodb_block_uis to be enabled or disabled by users when they edit their <a href="@user">My account</a> page. Some dynamic mongodb_block_uis, such as those generated by modules, will be displayed only on certain pages.', array('@content-type' => url('admin/structure/types'), '@user' => url('user'))) . '</dd>';
  21. $output .= '<dt>' . t('Creating custom mongodb_block_uis') . '</dt>';
  22. $output .= '<dd>' . t('Users with the <em>Administer mongodb blocks</em> permission can <a href="@mongodb_block_ui-add">add custom mongodb_block_uis</a>, which are then listed on the <a href="@mongodb_block_uis">Blocks administration page</a>. Once created, custom mongodb_block_uis behave just like default and module-generated mongodb_block_uis.', array('@mongodb_block_uis' => url('admin/structure/mongodb_block_ui'), '@mongodb_block_ui-add' => url('admin/structure/mongodb_block_ui/add'))) . '</dd>';
  23. $output .= '</dl>';
  24. return $output;
  25. case 'admin/structure/mongodb_block/add':
  26. return '<p>' . t('Use this page to create a new custom mongodb_block_ui.') . '</p>';
  27. }
  28. }
  29. /**
  30. * Implements hook_theme().
  31. */
  32. function mongodb_block_ui_theme() {
  33. return array(
  34. 'mongodb_block_ui_admin_display_form' => array(
  35. 'template' => 'mongodb-block-ui-admin-display-form',
  36. 'file' => 'mongodb_block_ui.admin.inc',
  37. 'render element' => 'form',
  38. ),
  39. );
  40. }
  41. /**
  42. * Implements hook_permission().
  43. */
  44. function mongodb_block_ui_permission() {
  45. return array(
  46. 'administer mongodb blocks' => array(
  47. 'title' => t('administer mongodb blocks'),
  48. ),
  49. );
  50. }
  51. /**
  52. * Implements hook_menu().
  53. */
  54. function mongodb_block_ui_menu() {
  55. $default_theme = variable_get('theme_default', 'garland');
  56. $items['admin/structure/mongodb_block'] = array(
  57. 'title' => 'Blocks',
  58. 'description' => 'Configure what blocks content appears in your site\'s sidebars and other regions.',
  59. 'page callback' => 'mongodb_block_ui_admin_display',
  60. 'page arguments' => array($default_theme),
  61. 'access arguments' => array('administer mongodb blocks'),
  62. 'file' => 'mongodb_block_ui.admin.inc',
  63. );
  64. $items['admin/structure/mongodb_block/list'] = array(
  65. 'title' => 'List',
  66. 'type' => MENU_DEFAULT_LOCAL_TASK,
  67. 'weight' => -10,
  68. );
  69. $items['admin/structure/mongodb_block/manage/%/%/%'] = array(
  70. 'title' => 'Configure mongodb_block',
  71. 'page callback' => 'drupal_get_form',
  72. 'page arguments' => array('mongodb_block_ui_admin_configure', 4, 5, 6),
  73. 'access arguments' => array('administer mongodb blocks'),
  74. 'file' => 'mongodb_block_ui.admin.inc',
  75. );
  76. $items['admin/structure/mongodb_block/manage/%/%/%/configure'] = array(
  77. 'title' => 'Configure mongodb_blocks',
  78. 'type' => MENU_DEFAULT_LOCAL_TASK,
  79. 'context' => MENU_CONTEXT_INLINE,
  80. );
  81. $items['admin/structure/mongodb_block/manage/%/%/%/delete'] = array(
  82. 'title' => 'Delete block',
  83. 'page callback' => 'drupal_get_form',
  84. 'page arguments' => array('mongodb_block_ui_custom_block_delete', 4, 5, 6),
  85. 'access arguments' => array('administer mongodb blocks'),
  86. 'type' => MENU_LOCAL_TASK,
  87. 'context' => MENU_CONTEXT_NONE,
  88. 'file' => 'mongodb_block_ui.admin.inc',
  89. );
  90. foreach (list_themes() as $key => $theme) {
  91. $items['admin/structure/mongodb_block/list/' . $key] = array(
  92. 'title' => check_plain($theme->info['name']),
  93. 'page arguments' => array($key),
  94. 'type' => $key == $default_theme ? MENU_DEFAULT_LOCAL_TASK : MENU_LOCAL_TASK,
  95. 'weight' => $key == $default_theme ? -10 : 0,
  96. 'access callback' => '_mongodb_block_ui_themes_access',
  97. 'access arguments' => array($theme),
  98. 'file' => 'mongodb_block_ui.admin.inc',
  99. );
  100. $items['admin/structure/mongodb_block/list/' . $key . '/add'] = array(
  101. 'title' => 'Add block',
  102. 'page callback' => 'drupal_get_form',
  103. 'page arguments' => array('mongodb_block_ui_add_block_form', $key),
  104. 'access arguments' => array('administer mongodb blocks'),
  105. 'type' => MENU_LOCAL_ACTION,
  106. 'file' => 'mongodb_block_ui.admin.inc',
  107. );
  108. $items['admin/structure/mongodb_block/demo/' . $key] = array(
  109. 'title' => check_plain($theme->info['name']),
  110. 'page callback' => 'mongodb_block_ui_admin_demo',
  111. 'page arguments' => array($key),
  112. 'type' => MENU_CALLBACK,
  113. 'access callback' => '_mongodb_block_ui_themes_access',
  114. 'access arguments' => array($theme),
  115. 'theme callback' => '_mongodb_block_ui_custom_theme',
  116. 'theme arguments' => array($key),
  117. 'file' => 'mongodb_block_ui.admin.inc',
  118. );
  119. }
  120. return $items;
  121. }
  122. /**
  123. * Menu item access callback - only admin or enabled themes can be accessed.
  124. */
  125. function _mongodb_block_ui_themes_access($theme) {
  126. return user_access('administer mongodb blocks') && drupal_theme_access($theme);
  127. }
  128. /**
  129. * Implements hook_block_info().
  130. *
  131. * This function loads custom blocks.
  132. */
  133. function mongodb_block_ui_block_info() {
  134. $blocks = array();
  135. $collection = mongodb_collection('block_custom');
  136. $result = $collection->find(array(), array('info', '_id'))
  137. ->sort(array('info'));
  138. $blocks = new stdClass();
  139. foreach ($result as $block) {
  140. $delta = $block['_id'];
  141. // Here comes the magic... by using an object we can have string array
  142. // keys later when we cast to array.
  143. $blocks->$delta = array(
  144. 'info' => $block['info'],
  145. // Not worth caching.
  146. 'cache' => DRUPAL_NO_CACHE,
  147. 'status' => TRUE,
  148. );
  149. }
  150. // Here , the $delta becomes a string.
  151. return (array) $blocks;
  152. }
  153. /**
  154. * Implements hook_mongodb_block_info_alter().
  155. *
  156. * This function loads block customizations.
  157. */
  158. function mongodb_block_ui_mongodb_block_info_alter(&$blocks, $theme) {
  159. foreach (mongodb_collection('block_customized', $theme)->find() as $block) {
  160. $index = $block['_id']['module'] . '_' . $block['_id']['delta'];
  161. if (!isset($blocks[$index])) {
  162. continue;
  163. }
  164. unset($block['_id']);
  165. if (isset($block['region']) && $block['region'] != MONGODB_BLOCK_REGION_NONE) {
  166. $block['status'] = TRUE;
  167. }
  168. $blocks[$index] = $block + $blocks[$index];
  169. }
  170. }
  171. /**
  172. * Implements hook_block_view().
  173. *
  174. * Generates the administrator-defined block for display.
  175. */
  176. function mongodb_block_ui_block_view($delta = 0, $edit = array()) {
  177. $block = mongodb_block_ui_load($delta);
  178. $data['subject'] = check_plain($block['title']);
  179. $data['content'] = check_markup($block['body']['value'], $block['body']['format'], '', TRUE);
  180. return $data;
  181. }
  182. /**
  183. * Loads a block object.
  184. *
  185. * @param string $delta
  186. * Unique ID of the block
  187. */
  188. function mongodb_block_ui_load($delta) {
  189. return mongodb_collection('block_custom')->findOne(array('_id' => (string) $delta));
  190. }
  191. /**
  192. * Define the custom block form.
  193. */
  194. function mongodb_block_ui_custom_block_form($edit = array()) {
  195. $edit += array(
  196. 'info' => '',
  197. 'body' => array('value' => '', 'format' => filter_default_format()),
  198. );
  199. $form['info'] = array(
  200. '#type' => 'textfield',
  201. '#title' => t('Block description'),
  202. '#default_value' => $edit['info'],
  203. '#maxlength' => 64,
  204. '#description' => t('A brief description of your block. Used on the <a href="@overview">Blocks administration page</a>.', array('@overview' => url('admin/structure/mongodb_block'))),
  205. '#required' => TRUE,
  206. '#weight' => -19,
  207. );
  208. $form['body_field']['#weight'] = -17;
  209. $form['body_field']['body'] = array(
  210. '#type' => 'text_format',
  211. '#title' => t('Block body'),
  212. '#default_value' => $edit['body']['value'],
  213. '#format' => $edit['body']['format'],
  214. '#rows' => 15,
  215. '#description' => t('The content of the block as shown to the user.'),
  216. '#required' => TRUE,
  217. '#weight' => -17,
  218. '#access' => filter_access(filter_format_load($edit['body']['format'])),
  219. );
  220. return $form;
  221. }
  222. /**
  223. * Implements hook_menu_delete().
  224. */
  225. function mongodb_block_ui_menu_delete($menu) {
  226. $collection = mongodb_collection('block_custom');
  227. $collection->remove(array(
  228. 'module' => 'menu',
  229. 'delta' => $menu['menu_name'],
  230. ));
  231. }
  232. /**
  233. * Implements hook_form_FORM_ID_alter().
  234. */
  235. function mongodb_block_ui_form_system_performance_settings_alter(&$form, &$form_state) {
  236. $disabled = count(module_implements('node_grants'));
  237. $form['caching']['block_cache'] = array(
  238. '#type' => 'checkbox',
  239. '#title' => t('Cache blocks'),
  240. '#default_value' => variable_get('block_cache', FALSE),
  241. '#disabled' => $disabled,
  242. '#description' => $disabled ? t('Block caching is inactive because you have enabled modules defining content access restrictions.') : NULL,
  243. '#weight' => -1,
  244. );
  245. }