View source
<?php
namespace HookUpdateDeployTools;
class Menus implements ImportInterface {
public static function import($menus) {
$t = get_t();
$completed = array();
$menus = (array) $menus;
$total_requested = count($menus);
try {
self::canImport();
$menu_feature_storage_uri = HudtInternal::getStoragePath('menu');
global $user;
$original_user = clone $user;
$temp_admin_user = user_load(1);
if (empty($temp_admin_user)) {
$temp_admin_user = clone $user;
$temp_admin_user->uid = 1;
}
$user = $temp_admin_user;
foreach ($menus as $mid => $menu_machine_name) {
$filename = "{$menu_machine_name}-export.txt";
$menu_uri = "{$menu_feature_storage_uri}{$filename}";
if (HudtInternal::canReadFile($filename, 'menu')) {
$options = array(
'link_to_content' => TRUE,
'remove_menu_items' => TRUE,
);
$results = menu_import_file($menu_uri, $menu_machine_name, $options);
$message = '@menu_machine_name: @deleted_menu_items links deleted.';
global $base_url;
$link = "{$base_url}/admin/structure/menu/manage/{$menu_machine_name}";
$vars = array(
'@deleted_menu_items' => $results['deleted_menu_items'],
'@menu_machine_name' => $menu_machine_name,
);
Message::make($message, $vars, WATCHDOG_INFO, 1, $link);
$total = $results['matched_nodes'] + $results['unknown_links'] + $results['external_links'];
$message = '@menu_machine_name: @total total menu items created consisting of:
@matched_nodes links with matching paths
@unknown_links links without matching paths
@external_links external links';
$vars = array(
'@total' => $total,
'@matched_nodes' => $results['matched_nodes'],
'@unknown_links' => $results['unknown_links'],
'@external_links' => $results['external_links'],
'@menu_machine_name' => $menu_machine_name,
);
Message::make($message, $vars, WATCHDOG_INFO, 1, $link);
$completed[$menu_machine_name] = $t('Imported');
if (!empty($results['error'])) {
$error = print_r($results['error'], TRUE);
$variables = array(
'@error' => $error,
'@menu_machine_name' => $menu_machine_name,
);
$message = "The requested menu import '@menu_machine_name' failed with the following errors @error. Adjust your @menu_machine_name-export.txt menu text file accordingly and re-run update.";
Message::make($message, $variables, WATCHDOG_ERROR, 1, $link);
throw new HudtException($message, $vars, WATCHDOG_ERROR, FALSE);
}
}
menu_cache_clear($menu_machine_name);
}
$user = $original_user;
} catch (\Exception $e) {
$user = $original_user;
$message = 'Menu import failed because: !error';
$variables = array(
'!error' => method_exists($e, 'logMessage') ? $e
->logMessage() : $e
->getMessage(),
);
if (!method_exists($e, 'logMessage')) {
Message::make($message, $vars, WATCHDOG_ERROR);
}
$done = $t("Menu imports NOT complete! \n");
$done .= HudtInternal::getSummary($completed, $total_requested, 'Imported');
Message::make($done, array(), FALSE, $indent);
throw new \DrupalUpdateException($t('Caught Exception: Update aborted! !error', $variables));
}
$done = $t('Menu imports complete');
$done .= HudtInternal::getSummary($completed, $total_requested, 'Imported');
return $done;
}
public static function canImport() {
Check::canUse('menu_import');
Check::canCall('menu_import_file');
Check::canCall('user_load');
return TRUE;
}
}