Need Install Profile Help

Events happening in the community are now at Drupal community events on www.drupal.org.
arcane's picture

In my install profile, I wrote some code to add the primary menu items after enabling a series of dependent modules. I cannot add menu items until the module paths exist. The problem seems to be timing, my menu code gets executed before all paths exist (after first enabling modules). Any on how to get around this?

Comments

Would a helper module make

frankcarey's picture

Would a helper module make this any simpler? When you enable the modules in the install hook, you could set this module to be the last in the list, thus they should all be loaded by then. Let me know how this works out because I will probably need to do this myself.

Sorry no joy

arcane's picture

I'm not having any luck, these are the steps I followed:

  1. Created a helper module as you suggested that populates the primary menu.
  2. Added the dependencies to the .info file
  3. Moved code execution to enable the module to the end of the script.

I still get the same problem. However, when I enable the module by hand, the primary menu is populated correctly.

Something Hackish

arcane's picture

I'd hate to have to write hackish something like this:

Create a hook_init.
    $primary_menu_installed = variable_get(primary_menu_installed, 0);
    If (!$primary_menu_installed) {

          a.  Check for desired_menu_path_exists 
          b.  If exists, then enable helper module.
          c.  variable_set( $primary_menu_installed, true)
         
          }

      else

      {
        return

       }

The menu table is in a weird

Steven Merrill's picture

The menu table is in a weird state during install. Note that after install, a menu_rebuild() always happens.

With that in mind, look at how openpublish does it. I'd recommend doing something similar. (They're leveraging the excellent Install Profile API module.)

<?php
function _openpublish_install_menus(&$context) {
 
cache_clear_all();
 
menu_rebuild();
 
 
// TODO: Rework into new Dashboard
 
$op_plid = install_menu_create_menu_item('admin/settings/openpublish/api-keys', 'OpenPublish Control Panel', 'Short cuts to important functionality.', 'navigation', 1, -49);
 
install_menu_create_menu_item('admin/settings/openpublish/api-keys', 'API Keys', 'Calais, Apture and Flickr API keys.', 'navigation', $op_plid, 1);
 
install_menu_create_menu_item('admin/settings/openpublish/calais-suite', 'Calais Suite', 'Administrative links to Calais, More Like This and Topic Hubs functionality.', 'navigation', $op_plid, 2);
 
install_menu_create_menu_item('admin/settings/openpublish/content', 'Content Links', 'Administrative links to content, comment, feed and taxonomy management.', 'navigation', $op_plid, 3);

 
// Primary Navigation
 
install_menu_create_menu_item('<front>',             'Home',       '', 'primary-links', 0, 1);
 
install_menu_create_menu_item('articles/Business',   'Business',   '', 'primary-links', 0, 2);
 
install_menu_create_menu_item('articles/Health',     'Health',     '', 'primary-links', 0, 3);
 
install_menu_create_menu_item('articles/Politics',   'Politics',   '', 'primary-links', 0, 4);
 
install_menu_create_menu_item('articles/Technology', 'Technology', '', 'primary-links', 0, 5);
 
install_menu_create_menu_item('blogs',               'Blogs',      '', 'primary-links', 0, 6);
 
install_menu_create_menu_item('resources',           'Resources''', 'primary-links', 0, 7);
 
install_menu_create_menu_item('events',              'Events',     '', 'primary-links', 0, 8);
 
install_menu_create_menu_item('topic-hubs',          'Topic Hubs', '', 'primary-links', 0, 9);

 
install_menu_create_menu('Footer Primary', 'footer-primary');
 
install_menu_create_menu_item('articles/all', 'Latest News', '', 'menu-footer-primary', 0, 1);
 
install_menu_create_menu_item('popular/all''Hot Topics''', 'menu-footer-primary', 0, 2);
 
install_menu_create_menu_item('blogs',        'Blogs',       '', 'menu-footer-primary', 0, 3);
 
install_menu_create_menu_item('resources',    'Resources',   '', 'menu-footer-primary', 0, 4);
 
install_menu_create_menu_item('events',       'Events',      '', 'menu-footer-primary', 0, 5);

 
install_menu_create_menu('Footer Secondary', 'footer-secondary');
 
install_menu_create_menu_item('node/3', 'Subscribe',      '', 'menu-footer-secondary', 0, 1);
 
install_menu_create_menu_item('node/2', 'Advertise',      '', 'menu-footer-secondary', 0, 2);
 
install_menu_create_menu_item('node/5', 'Jobs',           '', 'menu-footer-secondary', 0, 3);
 
install_menu_create_menu_item('node/6', 'Store',          '', 'menu-footer-secondary', 0, 4);
 
install_menu_create_menu_item('node/1', 'About Us',       '', 'menu-footer-secondary', 0, 5);
 
install_menu_create_menu_item('node/7', 'Site Map',       '', 'menu-footer-secondary', 0, 6);
 
install_menu_create_menu_item('node/8', 'Terms of Use',   '', 'menu-footer-secondary', 0, 7);
 
install_menu_create_menu_item('node/9', 'Privacy Policy', '', 'menu-footer-secondary', 0, 8);
 
install_menu_create_menu_item('node/1', 'About Us',       '', 'menu-footer-secondary', 0, 9);

 
install_menu_create_menu('Top Menu', 'top-menu');
 
install_menu_create_menu_item('node/1', 'About Us''', 'menu-top-menu', 0, 1);
 
install_menu_create_menu_item('node/2', 'Advertise', '', 'menu-top-menu', 0, 2);
 
install_menu_create_menu_item('node/3', 'Subscribe', '', 'menu-top-menu', 0, 3);
 
install_menu_create_menu_item('node/4', 'RSS',       '', 'menu-top-menu', 0, 4);
    
 
$msg = st('Installed Menus');
 
_openpublish_log($msg);
 
$context['message'] = $msg;
}
?>

Thanks Steven, I've actually

arcane's picture

Thanks Steven,

I've actually used this approach successfully in the past, it doesn't solve my original problem though. In my case, I was enabling a view module that surfaced a specific path that I wanted to place in the primary-links menu.
Even with calling menu_rebuild, I was still running into the paths not being defined.

I ended up with the following code which is working for me:

/**
* Implementation of hook_init().
*/
function site_deploy_menu_init() {

        $primary_menu_installed = variable_get("primary_menu_installed", 0);

        if (!$primary_menu_installed) {
                $photos_path_exists = menu_get_item("photos");

                if ($photos_path_exists)  {


                        // Enable Menus
                        // Note we use the macro module to record menu additions and then export them to a file

                        include_once drupal_get_path ('module','site_deploy_menu') . '/menus/menu-primary-links.inc';
                        include_once './includes/install.inc';
                        include_once './includes/menu.inc';
                          eval($form_state['values']['macro']);
                          drupal_execute_my_macro($macro);
                        menu_rebuild();

                        variable_set("primary_menu_installed", 1);

                }

                else

                {
                        return;

                }

        }

}


// **************************************************************************************************
// Helper functions
// **************************************************************************************************

function drupal_execute_my_macro($macro) {
  foreach ($macro as $key => $data) {
    $item = menu_get_item($data['path']);
    if ($item && !empty($item['file'])) {
      include_once $item['file'];
    }
  }

Blocks

arcane's picture

In addition, I'm coming upon a similar scenario with blocks. I'm finding that after an initial installation, blocks don't seem to be fully populated until I go to the admin/build/blocks page. I've used phpmyadmin to view the blocks table and it's empty until I go to that page. I'm not sure how to handle that situation.

Currently I am using the hook init trick, and am redirecting to the admin/build/blocks page at the end of my install routine. But I am hoping to find a better way.