You are here

function menu_taxonomy_xml_term_postsave in Taxonomy import/export via XML 6.2

Hook into importing term data.

Runs in post because we need to know the new term ID now.

Does not delete old paths.

File

includes/taxonomy_xml.menu.inc, line 55
Support for importing or exporting menu items along with terms

Code

function menu_taxonomy_xml_term_postsave(&$term) {
  if (!empty($term->predicates['menu'])) {

    // probably is an array containing one TRUE/FALSE flag
    if (!is_array($term->predicates['menu'])) {
      $term->predicates['menu'] = array(
        $term->predicates['menu'],
      );
    }
    $do_menu = reset($term->predicates['menu']);
    if ($do_menu) {

      // Invent or update a menu item.
      // We have only paths to go on
      foreach ((array) $term->predicates['path'] as $path) {
        $term->path = $path;
      }
      if (!$term->path) {
        drupal_set_message(t("I was about to set a menu item for the term %term, but there is no path set. So I won't do that then", array(
          '%term' => $term->name,
        )), 'warning');
        return FALSE;
      }

      // First look for an existing item at the terms path
      if ($menu = taxonomy_xml_menu_get_item_by_path($term->path)) {

        // If it exists, then we can be pretty sure it's pointing at the right
        // place, so, just leave it
        drupal_set_message(t("Checked menu item for %term - it already exists.", array(
          '%term' => $term->name,
        )));

        // TODO does this mean I should reset/override it?
        return TRUE;
      }

      // Do NOT recurse up and autocreate the tree.
      // Just see if there is a valid parent and use that.
      if ($parent_menu = taxonomy_xml_menu_get_item_by_path(dirname($term->path))) {
        drupal_set_message(t("Need to make a new menu item for %term - based on path %path. Parent item was found, so this should work.", array(
          '%term' => $term->name,
          '%path' => $term->path,
        )));
        $menu = array(
          'link_title' => $term->name,
          'link_path' => drupal_get_normal_path($term->path),
          'parent' => $parent_menu['menu_name'] . ':' . $parent_menu['mlid'],
          'menu_name' => $parent_menu['menu_name'],
          'plid' => $parent_menu['mlid'],
        );
        $new_mlid = menu_link_save($menu);
      }
      else {

        // If no parent .. give up.
        drupal_set_message(t("I was about to set a menu item for the term being added at the path %path , but there is no parent menu item to attach to. So I won't do that then", array(
          '%path' => $term->path,
        )));
        return FALSE;
      }
    }
  }
}