You are here

function _nodesymlinks_restore_menu_link in NodeSymlinks 6

Same name and namespace in other branches
  1. 7 nodesymlinks.install \_nodesymlinks_restore_menu_link()

Private function for re-populating menu_links with our stored entries.

1 call to _nodesymlinks_restore_menu_link()
nodesymlinks_enable in ./nodesymlinks.install
Implementation of hook_enable().

File

./nodesymlinks.install, line 89
Installation functions for NodeSymlinks.

Code

function _nodesymlinks_restore_menu_link($record) {

  // Make sure this MLID doesn't actually exist.
  $exists = (bool) db_result(db_query('SELECT * FROM {menu_links} WHERE mlid = %d', $record['mlid']));
  if (!$exists) {

    // Looks like it got wiped out.
    // Let's restore it.
    $item = unserialize($record['item_data']);
    $nid = $record['nid'];

    // Dynamically load the node in case the path changed between disable and re-enable.
    $node = node_load($nid);

    // This is from nodesymlinks_item_save. We basically want to
    // re-save our item via the menu_link API again.
    // @WARNING:
    // The items we restore must be re-entered into the menu_link system.
    // I do not think it will cause problems, so I re-use the old MLIDs
    // which should have been originally granted in the serial datatype.
    // BUT, if menu_links are suddenly getting overwritten, this could very
    // well be the culprit.
    // This is an attempt to try and keep our URLs sane so they don't all
    // break bookmarks on every disable/re-enable.
    $item['mlid'] = $record['mlid'];
    $item['link_path'] = 'node/' . $nid . '/mid/' . $item['mlid'];
    if (menu_link_save($item)) {

      // make alias using the "fresh" node data.
      $node_alias = $node->path == 'node/' . $node->nid ? NULL : $node->path;
      if ($node_alias) {
        path_set_alias($item['link_path'], $node_alias . '_' . $item['mlid']);
      }

      // Replace the record in the db.
      // NOTE: I'm not sure if this will cause an infinite loop if our original
      // select isn't in a transaction. I guess we'll see.
      db_query('DELETE FROM {nodesymlinks_link_storage} WHERE mlid=%d', $record['mlid']);

      // Insert using our new $item record (with updated serialized item data in case node path changed)
      db_query("INSERT INTO {nodesymlinks_link_storage} (mlid, nid, item_data)\n        VALUES (%d, %d, '%s')", $item['mlid'], $nid, serialize($item));
    }
  }
}