context_reaction_block.inc 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  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. // Make blocks editable if allowed.
  368. if ($this->is_editable_region($r)) {
  369. foreach ($context_blocks[$r] as $key => $block) {
  370. $context_blocks[$r][$key] = $this->editable_block($block);
  371. }
  372. }
  373. }
  374. // Sort blocks.
  375. uasort($context_blocks[$r], array('context_reaction_block', 'block_sort'));
  376. }
  377. }
  378. return isset($context_blocks[$region]) ? $context_blocks[$region] : array();
  379. }
  380. /**
  381. * Determine if there is an active context editor block, and set a flag. We will set a flag so
  382. * that we can make sure that blocks with empty content have some default content. This is
  383. * needed so the save of the context inline editor does not remove the blocks with no content.
  384. */
  385. function is_editable_check($context_blocks) {
  386. foreach ($context_blocks as $r => $blocks) {
  387. if (isset($blocks['context_ui-editor'])) {
  388. $block = $blocks['context_ui-editor'];
  389. // see if the editor is actually enabled, lifted from _block_render_blocks
  390. 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'))) {
  391. $array = $cache->data;
  392. }
  393. else {
  394. $array = module_invoke($block->module, 'block_view', $block->delta);
  395. drupal_alter(array('block_view', "block_view_{$block->module}_{$block->delta}"), $array, $block);
  396. }
  397. if(!empty($array['content'])) {
  398. context_set('context_ui', 'context_ui_editor_present', TRUE);
  399. }
  400. break;
  401. }
  402. }
  403. }
  404. /**
  405. * Generate the safe weight range for a block being added to a region such that
  406. * there are enough potential unique weights to support all blocks.
  407. */
  408. protected function max_block_weight() {
  409. $blocks = $this->get_blocks();
  410. $block_count = 0;
  411. foreach ($blocks as $region => $block_list) {
  412. $block_count += count($block_list);
  413. }
  414. // Add 2 to make sure there's space at either end of the block list
  415. return round(($block_count + 2) / 2);
  416. }
  417. /**
  418. * Check or set whether a rebuild of the block info cache is needed.
  419. */
  420. function rebuild_needed($set = NULL) {
  421. if (isset($set) && $set != variable_get('context_block_rebuild_needed', FALSE)) {
  422. variable_set('context_block_rebuild_needed', $set);
  423. }
  424. return (bool) variable_get('context_block_rebuild_needed', FALSE);
  425. }
  426. /**
  427. * Helper function to generate a list of blocks from a specified region. If provided a context object,
  428. * will generate a full list of blocks for that region distinguishing between system blocks and
  429. * context-provided blocks.
  430. *
  431. * @param $region
  432. * The string identifier for a theme region. e.g. "left"
  433. * @param $context
  434. * A context object.
  435. *
  436. * @return
  437. * A keyed (by "module_delta" convention) array of blocks.
  438. */
  439. function get_blocks($region = NULL, $context = NULL, $reset = FALSE) {
  440. static $block_info;
  441. $theme_key = variable_get('theme_default', 'garland');
  442. if (!isset($block_info) || $reset) {
  443. $block_info = array();
  444. if (!$reset) {
  445. $block_info = context_cache_get('block_info');
  446. }
  447. if (empty($block_info)) {
  448. if (module_exists('block')) {
  449. $block_blocks = _block_rehash($theme_key);
  450. $block_info = array();
  451. // Change from numeric keys to module-delta.
  452. foreach ($block_blocks as $block) {
  453. $block = (object) $block;
  454. unset($block->theme, $block->status, $block->weight, $block->region, $block->custom, $block->visibility, $block->pages);
  455. $block->bid = "{$block->module}-{$block->delta}";
  456. $block_info[$block->bid] = $block;
  457. }
  458. }
  459. else {
  460. $block_info = array();
  461. foreach (module_implements('block_info') as $module) {
  462. $module_blocks = module_invoke($module, 'block_info');
  463. if (!empty($module_blocks)) {
  464. foreach ($module_blocks as $delta => $block) {
  465. $block = (object) $block;
  466. $block->module = $module;
  467. $block->delta = $delta;
  468. $block->bid = "{$block->module}-{$block->delta}";
  469. $block_info[$block->bid] = $block;
  470. }
  471. }
  472. }
  473. }
  474. context_cache_set('block_info', $block_info);
  475. }
  476. // Allow other modules that may declare blocks dynamically to alter
  477. // this list.
  478. drupal_alter('context_block_info', $block_info);
  479. // Gather only region info from the database.
  480. if (module_exists('block')) {
  481. $result = db_select('block')
  482. ->fields('block')
  483. ->condition('theme', $theme_key)
  484. ->execute()
  485. ->fetchAllAssoc('bid');
  486. drupal_alter('block_list', $result);
  487. drupal_alter('context_block_list', $result);
  488. foreach ($result as $row) {
  489. if (isset($block_info["{$row->module}-{$row->delta}"])) {
  490. $block_info["{$row->module}-{$row->delta}"] = (object) array_merge((array) $row, (array) $block_info["{$row->module}-{$row->delta}"]);
  491. unset($block_info["{$row->module}-{$row->delta}"]->status);
  492. unset($block_info["{$row->module}-{$row->delta}"]->visibility);
  493. }
  494. }
  495. }
  496. }
  497. $blocks = array();
  498. // No region specified, provide all blocks.
  499. if (!isset($region)) {
  500. $blocks = $block_info;
  501. }
  502. // Region specified.
  503. else {
  504. foreach ($block_info as $bid => $block) {
  505. if (isset($block->region) && $block->region == $region) {
  506. $blocks[$bid] = $block;
  507. }
  508. }
  509. }
  510. // Add context blocks if provided.
  511. if (is_object($context) && $options = $this->fetch_from_context($context)) {
  512. if (!empty($options['blocks'])) {
  513. foreach ($options['blocks'] as $block) {
  514. if (
  515. isset($block_info["{$block['module']}-{$block['delta']}"]) && // Block is valid.
  516. (!isset($region) || (!empty($region) && $block['region'] == $region)) // No region specified or regions match.
  517. ) {
  518. $context_block = $block_info["{$block['module']}-{$block['delta']}"];
  519. $context_block->weight = $block['weight'];
  520. $context_block->region = $block['region'];
  521. $context_block->context = !empty($context->name) ? $context->name : 'tempname';
  522. $blocks[$context_block->bid] = $context_block;
  523. }
  524. }
  525. }
  526. uasort($blocks, array('context_reaction_block', 'block_sort'));
  527. }
  528. return $blocks;
  529. }
  530. /**
  531. * Sort callback.
  532. */
  533. static function block_sort($a, $b) {
  534. return ($a->weight - $b->weight);
  535. }
  536. /**
  537. * Compatibility wrapper around json_decode().
  538. */
  539. protected function json_decode($json, $assoc = FALSE) {
  540. // Requires PHP 5.2.
  541. if (function_exists('json_decode')) {
  542. return json_decode($json, $assoc);
  543. }
  544. else {
  545. watchdog('context', 'Please upgrade your PHP version to one that supports json_decode.');
  546. }
  547. }
  548. /**
  549. * Block renderer for AJAX requests. Triggered when $_GET['context_block']
  550. * is set. See ->execute() for how this is called.
  551. */
  552. function render_ajax($param) {
  553. // Besure the page isn't a 404 or 403.
  554. $headers = drupal_get_http_header();
  555. if (array_key_exists('status', $headers) && ($headers['status'] == "404 Not Found" || $headers['status'] == "403 Forbidden")) {
  556. return;
  557. }
  558. // Set the header right away. This will inform any players in the stack
  559. // that we are in the middle of responding to an AJAX request.
  560. drupal_add_http_header('Content-Type', 'text/javascript; charset=utf-8');
  561. if (strpos($param, ',') !== FALSE) {
  562. list($bid, $context) = explode(',', $param);
  563. list($module, $delta) = explode('-', $bid, 2);
  564. // Check token to make sure user has access to block.
  565. if (!(user_access('administer contexts') || user_access('context ajax block access') || $this->context_block_ajax_rendering_allowed($bid))) {
  566. echo drupal_json_encode(array('status' => 0));
  567. exit;
  568. }
  569. // Ensure $bid is valid.
  570. $info = $this->get_blocks();
  571. if (isset($info[$bid])) {
  572. module_load_include('module', 'block', 'block');
  573. $block = $info[$bid];
  574. $block->title = isset($block->title) ? $block->title : '';
  575. $block->context = $context;
  576. $block->region = '';
  577. $rendered_blocks = _block_render_blocks(array($block)); // For E_STRICT warning
  578. $block = array_shift($rendered_blocks);
  579. if (empty($block->content['#markup'])) {
  580. $block->content['#markup'] = "<div class='context-block-empty'>" . t('This block appears empty when displayed on this page.') . "</div>";
  581. }
  582. $block = $this->editable_block($block);
  583. $renderable_block = _block_get_renderable_array(array($block)); // For E_STRICT warning
  584. echo drupal_json_encode(array(
  585. 'status' => 1,
  586. 'block' => drupal_render($renderable_block),
  587. ));
  588. drupal_exit();
  589. }
  590. }
  591. echo drupal_json_encode(array('status' => 0));
  592. drupal_exit();
  593. }
  594. /**
  595. * Provide caching for system_region_list since it can get called
  596. * frequently. Evaluate for removal once https://drupal.org/node/1873450
  597. * lands or system_region_list is otherwise cached in core
  598. */
  599. protected function system_region_list($theme_key, $show = REGIONS_ALL) {
  600. static $cache = array();
  601. if (!isset($cache[$theme_key])) {
  602. $cache[$theme_key] = array();
  603. }
  604. if (!isset($cache[$theme_key][$show])) {
  605. $cache[$theme_key][$show] = system_region_list($theme_key, $show);
  606. }
  607. return $cache[$theme_key][$show];
  608. }
  609. /**
  610. * Allow modules to selectively allow ajax rendering of a specific block
  611. */
  612. private function context_block_ajax_rendering_allowed($bid) {
  613. $allowed = FALSE;
  614. foreach (module_invoke_all('context_allow_ajax_block_access', $bid) as $module_allow) {
  615. $allowed = $allow || $module_allow;
  616. if ($allowed) {
  617. break;
  618. }
  619. }
  620. return $allowed;
  621. }
  622. }