You are here

function _nodesymlinks_restore_menu_link in NodeSymlinks 7

Same name and namespace in other branches
  1. 6 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
Implements hook_enable().

File

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

Code

function _nodesymlinks_restore_menu_link($record) {

  // Make sure this MLID doesn't actually exist.
  $exists = (bool) db_query('SELECT * FROM {menu_links} WHERE mlid = :mlid', array(
    ':mlid' => $record['mlid'],
  ))
    ->fetchField();
  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 = array(
          'source' => $item['link_path'],
          'alias' => $node_alias . '_' . $item['mlid'],
        );
        path_save($path);
      }

      // 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_delete('nodesymlinks_link_storage')
        ->condition('mlid', $record['mlid'])
        ->execute();

      // Insert using our new $item record (with updated serialized item data
      // in case node path changed).
      $id = db_insert('nodesymlinks_link_storage')
        ->fields(array(
        'mlid' => $item['mlid'],
        'nid' => $nid,
        'item_data' => serialize($item),
      ))
        ->execute();
    }
  }
}