Problem/Motivation

Removing the last menu icon does not work, this is caused by the CSS generating function, menu_icons_css_generate(). When all menu icons are lost the SQL query on line 282 of menu_icons.module returns an empty resultset, therefore variable $css stays empty and the conditional logic on lines 310-314 is not executed:

  if (!empty($css)) {
    $csspath = 'public://css';
    file_prepare_directory($csspath, FILE_CREATE_DIRECTORY);
    file_unmanaged_save_data($css, $csspath . '/menu_icons.css', FILE_EXISTS_REPLACE);	
  }

Proposed resolution

There are two possible solutions to this problem, either filling $css with a comment that the file is intentionally left blank, which to me seems the nice way to do it. However, this needs error checking on the db_query, and I haven't found an easy way to do so.
The simpler solution would be to just assume that an empty result from the database, and consequently an empty $css is intentional, and therefore we can just drop the conditional part and always execute the file operations.
This second solution causes virtually no overhead as the function (to my knowledge) is only invoked on editing the menu items from the admin panel, and can only be considered unneccessary overhead in case you have the Menu Icons module enabled but no icons configured (which in most cases would be just a temporary situation anyway).
For completeness, the proposed new version of menu_icons_css_generate():

/**
 * Build CSS based on menu IDs
 *
 * @return A string with the CSS
 */
function menu_icons_css_generate() {

  $css = "";
  $result = db_query("SELECT mlid, options FROM {menu_links}");
  $pos = variable_get('menu_icons_position', 'left');

  foreach ($result as $item) {
    $options = unserialize($item->options);

    if (isset($options['menu_icon']) && $options['menu_icon']['enable'] && !empty($options['menu_icon']['path']) && file_exists($options['menu_icon']['path'])) {

      $image_path = $options['menu_icon']['path'];
      $image_style = (isset($options['menu_icon']['image_style']) && !empty($options['menu_icon']['image_style'])) ? $options['menu_icon']['image_style'] : NULL;

      if ($image_style) {
        $source_uri = $image_path;
        $image_path = image_style_path($image_style, $source_uri);

        if (!file_exists($image_path)) {
          image_style_create_derivative(image_style_load($image_style), $source_uri, $image_path);
        }
      }

      // Retrieve the image dimensions
      $info = image_get_info($image_path);
      $image_url = file_create_url($image_path);

      // Support private filesystem
      $css .= theme('menu_icons_css_item', array('mlid' => $item->mlid, 'path' => $image_url, 'size' => $info['width'], 'pos' => $pos));
    }
  }
  // Write new CSS file
  $csspath = 'public://css';
  file_prepare_directory($csspath, FILE_CREATE_DIRECTORY);
  file_unmanaged_save_data($css, $csspath . '/menu_icons.css', FILE_EXISTS_REPLACE);	
}

Comments

hugovdberg’s picture

Title: Last menu icon is disappearing on disable » Last menu icon is NOT disappearing on disable

Changed title, said exactly opposite of what I meant

bhauff’s picture

We are seeing this exact same issue. When there is only one icon set you can't remove it. If you add a second icon you can then unset either of them, but not both.

acrollet’s picture

Status: Active » Fixed

Thanks for the bug report - http://drupalcode.org/project/menu_icons.git/commit/098d3fa from #1200004: File Permissions could not be set on menu_icons.css got most of the way to fixing this issue, and I just added http://drupalcode.org/project/menu_icons.git/commit/40b46c0 to handle the case of disabling/removing the last menu icon.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.