Need help print cache of drupal menu + convert relative paths to absolute ones

0 down vote favorite
share [g+] share [fb] share [tw]

I’ve got a script put togheter form other sources, not my own thinking. The reason for me needing to get this to work is to get a drupal top menu into top of an opencart installation to be able to use the superior cms functionalites while letting opencart handle the superior web shop functions.

The problem is that the menu that prints to a cache file from the script uses relative url.s. This of course means that when the menu is required into opencart, which is on a subdomain, the paths show the path of the subdomain. Not good.

The code is as follows:

[php]<?php
$cache_time = 3600; // Time in seconds to keep a page cached
$cache_folder = ‘cache’; // Folder to store cached files (no trailing slash)
$cache_filename = $cache_folder.md5($_SERVER[‘REQUEST_URI’]);
// Location to lookup or store cached file
//Check to see if this file has already been cached
// If it has get and store the file creation time
$cache_created = (file_exists($cache_file_name)) ? filemtime($this->filename) : 0;

if ((time() - $cache_created) < $cache_time) {readfile($cache_filename);
// The cached copy is still valid, read it into the output buffer
die();
}
?>

?php ob_start(); // Turns on output buffering ?>

<?php $contents = ob_get_contents(); ?> <?php if ($page['header_menu']): ?>
<?php print render($page['header_menu']); ?>
<?php endif; ?> <?php file_put_contents($cache_filename, ob_get_contents()); ob_end_flush(); ?>[/php]

Also, it would be great to be able to get the cached file always be written to the same filename, otherwise it wouldn’t do much good since the menu in the shop would’t change when the drupal menu does since the change would result in a new filename.

I’ve looked at other questions here regarding drupal and absolute paths but either I don’t understand enough or they are not applicable. If the first, I apologize for taking up everybodys time for something I should’ve understood from what already is available.

Any help very much appreciated. Cheers!

/Thomas Lillieskold

So I’m gonna go and answer myself with a solution, in case some other devil comes along and want to integrate a drupal top menu into opencart. The reason for this is of course to be able to use the superior cms, blog etc functions of drupal withouth having to buy expensive opencart modules to provide said functions.

So, I took the page.tpl file of the current theme I am using (of course the smart move to do instead is to create a subtheme of said theme och code it there to avoid overwriting when the base theme is updated if one uses an open source template that gets updated occasionally). So, the code is loaded whenever someone visits a page on the drupal site that uses the theme basically. :slight_smile:

<?php	  //assign dynamically generated page to $page
	  $pagefile = 'testmenu.txt';

	  // Define path and name of cached file
	  $cachefile = $pagefile;   
	 
	  // How long to keep cache file?
	  $cachetime = 18000;   
?>	 

<?php	  ob_start();
	  $contents = ob_get_contents(); //Stores the contents of the buffer in a variable as a string
?>	  
<?php	  if ($page['header_menu']): ?>
		<div id="header-menu-wrapper">
			<div id="header-menu">
        		<?php print render($page['header_menu']); ?>
      			</div>
   		 </div>
<?php endif; ?>

<?php	  $contents = ob_get_contents();
	  $contents = str_replace('href="','href="http://yourdomain.com', $contents);
	  file_put_contents($cachefile, $contents);
	  ob_end_flush();
?>

So, what I need to do now is rename the classes used in the css and the html respectively in drupal to avoid conflicts with opencart classes, import that css into the opencart theme file in the header and then require the html output from the theme code in drupal.

Now, what would be cool is to on a per regular basis import new users from opencart into drupal or something, but that’s not essential.

Sponsor our Newsletter | Privacy Policy | Terms of Service