context_reaction_block.inc 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. <?php
  2. /**
  3. * Expose blocks as context reactions.
  4. */
  5. class context_reaction_block extends context_reaction {
  6. /**
  7. * Options form.
  8. */
  9. function options_form($context) {
  10. // Rebuild the block info cache if necessary.
  11. $this->get_blocks(NULL, NULL, $this->rebuild_needed());
  12. $this->rebuild_needed(FALSE);
  13. $theme_key = variable_get('theme_default', 'garland');
  14. $weight_delta = $this->max_block_weight();
  15. $form = array(
  16. '#tree' => TRUE,
  17. '#theme' => 'context_block_form',
  18. 'max_block_weight' => array(
  19. '#value' => $weight_delta,
  20. '#type' => 'value',
  21. ),
  22. 'state' => array(
  23. '#type' => 'hidden',
  24. '#attributes' => array('class' => 'context-blockform-state'),
  25. ),
  26. );
  27. /**
  28. * Selector.
  29. */
  30. $modules = module_list();
  31. $form['selector'] = array(
  32. '#type' => 'item',
  33. '#tree' => TRUE,
  34. '#prefix' => '<div class="context-blockform-selector">',
  35. '#suffix' => '</div>',
  36. );
  37. foreach ($this->get_blocks() as $block) {
  38. $group = isset($block->context_group) ? $block->context_group : $block->module;
  39. if (!isset($form['selector'][$group])) {
  40. $form['selector'][$group] = array(
  41. '#type' => 'fieldset',
  42. '#collapsible' => TRUE,
  43. '#collapsed' => TRUE,
  44. '#title' => isset($block->context_group) ? $block->context_group : $modules[$block->module],
  45. );
  46. $form['selector'][$group]['checkboxes'] = array(
  47. '#type' => 'checkboxes',
  48. '#options' => array(),
  49. );
  50. }
  51. $form['selector'][$group]['checkboxes']['#options'][$block->bid] = check_plain($block->info);
  52. }
  53. ksort($form['selector']);
  54. /**
  55. * Regions.
  56. */
  57. $form['blocks'] = array(
  58. '#tree' => TRUE,
  59. '#theme' => 'context_block_regions_form',
  60. );
  61. foreach ($this->system_region_list($theme_key, REGIONS_VISIBLE) as $region => $label) {
  62. $form['blocks'][$region] = array(
  63. '#type' => 'item',
  64. '#title' => $label,
  65. '#tree' => TRUE,
  66. );
  67. foreach ($this->get_blocks($region, $context) as $block) {
  68. if (!empty($block->context)) {
  69. $form['blocks'][$region][$block->bid] = array(
  70. '#value' => check_plain($block->info),
  71. '#weight' => $block->weight,
  72. '#type' => 'markup',
  73. '#tree' => TRUE,
  74. 'weight' => array('#type' => 'weight', '#delta' => $weight_delta, '#default_value' => $block->weight),
  75. );
  76. }
  77. }
  78. }
  79. return $form;
  80. }
  81. /**
  82. * Options form submit handler.
  83. */
  84. function options_form_submit($values) {
  85. $blocks = array();
  86. $block_info = $this->get_blocks();
  87. // Retrieve blocks from submitted JSON string.
  88. if (!empty($values['state'])) {
  89. $edited = $this->json_decode($values['state']);
  90. }
  91. else {
  92. $edited = array();
  93. }
  94. foreach ($edited as $region => $block_data) {
  95. foreach ($block_data as $position => $data) {
  96. if (isset($block_info[$data->bid])) {
  97. $blocks[$data->bid] = array(
  98. 'module' => $block_info[$data->bid]->module,
  99. 'delta' => $block_info[$data->bid]->delta,
  100. 'region' => $region,
  101. 'weight' => $data->weight,
  102. );
  103. }
  104. }
  105. }
  106. return array('blocks' => $blocks);
  107. }
  108. /**
  109. * Context editor form for blocks.
  110. */
  111. function editor_form($context) {
  112. $form = array();
  113. drupal_add_library('system', 'ui.droppable');
  114. drupal_add_library('system', 'ui.sortable');
  115. drupal_add_js(drupal_get_path('module', 'context_ui') . '/json2.js');
  116. drupal_add_js(drupal_get_path('module', 'context_ui') . '/theme/filter.js');
  117. drupal_add_js(drupal_get_path('module', 'context') . '/plugins/context_reaction_block.js');
  118. drupal_add_css(drupal_get_path('module', 'context') . '/plugins/context_reaction_block.css');
  119. // We might be called multiple times so use a static to ensure this is set just once.
  120. static $once;
  121. if (!isset($once)) {
  122. $settings = array(
  123. 'path' => drupal_is_front_page() ? base_path() : url($_GET['q']),
  124. 'params' => (object) array_diff_key($_GET, array('q' => '')),
  125. 'scriptPlaceholder' => theme('context_block_script_placeholder', array('text' => '')),
  126. );
  127. drupal_add_js(array('contextBlockEditor' => $settings), 'setting');
  128. $once = TRUE;
  129. }
  130. $form['state'] = array(
  131. '#type' => 'hidden',
  132. '#attributes' => array('class' => array('context-block-editor-state')),
  133. );
  134. $form['browser'] = array(
  135. '#markup' => theme('context_block_browser', array(
  136. 'blocks' => $this->get_blocks(NULL, NULL, $this->rebuild_needed()),
  137. 'context' => $context
  138. )),
  139. );
  140. $this->rebuild_needed(FALSE);
  141. return $form;
  142. }
  143. /**
  144. * Submit handler context editor form.
  145. */
  146. function editor_form_submit(&$context, $values) {
  147. $edited = !empty($values['state']) ? (array) $this->json_decode($values['state']) : array();
  148. $options = array();
  149. // Take the existing context values and remove blocks that belong affected regions.
  150. $affected_regions = array_keys($edited);
  151. if (!empty($context->reactions['block']['blocks'])) {
  152. $options = $context->reactions['block'];
  153. foreach ($options['blocks'] as $key => $block) {
  154. if (in_array($block['region'], $affected_regions)) {
  155. unset($options['blocks'][$key]);
  156. }
  157. }
  158. }
  159. // Iterate through blocks and add in the ones that belong to the context.
  160. foreach ($edited as $region => $blocks) {
  161. foreach ($blocks as $weight => $block) {
  162. if ($block->context === $context->name) {
  163. $split = explode('-', $block->bid);
  164. $options['blocks'][$block->bid] = array(
  165. 'module' => array_shift($split),
  166. 'delta' => implode('-', $split),
  167. 'region' => $region,
  168. 'weight' => $weight,
  169. );
  170. }
  171. }
  172. }
  173. return $options;
  174. }
  175. /**
  176. * Settings form for variables.
  177. */
  178. function settings_form() {
  179. $form = array();
  180. $form['context_reaction_block_all_regions'] = array(
  181. '#title' => t('Show all regions'),
  182. '#type' => 'checkbox',
  183. '#default_value' => variable_get('context_reaction_block_all_regions', FALSE),
  184. '#description' => t('Show all regions including those that are empty. Enable if you are administering your site using the inline editor.')
  185. );
  186. return $form;
  187. }
  188. /**
  189. * Execute.
  190. */
  191. function execute(&$page) {
  192. global $theme;
  193. // The theme system might not yet be initialized. We need $theme.
  194. drupal_theme_initialize();
  195. // If the context_block querystring param is set, switch to AJAX rendering.
  196. // Note that we check the output buffer for any content to ensure that we
  197. // are not in the middle of a PHP template render.
  198. if (isset($_GET['context_block']) && !ob_get_contents()) {
  199. return $this->render_ajax($_GET['context_block']);
  200. }
  201. // Populate all block regions
  202. $all_regions = $this->system_region_list($theme);
  203. // Load all region content assigned via blocks.
  204. foreach (array_keys($all_regions) as $region) {
  205. if ($this->is_enabled_region($region)) {
  206. if ($blocks = $this->block_get_blocks_by_region($region)) {
  207. // Are the blocks already sorted.
  208. $blocks_sorted = TRUE;
  209. // If blocks have already been placed in this region (most likely by
  210. // Block module), then merge in blocks from Context.
  211. if (isset($page[$region])) {
  212. $page[$region] = array_merge($page[$region], $blocks);
  213. // Restore the weights that Block module manufactured
  214. // @see _block_get_renderable_array()
  215. foreach ($page[$region] as &$block) {
  216. if (isset($block['#block']->weight)) {
  217. $block['#weight'] = $block['#block']->weight;
  218. $blocks_sorted = FALSE;
  219. }
  220. }
  221. }
  222. else {
  223. $page[$region] = $blocks;
  224. }
  225. $page[$region]['#sorted'] = $blocks_sorted;
  226. }
  227. }
  228. }
  229. }
  230. /**
  231. * Return a list of enabled regions for which blocks should be built.
  232. * Split out into a separate method for easy overrides in extending classes.
  233. */
  234. protected function is_enabled_region($region) {
  235. global $theme;
  236. $regions = array_keys($this->system_region_list($theme));
  237. return in_array($region, $regions, TRUE);
  238. }
  239. /**
  240. * Determine whether inline editing requirements are met and that the current
  241. * user may edit.
  242. */
  243. protected function is_editable_region($region, $reset = FALSE) {
  244. // Check requirements.
  245. // Generally speaking, it does not make sense to allow anonymous users to
  246. // edit a context inline. Though it may be possible to save (and restore)
  247. // an edited context to an anonymous user's cookie or session, it's an
  248. // edge case and probably not something we want to encourage anyway.
  249. static $requirements;
  250. if (!isset($requirements) || $reset) {
  251. global $user;
  252. if ($user->uid && user_access('administer contexts') && variable_get('context_ui_dialog_enabled', FALSE)) {
  253. $requirements = TRUE;
  254. drupal_add_library('system', 'ui.droppable');
  255. drupal_add_library('system', 'ui.sortable');
  256. drupal_add_js(drupal_get_path('module', 'context') . '/plugins/context_reaction_block.js');
  257. drupal_add_css(drupal_get_path('module', 'context') . '/plugins/context_reaction_block.css');
  258. }
  259. else {
  260. $requirements = FALSE;
  261. }
  262. }
  263. // Check that this region is not locked by the theme.
  264. global $theme;
  265. $info = system_get_info('theme', $theme);
  266. if ($info && isset($info['regions_locked']) && in_array($region, $info['regions_locked'])) {
  267. return FALSE;
  268. }
  269. // Check that this region is not hidden
  270. $visible = $this->system_region_list($theme, REGIONS_VISIBLE);
  271. return $requirements && $this->is_enabled_region($region) && isset($visible[$region]);
  272. }
  273. /**
  274. * Add markup for making a block editable.
  275. */
  276. protected function editable_block($block) {
  277. if (!empty($block->content)) {
  278. $block->content['#theme_wrappers'][] = 'context_block_edit_wrap';
  279. }
  280. else {
  281. // the block alter in context.module should ensure that blocks are never
  282. // empty if the inline editor is present but in the case that they are,
  283. // warn that editing the context is likely to cause this block to be dropped
  284. drupal_set_message(t('The block with delta @delta from module @module is not compatible with the inline editor and will be dropped from the context containing it if you edit contexts here', array('@delta' => $block->delta, '@module' => $block->module)), 'warning');
  285. }
  286. return $block;
  287. }
  288. /**
  289. * Add markup for making a region editable.
  290. */
  291. protected function editable_region($region, $build) {
  292. if ($this->is_editable_region($region) &&
  293. (!empty($build) ||
  294. variable_get('context_reaction_block_all_regions', FALSE) ||
  295. context_isset('context_ui', 'context_ui_editor_present'))
  296. ) {
  297. global $theme;
  298. $regions = $this->system_region_list($theme);
  299. $name = isset($regions[$region]) ? $regions[$region] : $region;
  300. // The negative weight + sorted will push our region marker to the top of the region
  301. $build['context'] = array(
  302. '#prefix' => "<div class='context-block-region' id='context-block-region-{$region}'>",
  303. '#markup' => "<span class='region-name'>{$name}</span>" .
  304. "<a class='context-ui-add-link'>" . t('Add a block here.') . '</a>',
  305. '#suffix' => '</div>',
  306. '#weight' => -100,
  307. );
  308. $build['#sorted'] = FALSE;
  309. }
  310. return $build;
  311. }
  312. /**
  313. * Get a renderable array of a region containing all enabled blocks.
  314. */
  315. function block_get_blocks_by_region($region) {
  316. module_load_include('module', 'block', 'block');
  317. $build = array();
  318. if ($list = $this->block_list($region)) {
  319. $build = _block_get_renderable_array($list);
  320. }
  321. if ($this->is_editable_region($region)) {
  322. $build = $this->editable_region($region, $build);
  323. }
  324. return $build;
  325. }
  326. /**
  327. * An alternative version of block_list() that provides any context enabled blocks.
  328. */
  329. function block_list($region) {
  330. module_load_include('module', 'block', 'block');
  331. $context_blocks = &drupal_static('context_reaction_block_list');;
  332. $contexts = context_active_contexts();
  333. if (!isset($context_blocks)) {
  334. $info = $this->get_blocks();
  335. $context_blocks = array();
  336. foreach ($contexts as $context) {
  337. $options = $this->fetch_from_context($context);
  338. if (!empty($options['blocks'])) {
  339. foreach ($options['blocks'] as $context_block) {
  340. $bid = "{$context_block['module']}-{$context_block['delta']}";
  341. if (isset($info[$bid])) {
  342. $block = (object) array_merge((array) $info[$bid], $context_block);
  343. $block->context = $context->name;
  344. $block->title = isset($info[$block->bid]->title) ? $info[$block->bid]->title : NULL;
  345. $block->cache = isset($info[$block->bid]->cache) ? $info[$block->bid]->cache : DRUPAL_NO_CACHE;
  346. $context_blocks[$block->region][$block->bid] = $block;
  347. }
  348. }
  349. }
  350. }
  351. $this->is_editable_check($context_blocks);
  352. global $theme;
  353. $active_regions = $this->system_region_list($theme);
  354. // Make context renders regions in the same order as core.
  355. $_context_blocks = array();
  356. foreach ($active_regions as $r => $name) {
  357. if (isset($context_blocks[$r])) {
  358. $_context_blocks[$r] = $context_blocks[$r];
  359. }
  360. }
  361. $context_blocks = $_context_blocks;
  362. unset($_context_blocks);
  363. foreach ($context_blocks as $r => $blocks) {
  364. // Only render blocks in an active region.
  365. if (array_key_exists($r, $active_regions)) {
  366. $context_blocks[$r] = _block_render_blocks($blocks);
  367. $editable = $this->is_editable_region($r);
  368. foreach ($context_blocks[$r] as $key => $block) {
  369. // Add the region property to each block.
  370. $context_blocks[$r][$key]->region = $r;
  371. // Make blocks editable if allowed.
  372. if ($editable) {
  373. $context_blocks[$r][$key] = $this->editable_block($block);
  374. }
  375. }
  376. }
  377. // Sort blocks.
  378. uasort($context_blocks[$r], array('context_reaction_block', 'block_sort'));
  379. }
  380. }
  381. return isset($context_blocks[$region]) ? $context_blocks[$region] : array();
  382. }
  383. /**
  384. * Determine if there is an active context editor block, and set a flag. We will set a flag so
  385. * that we can make sure that blocks with empty content have some default content. This is
  386. * needed so the save of the context inline editor does not remove the blocks with no content.
  387. */
  388. function is_editable_check($context_blocks) {
  389. foreach ($context_blocks as $r => $blocks) {
  390. if (isset($blocks['context_ui-editor'])) {
  391. $block = $blocks['context_ui-editor'];
  392. // see if the editor is actually enabled, lifted from _block_render_blocks
  393. if (!count(module_implements('node_grants')) && ($_SERVER['REQUEST_METHOD'] == 'GET' || $_SERVER['REQUEST_METHOD'] == 'HEAD') && ($cid = _block_get_cache_id($block)) && ($cache = cache_get($cid, 'cache_block'))) {
  394. $array = $cache->data;
  395. }
  396. else {
  397. $array = module_invoke($block->module, 'block_view', $block->delta);
  398. drupal_alter(array('block_view', "block_view_{$block->module}_{$block->delta}"), $array, $block);
  399. }
  400. if(!empty($array['content'])) {
  401. context_set('context_ui', 'context_ui_editor_present', TRUE);
  402. }
  403. break;
  404. }
  405. }
  406. }
  407. /**
  408. * Generate the safe weight range for a block being added to a region such that
  409. * there are enough potential unique weights to support all blocks.
  410. */
  411. protected function max_block_weight() {
  412. $blocks = $this->get_blocks();
  413. // Add 2 to make sure there's space at either end of the block list.
  414. return round((count($blocks) + 2) / 2);
  415. }
  416. /**
  417. * Check or set whether a rebuild of the block info cache is needed.
  418. */
  419. function rebuild_needed($set = NULL) {
  420. if (isset($set) && $set != variable_get('context_block_rebuild_needed', FALSE)) {
  421. variable_set('context_block_rebuild_needed', $set);
  422. }
  423. return (bool) variable_get('context_block_rebuild_needed', FALSE);
  424. }
  425. /**
  426. * Helper function to generate a list of blocks from a specified region. If provided a context object,
  427. * will generate a full list of blocks for that region distinguishing between system blocks and
  428. * context-provided blocks.
  429. *
  430. * @param $region
  431. * The string identifier for a theme region. e.g. "left"
  432. * @param $context
  433. * A context object.
  434. *
  435. * @return
  436. * A keyed (by "module_delta" convention) array of blocks.
  437. */
  438. function get_blocks($region = NULL, $context = NULL, $reset = FALSE) {
  439. static $block_info;
  440. $theme_key = variable_get('theme_default', 'garland');
  441. if (!isset($block_info) || $reset) {
  442. $block_info = array();
  443. if (!$reset) {
  444. $block_info = context_cache_get('block_info');
  445. }
  446. if (empty($block_info)) {
  447. if (module_exists('block')) {
  448. $block_blocks = _block_rehash($theme_key);
  449. $block_info = array();
  450. // Change from numeric keys to module-delta.
  451. foreach ($block_blocks as $block) {
  452. $block = (object) $block;
  453. unset($block->theme, $block->status, $block->weight, $block->region, $block->custom, $block->visibility, $block->pages);
  454. $block->bid = "{$block->module}-{$block->delta}";
  455. $block_info[$block->bid] = $block;
  456. }
  457. }
  458. else {
  459. $block_info = array();
  460. foreach (module_implements('block_info') as $module) {
  461. $module_blocks = module_invoke($module, 'block_info');
  462. if (!empty($module_blocks)) {
  463. foreach ($module_blocks as $delta => $block) {
  464. $block = (object) $block;
  465. $block->module = $module;
  466. $block->delta = $delta;
  467. $block->bid = "{$block->module}-{$block->delta}";
  468. $block_info[$block->bid] = $block;
  469. }
  470. }
  471. }
  472. }
  473. context_cache_set('block_info', $block_info);
  474. }
  475. // Allow other modules that may declare blocks dynamically to alter
  476. // this list.
  477. drupal_alter('context_block_info', $block_info);
  478. // Gather only region info from the database.
  479. if (module_exists('block')) {
  480. $result = db_select('block')
  481. ->fields('block')
  482. ->condition('theme', $theme_key)
  483. ->execute()
  484. ->fetchAllAssoc('bid');
  485. drupal_alter('block_list', $result);
  486. drupal_alter('context_block_list', $result);
  487. foreach ($result as $row) {
  488. if (isset($block_info["{$row->module}-{$row->delta}"])) {
  489. $block_info["{$row->module}-{$row->delta}"] = (object) array_merge((array) $row, (array) $block_info["{$row->module}-{$row->delta}"]);
  490. unset($block_info["{$row->module}-{$row->delta}"]->status);
  491. unset($block_info["{$row->module}-{$row->delta}"]->visibility);
  492. }
  493. }
  494. }
  495. }
  496. $blocks = array();
  497. // No region specified, provide all blocks.
  498. if (!isset($region)) {
  499. $blocks = $block_info;
  500. }
  501. // Region specified.
  502. else {
  503. foreach ($block_info as $bid => $block) {
  504. if (isset($block->region) && $block->region == $region) {
  505. $blocks[$bid] = $block;
  506. }
  507. }
  508. }
  509. // Add context blocks if provided.
  510. if (is_object($context) && $options = $this->fetch_from_context($context)) {
  511. if (!empty($options['blocks'])) {
  512. foreach ($options['blocks'] as $block) {
  513. if (
  514. isset($block_info["{$block['module']}-{$block['delta']}"]) && // Block is valid.
  515. (!isset($region) || (!empty($region) && $block['region'] == $region)) // No region specified or regions match.
  516. ) {
  517. $context_block = $block_info["{$block['module']}-{$block['delta']}"];
  518. $context_block->weight = $block['weight'];
  519. $context_block->region = $block['region'];
  520. $context_block->context = !empty($context->name) ? $context->name : 'tempname';
  521. $blocks[$context_block->bid] = $context_block;
  522. }
  523. }
  524. }
  525. uasort($blocks, array('context_reaction_block', 'block_sort'));
  526. }
  527. return $blocks;
  528. }
  529. /**
  530. * Sort callback.
  531. */
  532. static function block_sort($a, $b) {
  533. return ($a->weight - $b->weight);
  534. }
  535. /**
  536. * Compatibility wrapper around json_decode().
  537. */
  538. protected function json_decode($json, $assoc = FALSE) {
  539. // Requires PHP 5.2.
  540. if (function_exists('json_decode')) {
  541. return json_decode($json, $assoc);
  542. }
  543. else {
  544. watchdog('context', 'Please upgrade your PHP version to one that supports json_decode.');
  545. }
  546. }
  547. /**
  548. * Block renderer for AJAX requests. Triggered when $_GET['context_block']
  549. * is set. See ->execute() for how this is called.
  550. */
  551. function render_ajax($param) {
  552. // Besure the page isn't a 404 or 403.
  553. $headers = drupal_get_http_header();
  554. if (array_key_exists('status', $headers) && ($headers['status'] == "404 Not Found" || $headers['status'] == "403 Forbidden")) {
  555. return;
  556. }
  557. // Set the header right away. This will inform any players in the stack
  558. // that we are in the middle of responding to an AJAX request.
  559. drupal_add_http_header('Content-Type', 'text/javascript; charset=utf-8');
  560. if (strpos($param, ',') !== FALSE) {
  561. list($bid, $context) = explode(',', $param);
  562. list($module, $delta) = explode('-', $bid, 2);
  563. // Check token to make sure user has access to block.
  564. if (!(user_access('administer contexts') || user_access('context ajax block access') || $this->context_block_ajax_rendering_allowed($bid))) {
  565. echo drupal_json_encode(array('status' => 0));
  566. exit;
  567. }
  568. // Ensure $bid is valid.
  569. $info = $this->get_blocks();
  570. if (isset($info[$bid])) {
  571. module_load_include('module', 'block', 'block');
  572. $block = $info[$bid];
  573. $block->title = isset($block->title) ? $block->title : '';
  574. $block->context = $context;
  575. $block->region = '';
  576. $rendered_blocks = _block_render_blocks(array($block)); // For E_STRICT warning
  577. $block = array_shift($rendered_blocks);
  578. if (empty($block->content['#markup'])) {
  579. $block->content['#markup'] = "<div class='context-block-empty'>" . t('This block appears empty when displayed on this page.') . "</div>";
  580. }
  581. $block = $this->editable_block($block);
  582. $renderable_block = _block_get_renderable_array(array($block)); // For E_STRICT warning
  583. echo drupal_json_encode(array(
  584. 'status' => 1,
  585. 'block' => drupal_render($renderable_block),
  586. ));
  587. drupal_exit();
  588. }
  589. }
  590. echo drupal_json_encode(array('status' => 0));
  591. drupal_exit();
  592. }
  593. /**
  594. * Provide caching for system_region_list since it can get called
  595. * frequently. Evaluate for removal once https://drupal.org/node/1873450
  596. * lands or system_region_list is otherwise cached in core
  597. */
  598. protected function system_region_list($theme_key, $show = REGIONS_ALL) {
  599. static $cache = array();
  600. if (!isset($cache[$theme_key])) {
  601. $cache[$theme_key] = array();
  602. }
  603. if (!isset($cache[$theme_key][$show])) {
  604. $cache[$theme_key][$show] = system_region_list($theme_key, $show);
  605. }
  606. return $cache[$theme_key][$show];
  607. }
  608. /**
  609. * Allow modules to selectively allow ajax rendering of a specific block
  610. */
  611. private function context_block_ajax_rendering_allowed($bid) {
  612. $allowed = FALSE;
  613. foreach (module_invoke_all('context_allow_ajax_block_access', $bid) as $module_allow) {
  614. $allowed = $allow || $module_allow;
  615. if ($allowed) {
  616. break;
  617. }
  618. }
  619. return $allowed;
  620. }
  621. }