View source
<?php
function menu_features_api() {
return array(
'menu_custom' => array(
'name' => t('Menus'),
'default_hook' => 'menu_default_menu_custom',
'feature_source' => TRUE,
'default_file' => FEATURES_DEFAULTS_INCLUDED,
),
'menu_links' => array(
'name' => t('Menu links'),
'default_hook' => 'menu_default_menu_links',
'feature_source' => TRUE,
'default_file' => FEATURES_DEFAULTS_INCLUDED,
),
'menu' => array(
'name' => t('Menu items'),
'default_hook' => 'menu_default_items',
'default_file' => FEATURES_DEFAULTS_INCLUDED,
'feature_source' => FALSE,
),
);
}
function menu_features_export($data, &$export, $module_name = '') {
$pipe = array();
foreach ($data as $path) {
$pipe['menu_links'][] = "features:{$path}";
}
return $pipe;
}
function menu_custom_features_export_options() {
$options = array();
$result = db_query("SELECT * FROM {menu_custom} ORDER BY title", array(), array(
'fetch' => PDO::FETCH_ASSOC,
));
foreach ($result as $menu) {
$options[$menu['menu_name']] = $menu['title'];
}
return $options;
}
function menu_custom_features_export($data, &$export, $module_name = '') {
$export['dependencies']['features'] = 'features';
$export['dependencies']['menu'] = 'menu';
$pipe = array();
$map = features_get_default_map('menu_custom', 'menu_name');
foreach ($data as $menu_name) {
if (isset($map[$menu_name]) && $map[$menu_name] != $module_name) {
$export['dependencies'][$map[$menu_name]] = $map[$menu_name];
}
else {
$export['features']['menu_custom'][$menu_name] = $menu_name;
}
}
return $pipe;
}
function menu_custom_features_export_render($module, $data) {
$code = array();
$code[] = ' $menus = array();';
$code[] = '';
$translatables = array();
foreach ($data as $menu_name) {
$row = db_select('menu_custom')
->fields('menu_custom')
->condition('menu_name', $menu_name)
->execute()
->fetchAssoc();
if ($row) {
$export = features_var_export($row, ' ');
$code[] = " // Exported menu: {$menu_name}.";
$code[] = " \$menus['{$menu_name}'] = {$export};";
$translatables[] = $row['title'];
$translatables[] = $row['description'];
}
}
if (!empty($translatables)) {
$code[] = features_translatables_export($translatables, ' ');
}
$code[] = '';
$code[] = ' return $menus;';
$code = implode("\n", $code);
return array(
'menu_default_menu_custom' => $code,
);
}
function menu_custom_features_revert($module) {
menu_custom_features_rebuild($module);
}
function menu_custom_features_rebuild($module) {
if ($defaults = features_get_default('menu_custom', $module)) {
foreach ($defaults as $menu) {
menu_save($menu);
}
}
}
function menu_links_features_export_options() {
global $menu_admin;
$menu_admin = TRUE;
$menu_links = menu_parent_options(menu_get_menus(), array(
'mlid' => 0,
));
$options = array();
foreach ($menu_links as $key => $name) {
list($menu_name, $mlid) = explode(':', $key, 2);
if ($mlid != 0) {
$link = menu_link_load($mlid);
$identifier = menu_links_features_identifier($link);
$options[$identifier] = "{$menu_name}: {$name}";
}
}
$menu_admin = FALSE;
return $options;
}
function menu_links_features_identifier($link) {
return isset($link['menu_name'], $link['link_path']) ? "{$link['menu_name']}:{$link['link_path']}" : FALSE;
}
function menu_links_features_export($data, &$export, $module_name = '') {
$export['dependencies']['features'] = 'features';
$export['dependencies']['menu'] = 'menu';
$pipe = array();
$map = features_get_default_map('menu_links', 'menu_links_features_identifier');
foreach ($data as $identifier) {
if ($link = features_menu_link_load($identifier)) {
if (isset($map[$identifier]) && $map[$identifier] != $module_name) {
$export['dependencies'][$map[$identifier]] = $map[$identifier];
}
else {
$export['features']['menu_links'][$identifier] = $identifier;
}
if (!in_array($link['menu_name'], array(
'features',
'primary-links',
'secondary-links',
'navigation',
'admin',
'devel',
))) {
$pipe['menu_custom'][] = $link['menu_name'];
}
}
}
return $pipe;
}
function menu_links_features_export_render($module, $data) {
$code = array();
$code[] = ' $menu_links = array();';
$code[] = '';
$translatables = array();
foreach ($data as $identifier) {
if ($link = features_menu_link_load($identifier)) {
if (!empty($link['plid']) && ($parent = menu_link_load($link['plid']))) {
$link['parent_path'] = $parent['link_path'];
}
unset($link['plid']);
unset($link['mlid']);
$code[] = " // Exported menu link: {$identifier}";
$code[] = " \$menu_links['{$identifier}'] = " . features_var_export($link, ' ') . ";";
$translatables[] = $link['link_title'];
}
}
if (!empty($translatables)) {
$code[] = features_translatables_export($translatables, ' ');
}
$code[] = '';
$code[] = ' return $menu_links;';
$code = implode("\n", $code);
return array(
'menu_default_menu_links' => $code,
);
}
function menu_links_features_revert($module) {
menu_links_features_rebuild($module);
}
function menu_links_features_rebuild($module) {
if ($menu_links = features_get_default('menu_links', $module)) {
menu_links_features_rebuild_ordered($menu_links);
}
}
function menu_links_features_rebuild_ordered($menu_links, $reset = FALSE) {
static $ordered;
static $all_links;
if (!isset($ordered) || $reset) {
$ordered = array();
$unordered = features_get_default('menu_links');
if ($unordered) {
do {
$current = count($unordered);
foreach ($unordered as $key => $link) {
$identifier = menu_links_features_identifier($link);
$parent = isset($link['parent_path']) ? "{$link['menu_name']}:{$link['parent_path']}" : '';
if (empty($parent)) {
$ordered[$identifier] = 0;
$all_links[$identifier] = $link;
unset($unordered[$key]);
}
elseif (isset($ordered[$parent])) {
$ordered[$identifier] = $ordered[$parent] + 1;
$all_links[$identifier] = $link;
unset($unordered[$key]);
}
}
} while (count($unordered) < $current);
}
asort($ordered);
}
foreach (array_keys($ordered) as $identifier) {
$link = $all_links[$identifier];
$existing = features_menu_link_load($identifier);
if (!$existing || in_array($link, $menu_links)) {
if ($existing) {
$link['mlid'] = $existing['mlid'];
}
if (!empty($link['parent_path']) && ($parent = features_menu_link_load("{$link['menu_name']}:{$link['parent_path']}"))) {
$link['plid'] = $parent['mlid'];
}
else {
$link['plid'] = 0;
}
menu_link_save($link);
}
}
}
function features_menu_link_load($identifier) {
list($menu_name, $link_path) = explode(':', $identifier, 2);
$link = db_select('menu_links')
->fields('menu_links', array(
'menu_name',
'mlid',
'plid',
'link_path',
'router_path',
'link_title',
'options',
'module',
'hidden',
'external',
'has_children',
'expanded',
'weight',
))
->condition('menu_name', $menu_name)
->condition('link_path', $link_path)
->execute()
->fetchAssoc();
if ($link) {
$link['options'] = unserialize($link['options']);
return $link;
}
return FALSE;
}