You are here

function _menu_target_menu_links_update_target in Menu target 8

Helper function for updating all menu link target or class attributes.

File

./menu_target.admin.inc, line 166
Allows content editors to choose wether or not to open menu items in a new window

Code

function _menu_target_menu_links_update_target($menu_target_type) {
  $old_attribute = $menu_target_type == 'html' ? 'class' : 'target';
  $old_value = $menu_target_type == 'html' ? 'target-blank' : '_blank';
  $new_attribute = $menu_target_type == 'html' ? 'target' : 'class';
  $new_value = $menu_target_type == 'html' ? '_blank' : 'target-blank';
  foreach (db_query("SELECT mlid, options FROM {menu_links} WHERE options <> ''") as $menu_item) {
    $menu_item->options = unserialize($menu_item->options);
    if (isset($menu_item->options['attributes'])) {
      $attributes =& $menu_item->options['attributes'];
    }
    if (isset($attributes[$old_attribute]) && in_array($old_value, $attributes[$old_attribute])) {
      unset($attributes[$old_attribute][array_search($old_value, $attributes[$old_attribute])]);
      if (empty($attributes[$old_attribute])) {
        unset($attributes[$old_attribute]);
      }
      $attributes[$new_attribute][] = $new_value;
    }
    db_update('menu_links')
      ->fields(array(
      'options' => serialize($menu_item->options),
    ))
      ->condition('mlid', $menu_item->mlid)
      ->execute();
  }
}