You are here

function _simplemenu_add_menu in SimpleMenu 6

Same name and namespace in other branches
  1. 7 simplemenu.module \_simplemenu_add_menu()

\brief Add the simplemenu variable with the menu to be displayed.

This function loads the menu to be displayed and transforms it so it works with superfish.

If the cache version of the simplemenu JavaScript string cannot be created, then it is sent inline whether or not the user asked for it to be sent inline.

1 call to _simplemenu_add_menu()
simplemenu_init in ./simplemenu.module
Implementation of hook_init().

File

./simplemenu.module, line 72
Creates a simplemenu.

Code

function _simplemenu_add_menu() {
  $menu = simplemenu_get_menu();
  if (!$menu) {
    return FALSE;
  }
  $simplemenu = 'var simplemenu=' . drupal_to_js($menu) . ';';

  // Note that if file_downloads is set to private the file wouldn't be visible
  // from the outside so we cannot cache it... It is fixed in D7 with the
  // use of protocols such as 'public://css'.
  $has_file = variable_get('simplemenu_cache_menu', TRUE) && variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PUBLIC;
  if ($has_file) {
    $js_path = file_create_path('js');

    // same path as concatenated Core JS
    $js_md5 = md5($simplemenu);

    // this is a lot faster than transferring the menu for each page!
    $js_filename = $js_path . '/simplemenu-' . $js_md5 . '.js';
    $has_file = file_check_directory($js_path, FILE_CREATE_DIRECTORY);
    if ($has_file) {

      // The old way was to send the whole menu each time
      if (!file_exists($js_filename)) {

        // use LOCK so concurrent writes don't mess up the file
        @file_put_contents($js_filename, $simplemenu);
        $has_file = file_exists($js_filename);
      }
      else {
        $has_file = TRUE;
      }
    }
  }
  $scope = variable_get('simplemenu_menu_scope', 'footer');
  if ($has_file) {
    drupal_add_js($js_filename, 'module', $scope);
  }
  else {
    drupal_add_js($simplemenu, 'inline', $scope);
  }
  return TRUE;
}