Commit 1650fea5 authored by webchick's avatar webchick

#516150 by David_Rothstein, dropcube, Senpai, alexanderpas, sun: Add fallback...

#516150 by David_Rothstein, dropcube, Senpai, alexanderpas, sun: Add fallback for main content block rendering. (Make it so you can't render your site completely unusable by disabling block module. Oopsie.)
parent 24844396
......@@ -4513,10 +4513,17 @@ function drupal_alter($type, &$data, &$context1 = NULL, &$context2 = NULL) {
*/
function drupal_set_page_content($content = NULL) {
$content_block = &drupal_static(__FUNCTION__, NULL);
$main_content_display = &drupal_static('system_main_content_added', FALSE);
if (!empty($content)) {
$content_block = (is_array($content) ? $content : array('main' => array('#markup' => $content)));
}
else {
// Indicate that the main content has been requested. We assume that
// the module requesting the content will be adding it to the page.
// A module can indicate that it does not handle the content by setting
// the static variable back to FALSE after calling this function.
$main_content_display = TRUE;
return $content_block;
}
}
......@@ -4534,6 +4541,8 @@ function drupal_set_page_content($content = NULL) {
* @see element_info('page')
*/
function drupal_render_page($page) {
$main_content_display = &drupal_static('system_main_content_added', FALSE);
// Allow menu callbacks to return strings or arbitrary arrays to render.
// If the array returned is not of #type page directly, we need to fill
// in the page with defaults.
......@@ -4551,6 +4560,13 @@ function drupal_render_page($page) {
// 'sidebar_first', 'footer', etc.
drupal_alter('page', $page);
// If no module has taken care of the main content, add it to the page now.
// This allows the site to still be usable even if no modules that
// control page regions (for example, the Block module) are enabled.
if (!$main_content_display) {
$page['content']['system_main'] = drupal_set_page_content();
}
return drupal_render($page);
}
......
......@@ -2211,20 +2211,20 @@ function template_preprocess_page(&$variables) {
// Move some variables to the top level for themer convenience and template cleanliness.
$variables['show_messages'] = $variables['page']['#show_messages'];
foreach (system_region_list($GLOBALS['theme']) as $region_key => $region_name) {
if (!isset($variables['page'][$region_key])) {
$variables['page'][$region_key] = array();
}
}
// Set up layout variable.
$variables['layout'] = 'none';
if (!empty($variables['page']['sidebar_first'])) {
$variables['layout'] = 'first';
}
else {
$variables['page']['sidebar_first'] = array();
}
if (!empty($variables['page']['sidebar_second'])) {
$variables['layout'] = ($variables['layout'] == 'first') ? 'both' : 'second';
}
else {
$variables['page']['sidebar_second'] = array();
}
$variables['base_path'] = base_path();
$variables['front_page'] = url();
......
......@@ -73,6 +73,27 @@ function system_test_menu() {
'type' => MENU_CALLBACK,
);
$items['system-test/main-content-handling'] = array(
'title' => 'Test main content handling',
'page callback' => 'system_test_main_content_fallback',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
$items['system-test/main-content-fallback'] = array(
'title' => 'Test main content fallback',
'page callback' => 'system_test_main_content_fallback',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
$items['system-test/main-content-duplication'] = array(
'title' => 'Test main content duplication',
'page callback' => 'system_test_main_content_fallback',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
return $items;
}
......@@ -226,3 +247,31 @@ function system_test_lock_exit() {
return 'FALSE: Lock not acquired in system_test_lock_exit()';
}
}
/**
* Implement hook_page_build().
*/
function system_test_page_build(&$page) {
$menu_item = menu_get_item();
$main_content_display = &drupal_static('system_main_content_added', FALSE);
if ($menu_item['path'] == 'system-test/main-content-handling') {
$page['footer'] = drupal_set_page_content();
$page['footer']['main']['#markup'] = '<div id="system-test-content">' . $page['footer']['main']['#markup'] . '</div>';
}
else if ($menu_item['path'] == 'system-test/main-content-fallback') {
drupal_set_page_content();
$main_content_display = FALSE;
}
else if ($menu_item['path'] == 'system-test/main-content-duplication') {
drupal_set_page_content();
}
}
/**
* Menu callback to test main content fallback().
*/
function system_test_main_content_fallback() {
return t('Content to test main content fallback');
}
......@@ -999,7 +999,89 @@ class SystemBlockTestCase extends DrupalWebTestCase {
$this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
$this->drupalPost('admin/structure/block/manage/system/powered-by/configure', array('title' => '', 'color' => 'powered-blue', 'size' => '80x15'), t('Save block'));
}
}
/**
* Test main content rendering fallback provided by system module.
*/
class SystemMainContentFallback extends DrupalWebTestCase {
protected $admin_user;
protected $web_user;
public static function getInfo() {
return array(
'name' => 'Main content rendering fallback',
'description' => ' Test system module main content rendering fallback.',
'group' => 'System',
);
}
function setUp() {
parent::setUp('system_test');
// Create and login admin user.
$this->admin_user = $this->drupalCreateUser(array(
'access administration pages',
'administer site configuration',
'administer blocks',
'administer nodes',
));
$this->drupalLogin($this->admin_user);
// Create a web user.
$this->web_user = $this->drupalCreateUser(array('access user profiles', 'access content'));
}
/**
* Test availability of main content.
*/
function testMainContentFallback() {
$edit = array();
// Disable the dashboard module, which depends on the block module.
$edit['modules[Core][dashboard][enable]'] = FALSE;
$this->drupalPost('admin/config/modules', $edit, t('Save configuration'));
$this->assertText(t('The configuration options have been saved.'), t('Modules status has been updated.'));
// Disable the block module.
$edit['modules[Core][block][enable]'] = FALSE;
$this->drupalPost('admin/config/modules', $edit, t('Save configuration'));
$this->assertText(t('The configuration options have been saved.'), t('Modules status has been updated.'));
module_list(TRUE);
$this->assertFalse(module_exists('block'), t('Block module disabled.'));
// At this point, no region is filled and fallback should be triggered.
$this->drupalGet('admin/config/system/site-information');
$this->assertField('site_name', t('Admin interface still availble.'));
// Fallback should not trigger when another module is handling content.
$this->drupalGet('system-test/main-content-handling');
$this->assertRaw('id="system-test-content"', t('Content handled by another module'));
$this->assertText(t('Content to test main content fallback'), t('Main content still displayed.'));
// Fallback should trigger when another module
// indicates that it is not handling the content.
$this->drupalGet('system-test/main-content-fallback');
$this->assertText(t('Content to test main content fallback'), t('Main content fallback properly triggers.'));
// Fallback should not trigger when another module is handling content.
// Note that this test ensures that no duplicate
// content gets created by the fallback.
$this->drupalGet('system-test/main-content-duplication');
$this->assertNoText(t('Content to test main content fallback'), t('Main content not duplicated.'));
// Request a user* page and see if it is displayed.
$this->drupalLogin($this->web_user);
$this->drupalGet('user/' . $this->web_user->uid . '/edit');
$this->assertField('mail', t('User interface still availble.'));
// Enable the block module again.
$this->drupalLogin($this->admin_user);
$edit = array();
$edit['modules[Core][block][enable]'] = 'block';
$this->drupalPost('admin/config/modules', $edit, t('Save configuration'));
$this->assertText(t('The configuration options have been saved.'), t('Modules status has been updated.'));
module_list(TRUE);
$this->assertTrue(module_exists('block'), t('Block module re-enabled.'));
}
}
class SystemSettingsForm extends DrupalWebTestCase {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment