Localized stray item generate #1102570: array_flip() [function.array-flip] issue in DrupalDefaultEntityController / entity.inc error and "Trying to get property of non-object in taxonomy_menu_translated_menu_link_alter() ".
I got few some old D6 menu terms that stay in menu after rebuild.

Those stray items don't have any term relation.
Then in the function taxonomy_menu_translated_menu_link_alter, _taxonomy_menu_get_item return 0.
After that the function i18n_taxonomy_vocabulary_mode generate the error #1102570: array_flip() [function.array-flip] issue in DrupalDefaultEntityController / entity.inc as there is no entity.

What would be the best:

  • To test $t if it is 0 before continuing?
  • Make a the rebuild function running well?

Comments

rondev’s picture

I added a test and an output to the watchdog to help to remove bad menu entries:

function taxonomy_menu_translated_menu_link_alter(&$item, $map) {
  if (module_exists('i18n_taxonomy')) {
    // In case of localized terms, use term translation for menu title.
    if ($item['module'] == 'taxonomy_menu') {
      $t = _taxonomy_menu_get_item($item['mlid']);
      // Only translate when term exist (may per example occur with stray menu item)
      if ($t) {
        // Only translate when translation mode is set to localize
        if (i18n_taxonomy_vocabulary_mode($t->vid, I18N_MODE_LOCALIZE)) {
          // this is a term
          if ($t->tid > 0) {
            $term = taxonomy_term_load($t->tid);
            $display_num = '';
            $num = _taxonomy_menu_term_count($t->tid);

            // If hide menu is selected and the term count is 0 and the term has no children then do not create the menu item
            if ($num == 0 && variable_get(_taxonomy_menu_build_variable('hide_empty_terms', $t->vid), FALSE) && !_taxonomy_menu_children_has_nodes($t->tid, $t->vid)) {
              $display_num = '';
            }
            // if display number is selected and $num > 0 then change the title
            else if (variable_get(_taxonomy_menu_build_variable('display_num', $t->vid), FALSE)) {
              // if number > 0 and display decendants, then count all of the children
              if (variable_get(_taxonomy_menu_build_variable('display_descendants', $t->vid), FALSE)) {
                $num = taxonomy_menu_term_count_nodes($t->tid, $t->vid);
              }
              $display_num = " ($num)";
            }

            if ($item['title'] != ($term->name . $display_num)) {
              // Should not happen
              watchdog('error', t('Menu and taxonomy name mismatch: @title != @name', array('@title' => $item['title'], '@name' => $term->name . $display_num)));
            }

            $term = i18n_taxonomy_localize_terms($term);
            $item['title'] = $item['link_title'] = $term->name . $display_num;
            if ($term->description) {
              $item['options']['attributes']['title'] = $term->description;
            }
          }
          // is a vocabulary
          else {
            $vocab = taxonomy_vocabulary_load($t->vid);
            $item['title'] = i18n_string('taxonomy:vocabulary:'. $vocab->vid .':name', $vocab->name);
          }
        }
      }
      // no term, add a watchdog entry to help
      else {
        watchdog('taxonomy_menu', t('Error with menu entry "%me" in menu "%mt"'), array('%me' => $item['title'],'%mt' => $item['menu_name']));
      }
    }
  }
}

PS: Although I may propose working code, I consider myself as a newbie concerning php and drupal api.

The problem with that solution is that it adds watchdog entry when rebuilding menu too for each valid terms. We should have a more sophisticated test to see if the menu entry is a valid term. Per example with if (_taxonomy_menu_get_tid($item['mlid']))

rondev’s picture

Status: Needs work » Needs review
  • Make a the rebuild function running well?

I cite myself because I studied more in deep the _taxonomy_menu_delete_all function.
It deletes only terms of the taxonomy, not menu entries that are not terms that may have been added sooner or later. I consider myself that as a good and safe behavior.

The watchdog entry I added in the proposed code to taxonomy_menu_translated_menu_link_alter should help the site maintainer to let, move or remove the menu entry which is in a menu dedicated to taxonomy terms. It seems a good way to solve the issue.

dstol’s picture

Status: Needs review » Fixed

Hey, welcome to Drupal and thanks for the patch! http://drupalcode.org/project/taxonomy_menu.git/commit/333323c

dstol’s picture

rondev’s picture

I don't think my modification generate the issue #1496192: Latest dev breaks multi language taxonomy menu.
I'm not sure. I just test if $t is valid.
Before, if $t was not valid there was an error and the code was not executed.
Founded nothing wrong using a debugger.

johnv’s picture

Title: Localized stray item generate "array_flip() [function.array-flip]..." and "Trying to get property of non-object" errors » [i18n] Localized stray item generate "array_flip() [function.array-flip]..." and "Trying to get property of non-object" errors

  • dstol committed 333323c on 8.x-3.x
    Issue #1483010 by rondev: Fixed Localized stray item generate '...
vladimiraus’s picture

Issue summary: View changes
Status: Needs work » Closed (outdated)

Thank you for your contributions.
Drupal 7 is no longer supported.
Closing this issue as outdated.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.