You are here

function i18nmenu_nodeapi in Internationalization 6

Implementation of hook_nodeapi().

Save or delete menu item strings associated with nodes.

File

i18nmenu/i18nmenu.module, line 422
Internationalization (i18n) submodule: Menu translation.

Code

function i18nmenu_nodeapi(&$node, $op) {
  switch ($op) {
    case 'presave':

      // Ensure that the menu item language always matches node language.
      if (isset($node->menu) && isset($node->language)) {
        $node->menu['language'] = $node->language;
      }
      break;
    case 'insert':
    case 'update':
      if (isset($node->menu)) {
        $item = $node->menu;
        if (!empty($item['delete'])) {
          _i18nmenu_delete_item($item['mlid']);
        }
        elseif (trim($item['link_title'])) {
          $item['link_title'] = trim($item['link_title']);
          $item['link_path'] = "node/{$node->nid}";
          _i18nmenu_update_item($item);
        }
      }
      break;
    case 'delete':

      // Delete all menu item link translations that point to this node.
      $result = db_query("SELECT mlid FROM {menu_links} WHERE link_path = 'node/%d' AND module = 'menu'", $node->nid);
      while ($m = db_fetch_array($result)) {
        _i18nmenu_delete_item($m['mlid']);
      }
      break;
    case 'prepare translation':
      if (empty($node->menu['mlid']) && !empty($node->translation_source)) {
        $tnode = $node->translation_source;

        // Prepare the tnode so the menu item will be available.
        node_object_prepare($tnode);
        $node->menu['link_title'] = $tnode->menu['link_title'];
        $node->menu['weight'] = $tnode->menu['weight'];
      }
      break;
  }
}