Commit ebc7ef4b authored by davereid's avatar davereid Committed by Dave Reid

Issue #2419725 by Dave Reid: Refactored menu_block_get_config().

parent d9760209
......@@ -83,9 +83,9 @@ function menu_block_add_block_form_submit($form, &$form_state) {
* 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) {
$blocks = module_invoke_all('menu_block_blocks');
foreach (variable_get('menu_block_ids', array()) AS $delta) {
if (empty($blocks[$delta])) {
$exported = menu_block_get_exported_blocks();
foreach (variable_get('menu_block_ids', array()) as $delta) {
if (!isset($exported[$delta])) {
$form['blocks']['menu_block_' . $delta]['delete'] = array('#type' => 'link', '#title' => t('delete'), '#href' => 'admin/structure/block/delete-menu-block/' . $delta);
}
}
......@@ -133,10 +133,9 @@ function menu_block_delete_form_submit($form, &$form_state) {
function _menu_block_block_info() {
$blocks = array();
$deltas = variable_get('menu_block_ids', array());
foreach (array_keys(module_invoke_all('menu_block_blocks')) as $delta) {
$deltas[] = $delta;
}
foreach ($deltas AS $delta) {
$exported = menu_block_get_exported_blocks();
$deltas = array_unique(array_merge($deltas, array_keys($exported)));
foreach ($deltas 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.
......@@ -231,7 +230,7 @@ function menu_block_configure_form($form, &$form_state) {
}
}
// Merge in the default configuration.
$config += menu_block_get_config();
$config += menu_block_default_config();
// Don't display the config form if this delta is exported to code.
if (!empty($config['exported_to_code'])) {
......
......@@ -9,15 +9,12 @@
*/
function menu_block_uninstall() {
// Delete menu block variables.
drupal_load('module', 'menu_block');
$variable_keys = array_keys(menu_block_default_config());
foreach (variable_get('menu_block_ids', array()) as $delta) {
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");
foreach ($variable_keys as $key) {
variable_del("menu_block_{$delta}_{$key}");
}
}
variable_del('menu_block_ids');
variable_del('menu_block_suppress_core');
......
......@@ -229,58 +229,107 @@ function menu_block_get_all_menus() {
return $all_menus;
}
/**
* The default menu block configuration.
*
* @return array
*/
function menu_block_default_config() {
return array(
'parent' => 'main-menu:0',
'title_link' => 0,
'admin_title' => '',
'level' => 1,
'follow' => 0,
'depth' => 0,
'expanded' => 0,
'sort' => 0,
);
}
/**
* Fetch all exported menu blocks.
*
* @return array
*/
function menu_block_get_exported_blocks() {
$blocks = &drupal_static(__FUNCTION__);
if (!isset($blocks)) {
$blocks = array();
// Do not use module_invoke_all() since it rewrites numeric indexes.
// Although exported menu blocks should not be using numeric IDs, we
// should still prevent them from being changed.
foreach (module_implements('menu_block_blocks') as $module) {
$blocks += module_invoke($module, 'menu_block_blocks');
}
}
return $blocks;
}
/**
* Returns the configuration for the requested block delta.
*
* @param string $delta
* The delta that uniquely identifies the block in the block system. If
* not specified, the default configuration will be returned.
* This is deprecated. Use menu_block_default_config() instead.
*
* @return array
* An associated array of configuration options.
*
* @see menu_block_default_config()
*/
function menu_block_get_config($delta = NULL) {
$config = array(
'delta' => $delta,
'menu_name' => 'main-menu',
'parent_mlid' => 0,
'parent' => '',
'title_link' => 0,
'admin_title' => '',
'level' => 1,
'follow' => 0,
'depth' => 0,
'expanded' => 0,
'sort' => 0,
);
static $defaults, $exported;
if (!isset($defaults)) {
$defaults = menu_block_default_config();
}
if (!isset($delta)) {
return $defaults;
}
if (!isset($exported)) {
$exported = menu_block_get_exported_blocks();
}
$configs = &drupal_static(__FUNCTION__, array());
if (!isset($configs[$delta])) {
$config = array();
// Get the block configuration options.
if ($delta) {
static $blocks;
if (!isset($blocks)) {
$blocks = module_invoke_all('menu_block_blocks');
}
if (!empty($blocks[$delta])) {
// Merge the default values.
$config = $blocks[$delta] + $config;
// Set the delta.
$config['delta'] = $delta;
// Flag the block as exported.
// Check if this an exported menu block.
if (isset($exported[$delta])) {
$config += $exported[$delta];
$config['exported_to_code'] = TRUE;
// Exported blocks generally have 'menu_name' and 'parent_mlid' defined
// but not 'parent'
if (!isset($config['parent'])) {
$config['parent'] = $config['menu_name'] . ':' . $config['parent_mlid'];
}
}
// Add in variable overrides and defaults.
foreach ($defaults as $key => $default) {
$override = variable_get("menu_block_{$delta}_{$key}");
if (isset($override)) {
$config[$key] = $override;
}
elseif (!isset($config[$key])) {
$config[$key] = $default;
}
}
// Split out the 'parent' item into 'menu_name' and 'parent_mlid'.
if (!isset($config['menu_name']) && !isset($config['parent_mlid'])) {
list($config['menu_name'], $config['parent_mlid']) = explode(':', $config['parent']);
}
$config['title_link'] = variable_get("menu_block_{$delta}_title_link", $config['title_link']);
$config['admin_title'] = variable_get("menu_block_{$delta}_admin_title", $config['admin_title']);
$config['level'] = variable_get("menu_block_{$delta}_level", $config['level']);
$config['follow'] = variable_get("menu_block_{$delta}_follow", $config['follow']);
$config['depth'] = variable_get("menu_block_{$delta}_depth", $config['depth']);
$config['expanded'] = variable_get("menu_block_{$delta}_expanded", $config['expanded']);
$config['sort'] = variable_get("menu_block_{$delta}_sort", $config['sort']);
$config['parent'] = variable_get("menu_block_{$delta}_parent", $config['menu_name'] . ':' . $config['parent_mlid']);
list($config['menu_name'], $config['parent_mlid']) = explode(':', $config['parent']);
$config['delta'] = $delta;
$configs[$delta] = $config;
}
return $config;
return $configs[$delta];
}
/**
......@@ -918,14 +967,10 @@ function menu_block_delete($delta) {
variable_set('menu_block_ids', $block_ids);
// Remove all the individual variables.
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");
$variable_keys = array_keys(menu_block_default_config());
foreach ($variable_keys as $key) {
variable_del("menu_block_{$delta}_{$key}");
}
}
}
......
......@@ -75,8 +75,7 @@ function menu_block_export_form_submit(&$form, &$form_state) {
$output .= <<<END_OF_CONFIG
'$id' => array(
'menu_name' => {$config['menu_name']},
'parent_mlid' => {$config['parent_mlid']},
'parent' => {$config['parent']},
'title_link' => {$config['title_link']},
'admin_title' => '{$config['admin_title']}',
'level' => {$config['level']},
......
......@@ -36,7 +36,6 @@ function menu_block_menu_tree_content_type_content_types() {
'js' => array(drupal_get_path('module', 'menu_block') . '/menu-block.js'),
'css' => array(drupal_get_path('module', 'menu_block') . '/menu-block-admin.css'),
);
unset($common_plugin['defaults']['delta']);
$menus = menu_block_get_all_menus();
$items = array();
......
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