From f53c22fb9c3862c0ec11040f079b2ab37ec85c61 Mon Sep 17 00:00:00 2001
From: Hugues Lamy <hugues@hlamy.ca>
Date: Wed, 9 Oct 2013 13:35:28 -0400
Subject: [PATCH] Add check to avoid including null attributes in menu_links

---
 includes/import.inc | 39 ++++++++++++++++++++++++++++++---------
 1 file changed, 30 insertions(+), 9 deletions(-)

diff --git a/includes/import.inc b/includes/import.inc
index fec321b..7389a50 100644
--- a/includes/import.inc
+++ b/includes/import.inc
@@ -153,6 +153,7 @@ function menu_import_parse_line($line, $prev_level, array $weights, array $paren
     'weight' => 0,
     'external' => FALSE,
     'level' => 0,
+    'mlid' => 0,
   );
 
   // Set default language
@@ -162,6 +163,7 @@ function menu_import_parse_line($line, $prev_level, array $weights, array $paren
   $langs = array_keys(language_list());
 
   $path = $description = $expanded = $hidden = $language = '';
+  $attributes = null;
 
   // JSON is used.
   // @todo: make the input JSON not so strict.
@@ -181,6 +183,15 @@ function menu_import_parse_line($line, $prev_level, array $weights, array $paren
       $hidden = !empty($details->hidden);
       $language = !empty($details->lang) && in_array($details->lang, $langs)
                     ? $details->lang : NULL;
+      $mlid = !empty($details->mlid) ? $details->mlid : 0;
+      $weight = isset($details->weight) ? $details->weight : null;
+      if (!empty($details->attributes)) {
+        foreach ((array)$details->attributes as $attribute) {
+          foreach($attribute as $attr_key => $attr_value) {
+            $attributes[$attr_key] = $attr_value;
+          }
+        }
+      }
     }
     else {
       return _menu_import_mark_error_item($menuitem, t('malformed item details'), $line);
@@ -208,22 +219,29 @@ function menu_import_parse_line($line, $prev_level, array $weights, array $paren
     return _menu_import_mark_error_item($menuitem, t('wrong indentation'), $line);
   }
 
-  if (isset($weights[$level])) {
-    if ($level > $prev_level) {
-      $weight = 0;
+  if (is_null($weight)) {
+    if (isset($weights[$level])) {
+      if ($level > $prev_level) {
+        $weight = 0;
+      }
+      else {
+        $weight = $weights[$level] + 1;
+      }
     }
     else {
-      $weight = $weights[$level] + 1;
+      $weight = 0;
     }
   }
-  else {
-    $weight = 0;
-  }
   $menuitem['weight'] = $weight;
   $menuitem['parent'] = !$level ? 0 : $parents[$level - 1];
   $menuitem['link_title'] = $title;
   $menuitem['level'] = $level;
   $menuitem['path'] = $path;
+  $menuitem['mlid'] = $mlid;
+  
+  if (!is_null($attributes)) {
+    $menuitem['options']['attributes'] = $attributes;
+  }
 
   if (url_is_external($path)) {
     $menuitem['external'] = TRUE;
@@ -249,7 +267,6 @@ function menu_import_parse_line($line, $prev_level, array $weights, array $paren
     // Important when setting the language, otherwise it'll be ignored.
     $menuitem['customized'] = 1;
   }
-
   return $menuitem;
 }
 
@@ -481,7 +498,11 @@ function menu_import_save_menu($menu, $options) {
       if (isset($menuitem['description'])) {
         $menuitem['options']['attributes']['title'] = $menuitem['description'];
       }
-
+      
+      // Save a blank menu_link if mlid is declared in order to force an update at this value.
+      if ($menuitem['mlid'] != 0) {
+        db_insert('menu_links')->fields(array('mlid' => $menuitem['mlid']))->execute();
+      }
       // Save menuitem and set mlid.
       $mlid = menu_link_save($menuitem);
       if (!$mlid) {
-- 
1.8.1.2