- Patch #35524 by asimmonds / drewish: converted the custom block forms code to the forms API's execute model.

4.7.x
Dries Buytaert 2005-11-13 08:26:01 +00:00
parent 3b7a6a4931
commit c8f6a2421b
2 changed files with 196 additions and 180 deletions

View File

@ -212,9 +212,18 @@ function _block_rehash($order_by = array('weight')) {
* Prepare the main block administration form. * Prepare the main block administration form.
*/ */
function block_admin_display() { function block_admin_display() {
global $theme_key; global $theme_key, $custom_theme;
$throttle = module_exist('throttle'); $throttle = module_exist('throttle');
// If non-default theme configuration has been selected, set the custom theme.
if (arg(3)) {
$custom_theme = arg(3);
}
else {
$custom_theme = variable_get('theme_default', 'bluemarine');
}
init_theme();
$blocks = _block_rehash(); $blocks = _block_rehash();
$block_regions = system_region_list($theme_key); $block_regions = system_region_list($theme_key);
@ -241,15 +250,9 @@ function block_admin_display() {
} }
function theme_block_admin_display($form) { function theme_block_admin_display($form) {
global $theme_key;
global $theme_key, $custom_theme;
$throttle = module_exist('throttle'); $throttle = module_exist('throttle');
// If non-default theme configuration has been selected, set the custom theme.
if (arg(3)) {
$custom_theme = arg(3);
init_theme();
}
$block_regions = system_region_list($theme_key); $block_regions = system_region_list($theme_key);
// Highlight regions on page, to provide visual reference. // Highlight regions on page, to provide visual reference.
@ -312,76 +315,83 @@ function block_box_get($bid) {
* Menu callback; displays the block configuration form. * Menu callback; displays the block configuration form.
*/ */
function block_admin_configure($module = NULL, $delta = 0) { function block_admin_configure($module = NULL, $delta = 0) {
$edit = $_POST['edit'];
$op = $_POST['op'];
switch ($op) { $form['module'] = array('#type' => 'value', '#value' => $module);
case t('Save block'): $form['delta'] = array('#type' => 'value', '#value' => $delta);
db_query("UPDATE {blocks} SET visibility = %d, pages = '%s', custom = %d WHERE module = '%s' AND delta = '%s'", $edit['visibility'], $edit['pages'], $edit['custom'], $module, $delta);
module_invoke($module, 'block', 'save', $delta, $edit);
drupal_set_message(t('The block configuration has been saved.'));
cache_clear_all();
drupal_goto('admin/block');
default: $edit = db_fetch_array(db_query("SELECT pages, visibility, custom FROM {blocks} WHERE module = '%s' AND delta = '%s'", $module, $delta));
// Always evaluates to TRUE, but a validation step may be added later.
if (!$edit) {
$edit = db_fetch_array(db_query("SELECT pages, visibility, custom FROM {blocks} WHERE module = '%s' AND delta = '%s'", $module, $delta));
}
// Module-specific block configurations. // Module-specific block configurations.
if ($settings = module_invoke($module, 'block', 'configure', $delta)) { if ($settings = module_invoke($module, 'block', 'configure', $delta)) {
$form['block_settings'] = array('#type' => 'fieldset', $form['block_settings'] = array(
'#title' => t('Block specific settings'), '#type' => 'fieldset',
'#collapsible' => true, '#title' => t('Block specific settings'),
'#weight' => 0); '#collapsible' => true,
'#weight' => 0,
);
foreach ($settings as $k => $v) { foreach ($settings as $k => $v) {
$form['block_settings'][$k] = $v; $form['block_settings'][$k] = $v;
} }
} }
// Get the block subject for the page title. // Get the block subject for the page title.
$info = module_invoke($module, 'block', 'list'); $info = module_invoke($module, 'block', 'list');
drupal_set_title(t("'%name' block", array('%name' => $info[$delta]['info']))); drupal_set_title(t("'%name' block", array('%name' => $info[$delta]['info'])));
// Standard block configurations. // Standard block configurations.
$form['user_vis_settings'] = array('#type' => 'fieldset', $form['user_vis_settings'] = array(
'#title' => t('User specific visibility settings'), '#type' => 'fieldset',
'#collapsible' => true, '#title' => t('User specific visibility settings'),
'#weight' => 0); '#collapsible' => true,
'#weight' => 0,
);
$form['user_vis_settings']['custom'] = array(
'#type' => 'radios',
'#title' => t('Custom visibility settings'),
'#options' => array(t('Users cannot control whether or not they see this block.'), t('Show this block by default, but let individual users hide it.'), t('Hide this block by default but let individual users show it.'), t('Allow individual users to customize the visibility of this block in their account settings.')),
'#default_value' => $edit['custom'],
);
$form['page_vis_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Page specific visibility settings'),
'#collapsible' => true,
'#weight' => 0,
);
$form['page_vis_settings']['visibility'] = array(
'#type' => 'radios',
'#title' => t('Show block on specific pages'),
'#options' => array(t('Show on every page except the listed pages.'), t('Show on only the listed pages.'), t('Show if the following PHP code returns <code>TRUE</code> (PHP-mode, experts only).')),
'#default_value' => $edit['visibility'],
);
$form['page_vis_settings']['pages'] = array(
'#type' => 'textarea',
'#title' => t('Pages'),
'#default_value' => $edit['pages'],
'#description' => t("Enter one page per line as Drupal paths. The '*' character is a wildcard. Example paths are '%blog' for the blog page and %blog-wildcard for every personal blog. %front is the front page. If the PHP-mode is chosen, enter PHP code between %php. Note that executing incorrect PHP-code can break your Drupal site.", array('%blog' => theme('placeholder', 'blog'), '%blog-wildcard' => theme('placeholder', 'blog/*'), '%front' => theme('placeholder', '<front>'), '%php' => theme('placeholder', '<?php ?>'))),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save block'),
);
$form['user_vis_settings']['custom'] = array( return drupal_get_form('block_admin_configure', $form);
'#type' => 'radios', }
'#title' => t('Custom visibility settings'),
'#default_value' => $edit['custom'],
'#options' => array(t('Users cannot control whether or not they see this block.'), t('Show this block by default, but let individual users hide it.'), t('Hide this block by default but let individual users show it.'), t('Allow individual users to customize the visibility of this block in their account settings.')),
'#default_value' => $edit['custom']);
function block_admin_configure_validate($form_id, $form_values) {
if (empty($form_values['info']) || db_num_rows(db_query("SELECT bid FROM {boxes} WHERE bid != %d AND info = '%s'", $form_values['delta'], $form_values['info']))) {
form_set_error('info', t('Please ensure that each block description is unique.'));
}
}
$form['page_vis_settings'] = array('#type' => 'fieldset', function block_admin_configure_execute($form_id, $form_values) {
'#title' => t('Page specific visibility settings'), if (!form_get_errors()) {
'#collapsible' => true, db_query("UPDATE {blocks} SET visibility = %d, pages = '%s', custom = %d WHERE module = '%s' AND delta = '%s'", $form_values['visibility'], $form_values['pages'], $form_values['custom'], $form_values['module'], $form_values['delta']);
'#weight' => 0); module_invoke($form_values['module'], 'block', 'save', $form_values['delta'], $form_values);
drupal_set_message(t('The block configuration has been saved.'));
cache_clear_all();
$form['page_vis_settings']['visibility'] = array( drupal_goto('admin/block');
'#type' => 'radios',
'#title' => t('Show block on specific pages'),
'#default_value' => $edit['visibility'],
'#options' => array(t('Show on every page except the listed pages.'), t('Show on only the listed pages.'), t('Show if the following PHP code returns <code>TRUE</code> (PHP-mode, experts only).')),
'#default_value' => $edit['visibility']);
$form['page_vis_settings']['pages'] = array(
'#type' => 'textarea',
'#title' => t('Pages'),
'#default_value' => $edit['pages'],
'#description' => t("Enter one page per line as Drupal paths. The '*' character is a wildcard. Example paths are '%blog' for the blog page and %blog1 for every personal blog. %front is the front page. If the PHP-mode is chosen, enter PHP code between %php. Note that executing incorrect PHP-code can break your Drupal site.", array('%blog' => theme('placeholder', 'blog'), '%blog1' => theme('placeholder', 'blog/*'), '%front' => theme('placeholder', '<front>'), '%php' => theme('placeholder', '<?php ?>'))));
$form['submit'] = array('#type' => 'submit', '#value' => t('Save block'));
return drupal_get_form('block_config', $form);
} }
} }
@ -389,24 +399,27 @@ function block_admin_configure($module = NULL, $delta = 0) {
* Menu callback; displays the block creation form. * Menu callback; displays the block creation form.
*/ */
function block_box_add() { function block_box_add() {
$edit = $_POST['edit']; $form = block_box_form();
$op = $_POST['op']; $form['submit'] = array('#type' => 'submit', '#value' => t('Save block'));
switch ($op) {
case t('Save block'):
if (block_box_save($edit)) {
drupal_set_message(t('The block has been created.'));
drupal_goto('admin/block');
}
// deliberate no break
default:
$form = block_box_form($edit);
$form['submit'] = array('#type' => 'submit', '#value' => t('Save block'));
}
return drupal_get_form('block_box_add', $form); return drupal_get_form('block_box_add', $form);
} }
function block_box_add_validate($form_id, $form_values) {
if (empty($form_values['info']) || db_num_rows(db_query("SELECT info FROM {boxes} WHERE info = '%s'", $form_values['info']))) {
form_set_error('info', t('Please ensure that each block description is unique.'));
}
}
function block_box_add_execute($form_id, $form_values) {
if (!form_get_errors()) {
if (block_box_save($form_values)) {
drupal_set_message(t('The block has been created.'));
drupal_goto('admin/block');
}
}
}
/** /**
* Menu callback; confirm deletion of custom blocks. * Menu callback; confirm deletion of custom blocks.
*/ */
@ -421,10 +434,9 @@ function block_box_delete($bid = 0) {
/** /**
* Deletion of custom blocks. * Deletion of custom blocks.
*/ */
function block_box_delete_confirm_execute($form_id, $edit) { function block_box_delete_confirm_execute($form_id, $form_values) {
$form = $GLOBALS['form_values']; db_query('DELETE FROM {boxes} WHERE bid = %d', $form_values['bid']);
db_query('DELETE FROM {boxes} WHERE bid = %d', $form['bid']); drupal_set_message(t('The block %name has been removed.', array('%name' => theme('placeholder', $form_values['info']))));
drupal_set_message(t('The block %name has been removed.', array('%name' => theme('placeholder', $form['info']))));
cache_clear_all(); cache_clear_all();
drupal_goto('admin/block'); drupal_goto('admin/block');
}; };
@ -448,10 +460,6 @@ function block_box_save($edit, $delta = NULL) {
db_query("UPDATE {boxes} SET title = '%s', body = '%s', info = '%s', format = %d WHERE bid = %d", $edit['title'], $edit['body'], $edit['info'], $edit['format'], $delta); db_query("UPDATE {boxes} SET title = '%s', body = '%s', info = '%s', format = %d WHERE bid = %d", $edit['title'], $edit['body'], $edit['info'], $edit['format'], $delta);
} }
else { else {
if (empty($edit['info']) || db_num_rows(db_query("SELECT info FROM {boxes} WHERE info = '%s'", $edit['info']))) {
form_set_error('info', t('Please ensure that each block description is unique.'));
return false;
}
db_query("INSERT INTO {boxes} (title, body, info, format) VALUES ('%s', '%s', '%s', %d)", $edit['title'], $edit['body'], $edit['info'], $edit['format']); db_query("INSERT INTO {boxes} (title, body, info, format) VALUES ('%s', '%s', '%s', %d)", $edit['title'], $edit['body'], $edit['info'], $edit['format']);
} }
return true; return true;

View File

@ -212,9 +212,18 @@ function _block_rehash($order_by = array('weight')) {
* Prepare the main block administration form. * Prepare the main block administration form.
*/ */
function block_admin_display() { function block_admin_display() {
global $theme_key; global $theme_key, $custom_theme;
$throttle = module_exist('throttle'); $throttle = module_exist('throttle');
// If non-default theme configuration has been selected, set the custom theme.
if (arg(3)) {
$custom_theme = arg(3);
}
else {
$custom_theme = variable_get('theme_default', 'bluemarine');
}
init_theme();
$blocks = _block_rehash(); $blocks = _block_rehash();
$block_regions = system_region_list($theme_key); $block_regions = system_region_list($theme_key);
@ -241,15 +250,9 @@ function block_admin_display() {
} }
function theme_block_admin_display($form) { function theme_block_admin_display($form) {
global $theme_key;
global $theme_key, $custom_theme;
$throttle = module_exist('throttle'); $throttle = module_exist('throttle');
// If non-default theme configuration has been selected, set the custom theme.
if (arg(3)) {
$custom_theme = arg(3);
init_theme();
}
$block_regions = system_region_list($theme_key); $block_regions = system_region_list($theme_key);
// Highlight regions on page, to provide visual reference. // Highlight regions on page, to provide visual reference.
@ -312,76 +315,83 @@ function block_box_get($bid) {
* Menu callback; displays the block configuration form. * Menu callback; displays the block configuration form.
*/ */
function block_admin_configure($module = NULL, $delta = 0) { function block_admin_configure($module = NULL, $delta = 0) {
$edit = $_POST['edit'];
$op = $_POST['op'];
switch ($op) { $form['module'] = array('#type' => 'value', '#value' => $module);
case t('Save block'): $form['delta'] = array('#type' => 'value', '#value' => $delta);
db_query("UPDATE {blocks} SET visibility = %d, pages = '%s', custom = %d WHERE module = '%s' AND delta = '%s'", $edit['visibility'], $edit['pages'], $edit['custom'], $module, $delta);
module_invoke($module, 'block', 'save', $delta, $edit);
drupal_set_message(t('The block configuration has been saved.'));
cache_clear_all();
drupal_goto('admin/block');
default: $edit = db_fetch_array(db_query("SELECT pages, visibility, custom FROM {blocks} WHERE module = '%s' AND delta = '%s'", $module, $delta));
// Always evaluates to TRUE, but a validation step may be added later.
if (!$edit) {
$edit = db_fetch_array(db_query("SELECT pages, visibility, custom FROM {blocks} WHERE module = '%s' AND delta = '%s'", $module, $delta));
}
// Module-specific block configurations. // Module-specific block configurations.
if ($settings = module_invoke($module, 'block', 'configure', $delta)) { if ($settings = module_invoke($module, 'block', 'configure', $delta)) {
$form['block_settings'] = array('#type' => 'fieldset', $form['block_settings'] = array(
'#title' => t('Block specific settings'), '#type' => 'fieldset',
'#collapsible' => true, '#title' => t('Block specific settings'),
'#weight' => 0); '#collapsible' => true,
'#weight' => 0,
);
foreach ($settings as $k => $v) { foreach ($settings as $k => $v) {
$form['block_settings'][$k] = $v; $form['block_settings'][$k] = $v;
} }
} }
// Get the block subject for the page title. // Get the block subject for the page title.
$info = module_invoke($module, 'block', 'list'); $info = module_invoke($module, 'block', 'list');
drupal_set_title(t("'%name' block", array('%name' => $info[$delta]['info']))); drupal_set_title(t("'%name' block", array('%name' => $info[$delta]['info'])));
// Standard block configurations. // Standard block configurations.
$form['user_vis_settings'] = array('#type' => 'fieldset', $form['user_vis_settings'] = array(
'#title' => t('User specific visibility settings'), '#type' => 'fieldset',
'#collapsible' => true, '#title' => t('User specific visibility settings'),
'#weight' => 0); '#collapsible' => true,
'#weight' => 0,
);
$form['user_vis_settings']['custom'] = array(
'#type' => 'radios',
'#title' => t('Custom visibility settings'),
'#options' => array(t('Users cannot control whether or not they see this block.'), t('Show this block by default, but let individual users hide it.'), t('Hide this block by default but let individual users show it.'), t('Allow individual users to customize the visibility of this block in their account settings.')),
'#default_value' => $edit['custom'],
);
$form['page_vis_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Page specific visibility settings'),
'#collapsible' => true,
'#weight' => 0,
);
$form['page_vis_settings']['visibility'] = array(
'#type' => 'radios',
'#title' => t('Show block on specific pages'),
'#options' => array(t('Show on every page except the listed pages.'), t('Show on only the listed pages.'), t('Show if the following PHP code returns <code>TRUE</code> (PHP-mode, experts only).')),
'#default_value' => $edit['visibility'],
);
$form['page_vis_settings']['pages'] = array(
'#type' => 'textarea',
'#title' => t('Pages'),
'#default_value' => $edit['pages'],
'#description' => t("Enter one page per line as Drupal paths. The '*' character is a wildcard. Example paths are '%blog' for the blog page and %blog-wildcard for every personal blog. %front is the front page. If the PHP-mode is chosen, enter PHP code between %php. Note that executing incorrect PHP-code can break your Drupal site.", array('%blog' => theme('placeholder', 'blog'), '%blog-wildcard' => theme('placeholder', 'blog/*'), '%front' => theme('placeholder', '<front>'), '%php' => theme('placeholder', '<?php ?>'))),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save block'),
);
$form['user_vis_settings']['custom'] = array( return drupal_get_form('block_admin_configure', $form);
'#type' => 'radios', }
'#title' => t('Custom visibility settings'),
'#default_value' => $edit['custom'],
'#options' => array(t('Users cannot control whether or not they see this block.'), t('Show this block by default, but let individual users hide it.'), t('Hide this block by default but let individual users show it.'), t('Allow individual users to customize the visibility of this block in their account settings.')),
'#default_value' => $edit['custom']);
function block_admin_configure_validate($form_id, $form_values) {
if (empty($form_values['info']) || db_num_rows(db_query("SELECT bid FROM {boxes} WHERE bid != %d AND info = '%s'", $form_values['delta'], $form_values['info']))) {
form_set_error('info', t('Please ensure that each block description is unique.'));
}
}
$form['page_vis_settings'] = array('#type' => 'fieldset', function block_admin_configure_execute($form_id, $form_values) {
'#title' => t('Page specific visibility settings'), if (!form_get_errors()) {
'#collapsible' => true, db_query("UPDATE {blocks} SET visibility = %d, pages = '%s', custom = %d WHERE module = '%s' AND delta = '%s'", $form_values['visibility'], $form_values['pages'], $form_values['custom'], $form_values['module'], $form_values['delta']);
'#weight' => 0); module_invoke($form_values['module'], 'block', 'save', $form_values['delta'], $form_values);
drupal_set_message(t('The block configuration has been saved.'));
cache_clear_all();
$form['page_vis_settings']['visibility'] = array( drupal_goto('admin/block');
'#type' => 'radios',
'#title' => t('Show block on specific pages'),
'#default_value' => $edit['visibility'],
'#options' => array(t('Show on every page except the listed pages.'), t('Show on only the listed pages.'), t('Show if the following PHP code returns <code>TRUE</code> (PHP-mode, experts only).')),
'#default_value' => $edit['visibility']);
$form['page_vis_settings']['pages'] = array(
'#type' => 'textarea',
'#title' => t('Pages'),
'#default_value' => $edit['pages'],
'#description' => t("Enter one page per line as Drupal paths. The '*' character is a wildcard. Example paths are '%blog' for the blog page and %blog1 for every personal blog. %front is the front page. If the PHP-mode is chosen, enter PHP code between %php. Note that executing incorrect PHP-code can break your Drupal site.", array('%blog' => theme('placeholder', 'blog'), '%blog1' => theme('placeholder', 'blog/*'), '%front' => theme('placeholder', '<front>'), '%php' => theme('placeholder', '<?php ?>'))));
$form['submit'] = array('#type' => 'submit', '#value' => t('Save block'));
return drupal_get_form('block_config', $form);
} }
} }
@ -389,24 +399,27 @@ function block_admin_configure($module = NULL, $delta = 0) {
* Menu callback; displays the block creation form. * Menu callback; displays the block creation form.
*/ */
function block_box_add() { function block_box_add() {
$edit = $_POST['edit']; $form = block_box_form();
$op = $_POST['op']; $form['submit'] = array('#type' => 'submit', '#value' => t('Save block'));
switch ($op) {
case t('Save block'):
if (block_box_save($edit)) {
drupal_set_message(t('The block has been created.'));
drupal_goto('admin/block');
}
// deliberate no break
default:
$form = block_box_form($edit);
$form['submit'] = array('#type' => 'submit', '#value' => t('Save block'));
}
return drupal_get_form('block_box_add', $form); return drupal_get_form('block_box_add', $form);
} }
function block_box_add_validate($form_id, $form_values) {
if (empty($form_values['info']) || db_num_rows(db_query("SELECT info FROM {boxes} WHERE info = '%s'", $form_values['info']))) {
form_set_error('info', t('Please ensure that each block description is unique.'));
}
}
function block_box_add_execute($form_id, $form_values) {
if (!form_get_errors()) {
if (block_box_save($form_values)) {
drupal_set_message(t('The block has been created.'));
drupal_goto('admin/block');
}
}
}
/** /**
* Menu callback; confirm deletion of custom blocks. * Menu callback; confirm deletion of custom blocks.
*/ */
@ -421,10 +434,9 @@ function block_box_delete($bid = 0) {
/** /**
* Deletion of custom blocks. * Deletion of custom blocks.
*/ */
function block_box_delete_confirm_execute($form_id, $edit) { function block_box_delete_confirm_execute($form_id, $form_values) {
$form = $GLOBALS['form_values']; db_query('DELETE FROM {boxes} WHERE bid = %d', $form_values['bid']);
db_query('DELETE FROM {boxes} WHERE bid = %d', $form['bid']); drupal_set_message(t('The block %name has been removed.', array('%name' => theme('placeholder', $form_values['info']))));
drupal_set_message(t('The block %name has been removed.', array('%name' => theme('placeholder', $form['info']))));
cache_clear_all(); cache_clear_all();
drupal_goto('admin/block'); drupal_goto('admin/block');
}; };
@ -448,10 +460,6 @@ function block_box_save($edit, $delta = NULL) {
db_query("UPDATE {boxes} SET title = '%s', body = '%s', info = '%s', format = %d WHERE bid = %d", $edit['title'], $edit['body'], $edit['info'], $edit['format'], $delta); db_query("UPDATE {boxes} SET title = '%s', body = '%s', info = '%s', format = %d WHERE bid = %d", $edit['title'], $edit['body'], $edit['info'], $edit['format'], $delta);
} }
else { else {
if (empty($edit['info']) || db_num_rows(db_query("SELECT info FROM {boxes} WHERE info = '%s'", $edit['info']))) {
form_set_error('info', t('Please ensure that each block description is unique.'));
return false;
}
db_query("INSERT INTO {boxes} (title, body, info, format) VALUES ('%s', '%s', '%s', %d)", $edit['title'], $edit['body'], $edit['info'], $edit['format']); db_query("INSERT INTO {boxes} (title, body, info, format) VALUES ('%s', '%s', '%s', %d)", $edit['title'], $edit['body'], $edit['info'], $edit['format']);
} }
return true; return true;