I am creating a set of nodes in an installation profile. When I try to create menu links for them in the same PROFILE_NAME.install, the links don't get created.

I have also tried creating the menus links in a separate module, and using hook_update_N() to enable them, but that also does not work.

When I uninstall the module and reactivate it, it creates correctly my menu's links correctly.

I tried to use hook_install_tasks() and put it in MYPROFILE_NAME.profile or MYPROFILE_NAME.install, but this also doesn't work.

How can I create menu links for nodes created as part of an installation profile?

My code for creating the nodes in MY_PROFILE_NAME.install is:

// creation of my nodes (same content for all)
   $nodi = array(
       // i have other 14 nodes  
      'title' => array('Home'),
       // i have other 14 paths for the nodes
      'path' => array('home'),
   );
$titoli = array();
$titoli = $nodi['title'];
$percorsi = array();
$percorsi = $nodi['path'];
//CREAZIONI DEL CONTENUTO PROGRAMMATICAMENTE
  for($i = 0; $i< count($titoli); $i++){
$testo_body = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec feugiat velit eget metus placerat,varius eros facilisis. Aenean aliquam imperdiet nisl ut dapibus...';
           $node = new stdClass(); // nuovo oggetto nodo
           $lingua = 'it'; 
           $node->type = "page"; //
           $node->title = $titoli[$i];
           $node->language = $lingua; 
           $node->path = array('alias' => $percorsi[$i]); // i think here is the problem !!!
           $node->uid = 1; // admin
           $node->status = 1;
           // Body field    
           $body_field_info = field_info_field('body');
           if (field_is_translatable('node', $body_field_info)) {
              $body_lingua = field_valid_language($lingua);
           }else{
              $body_lingua = LANGUAGE_NONE;
           }
           $node->body[$body_lingua][0]['value'] = $testo_body;
           $node->body[$body_lingua][0]['summary'] = 'text...';
           $node->body[$body_lingua][0]['format'] = 'full_html';
           $node = node_submit($node); // Prepare node for a submit
           node_save($node); // After this call we'll get a nid

And my code for creating the menu links is:


    // Create a Home link in the main menu.   
  $path = drupal_lookup_path('source', 'home');
  $item = array(
    'link_path' => $path,
    'router_path' => $path,
    'link_title' => st('Home'),
    'menu_name' => 'main-menu',
    'weight' => -50,
    'customized' => 1,
    'plid' => 0, // Parent menu item, 0 if menu item is on top level
  );
  menu_link_save($item);
  menu_rebuild();

Update 2

The menus are created in the menu_links table but with empty link_path so i can't get the nid created nodes. I treid also to override the install_finished() and put my function for creating menus in .install or in .profile files, but it's the same. Any ideas?

You can add a menu item using this

$item = array(
        'menu_name' => 'menu-name',
        'link_title' => 'Link title',
        'link_path' => 'node/'. $nid,
      );
menu_link_save($item);
//don't forget to clear menu cache after saving menu item
menu_cache_clear_all();

Hope this will help

Updated Answer As I looked into Drupal 7 standard profile installation file, that is standard.install (Line no 413), you can see "Home" link creation there.

// Create a Home link in the main menu.
  $item = array(
    'link_title' => st('Home'),
    'link_path' => '<front>',
    'menu_name' => 'main-menu',
  );
  menu_link_save($item);

  // Update the menu router information.
  menu_rebuild();
  • Thank you for your answer, but i already used the menu_link_save() and it works nice, my problem is that it doesn't work in MY_PROFILE_NAME.install and else separately in a module if called from the profile .install, it works only when i activate the module after the installation process. – Ossama Jan 13 '16 at 17:38
  • @Ossama, please see my updated answer. – Atul Dubey Jan 14 '16 at 5:58
  • 1
    @Ossama, if it helps you, please upvote and mark it as correct, Thanks. – Atul Dubey Jan 14 '16 at 10:14
  • Make sure your node is created before saving link. – Atul Dubey Jan 14 '16 at 10:36
  • the problem was 'customize' => 1, that i did not type first, now how can i know if my nodes are already created before creating the menus?shall i make a query on the db table? – Ossama Jan 14 '16 at 10:44
up vote 0 down vote accepted
$nodi = array(
        'title' => array(
                 'Home' ,
                 'Presentazione' ,
                         'Obiettivi' ,
                         'Comitato organizzatore' ,
                         'Patrocini' ,
                         'Edizioni passate' ,
                        // 'Calendario eventi' ,
                 'Materiali' ,
                         'Cartelle stampa' ,
                         'Documentazione' ,
                         'Video' ,
                 'Dove siamo' ,
                 'Contatti',
        ),
        'path' => array(
                 'home' ,
                 'presentazione' ,
                         'obiettivi' ,
                         'comitato-organizzatore' ,
                         'patrocini' ,
                         'edizioni-passate' ,
                  //'calendar-node-field-date/month' ,
                 'materiali' ,
                         'cartelle-stampa' ,
                         'documentazione' ,
                         'video' ,
                 'dove-siamo' ,
                 'contatti',
        ),
    );
    $titoli = array();
    $titoli = $nodi['title'];
    $percorsi = array();
    $percorsi = $nodi['path'];
    $miei_path = array();
    $alias = array();

    //CREAZIONI DEL CONTENUTO PROGRAMMATICAMENTE
      for($i = 0; $i< count($titoli); $i++){
        //SE È IL CALENDARIO O GALLERIA DELLE IMMAGINI NON CREARE LE PAGINE PERCHÉ CI SONO GIÀ

               $testo_body = 'Lorem ipsum dolor ..';
               $node = new stdClass(); // nuovo oggetto nodo
               $lingua = 'it'; 
               $node->type = "page";

               node_object_prepare($node); // Set some default values.

               $node->title = $titoli[$i];
               $node->language = $lingua; //non settato LANGUAGE_NONE
               $node->path['pathauto'] = 0;
               $node->uid = 1; // admin
               $node->status = 1;
               $node->comment = 0;
               $node->promote = 0;
               $node->moderate = 0;
               $node->sticky = 0;
               // Body field
               $body_field_info = field_info_field('body');
               if (field_is_translatable('node', $body_field_info)) {
                  $body_lingua = field_valid_language($lingua);
               }else{
                  $body_lingua = LANGUAGE_NONE;
               }
               $node->body[$body_lingua][0]['value'] = $testo_body;
               $node->body[$body_lingua][0]['summary'] = 'Questo è un testo esempio.';
               $node->body[$body_lingua][0]['format'] = 'full_html';
              $node = node_submit($node); // Prepare node for a submit

               $node->path = array('alias' => ('content/'.$percorsi[$i]));         // Setting a node path
              node_save($node); // After this call we'll get a nid

              $mio_path = $node->nid;
              $miei_path[$i] = $mio_path;
              $mio_alias[$i] = $node->path['alias'];
              watchdog(WATCHDOG_INFO, "E' stato creato il nodo " . $titoli[$i] . " correttamente, con alias  ".$percorsi[$i] ."---- " . $mio_alias[$i]. "   ".var_dump($node->path), $variables = array(), $severity = WATCHDOG_NOTICE, $link = NULL);
              $fornt = "node/".$miei_path[0];
              variable_set('site_frontpage', $fornt);
        }
  • i simply add an array $miei_path to save inside it all my node->nid to be used as value for link_path. – Ossama Apr 11 '16 at 13:33

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.