$theme) { if ($theme->status) { db_query("INSERT INTO {blocks} (visibility, pages, custom, title, module, theme, status, weight, delta, cache) VALUES(%d, '%s', %d, '%s', '%s', '%s', %d, %d, %d, %d)", $form_state['values']['visibility'], trim($form_state['values']['pages']), $form_state['values']['custom'], $form_state['values']['title'], $form_state['values']['module'], $theme->name, 0, 0, $delta, BLOCK_NO_CACHE); } } foreach (array_filter($form_state['values']['roles']) as $rid) { db_query("INSERT INTO {blocks_roles} (rid, module, delta) VALUES (%d, '%s', '%s')", $rid, $form_state['values']['module'], $delta); } drupal_set_message(t('The block has been created.')); cache_clear_all(); $form_state['redirect'] = 'admin/build/block'; return; } /** * Alters the block admin form to add delete links next to menu blocks. */ function _menu_block_form_block_admin_display_form_alter(&$form, $form_state) { foreach (variable_get('menu_block_ids', array()) AS $delta) { $form['menu_block_' . $delta]['delete'] = array('#value' => l(t('delete'), 'admin/build/block/delete-menu-block/'. $delta)); } } /** * Menu callback: confirm deletion of menu blocks. */ function menu_block_delete(&$form_state, $delta = 0) { $title = _menu_block_format_title(menu_block_get_config($delta)); $form['block_title'] = array('#type' => 'hidden', '#value' => $title); $form['delta'] = array('#type' => 'hidden', '#value' => $delta); return confirm_form($form, t('Are you sure you want to delete the "%name" block?', array('%name' => $title)), 'admin/build/block', NULL, t('Delete'), t('Cancel')); } /** * Deletion of menu blocks. */ function menu_block_delete_submit($form, &$form_state) { // Remove the menu block configuration variables. $delta = $form_state['values']['delta']; $block_ids = variable_get('menu_block_ids', array()); unset($block_ids[array_search($delta, $block_ids)]); sort($block_ids); variable_set('menu_block_ids', $block_ids); variable_del("menu_block_{$delta}_title_link"); variable_del("menu_block_{$delta}_admin_title"); variable_del("menu_block_{$delta}_parent"); variable_del("menu_block_{$delta}_level"); variable_del("menu_block_{$delta}_follow"); variable_del("menu_block_{$delta}_depth"); variable_del("menu_block_{$delta}_expanded"); variable_del("menu_block_{$delta}_sort"); db_query("DELETE FROM {blocks} WHERE module = 'menu_block' AND delta = %d", $delta); db_query("DELETE FROM {blocks_roles} WHERE module = 'menu_block' AND delta = %d", $delta); drupal_set_message(t('The "%name" block has been removed.', array('%name' => $form_state['values']['block_title']))); cache_clear_all(); $form_state['redirect'] = 'admin/build/block'; return; } /** * Returns the 'list' $op info for hook_block(). */ function _menu_block_block_list() { $blocks = array(); foreach (variable_get('menu_block_ids', array()) AS $delta) { $blocks[$delta]['info'] = _menu_block_format_title(menu_block_get_config($delta)); // Menu blocks can't be cached because each menu item can have // a custom access callback. menu.inc manages its own caching. $blocks[$delta]['cache'] = BLOCK_NO_CACHE; } return $blocks; } /** * Return the title of the block. * * @param $config * array The configuration of the menu block. * @return * string The title of the block. */ function _menu_block_format_title($config) { // If an administrative title is specified, use it. if (!empty($config['admin_title'])) { return check_plain($config['admin_title']); } $menus = menu_block_get_all_menus(); $menus[MENU_TREE__CURRENT_PAGE_MENU] = t('Current menu'); if (empty($config['menu_name']) || empty($menus[$config['menu_name']])) { $title = t('Unconfigured menu block'); } else { // Show the configured levels in the block info $replacements = array( '@menu_name' => $menus[$config['menu_name']], '@level1' => $config['level'], '@level2' => $config['level'] + $config['depth'] - 1, ); if ($config['parent_mlid']) { $parent_item = menu_link_load($config['parent_mlid']); $replacements['@menu_name'] = $parent_item['title']; } if ($config['follow']) { $title = t('@menu_name (active menu item)', $replacements); } elseif ($config['depth'] == 1) { $title = t('@menu_name (level @level1)', $replacements); } elseif ($config['depth']) { if ($config['expanded']) { $title = t('@menu_name (expanded levels @level1-@level2)', $replacements); } else { $title = t('@menu_name (levels @level1-@level2)', $replacements); } } else { if ($config['expanded']) { $title = t('@menu_name (expanded levels @level1+)', $replacements); } else { $title = t('@menu_name (levels @level1+)', $replacements); } } } return $title; } /** * Returns the 'configure' $op info for hook_block(). */ function _menu_block_block_configure($delta) { // Create a pseudo form state. $form_state = array('values' => menu_block_get_config($delta)); return menu_block_configure_form($form_state); } /** * Returns the configuration form for a menu tree. * * @param $form_state * array An associated array of configuration options should be present in the * 'values' key. If none are given, default configuration is assumed. * @return * array The form in Form API format. */ function menu_block_configure_form(&$form_state) { $config = array(); // Get the config from the form state. if (!empty($form_state['values'])) { $config = $form_state['values']; if (!empty($config['parent'])) { list($config['menu_name'], $config['parent_mlid']) = explode(':', $config['parent']); } } // Merge in the default configuration. $config += menu_block_get_config(); // Get the list of menus. $menus = menu_block_get_all_menus(); // Build the standard and jquery versions of the parent item options. $parent_options = $parent_options_js = array(); foreach ($menus AS $menu_name => $title) { if ($menu_name == MENU_TREE__CURRENT_PAGE_MENU) { $options = array($menu_name . ':0' => $title); $parent_options += $options; // For the js options, replace the root item option with better help text. $options[$menu_name . ':0'] = '<' . t('root of menu') . '>'; } else { // Retrieve the entire tree of each menu. $options = menu_parent_options(array($menu_name => $title), array('mlid' => 0)); $parent_options += $options; $options[$menu_name . ':0'] = '<' . t('root of @menu_name', array('@menu_name' => $title)) . '>'; } // Render the options as