You are here

function menu_views_menu_edit_item_submit in Menu Views 7

Submit handler for menu_edit_item form

1 string reference to 'menu_views_menu_edit_item_submit'
menu_views_form_menu_edit_item_alter in includes/form.inc
Implements hook_form_FORM_ID_alter(). Change path description. Insert the views selection.

File

includes/form.inc, line 169
Form hooks for the menu_views module.

Code

function menu_views_menu_edit_item_submit($form, &$form_state) {
  $item =& $form_state['values'];

  // Retrieve view information.
  $view = menu_views($form, $form_state);

  // Retrieve the original menu view information for this menu link.
  $original = array();
  if (isset($item['original_item']['options']['menu_views'])) {
    $original = $item['original_item']['options']['menu_views'];
  }

  // The value of "hidden" is the opposite of the value
  // supplied by the "enabled" checkbox.
  $item['hidden'] = (int) (!$item['enabled']);
  unset($item['enabled']);

  // Only set the title attribute if the menu_attributes moduule is not present.
  if (!module_exists('menu_attributes')) {
    $item['options']['attributes']['title'] = $item['description'];
  }

  // Set the menu name and plid.
  list($item['menu_name'], $item['plid']) = explode(':', $item['parent']);

  // Generate the new menu view information from the current form.
  $menu_views = array(
    'name' => $view->name,
    'display' => $view->display,
    'arguments' => $view->arguments,
    // The mlid should remain constant, do not use view information.
    'mlid' => $item['mlid'],
  );

  // Save the menu view information in menu link options.
  $item['options']['menu_views'] = $menu_views;

  // Unset the unecessary property.
  if (isset($item['menu_views'])) {
    unset($item['menu_views']);
  }

  // If this is a new menu link, save the initial menu link to return the mlid before we re-save the attached view.
  if (!$menu_views['mlid']) {
    $menu_views['mlid'] = menu_link_save($item);
  }

  // Save the menu link and display the appropriate status message.
  if (!menu_link_save($item)) {
    drupal_set_message(t('There was an error saving the menu link.'), 'error');
  }
  else {

    // Compare the original view information with the new one and determine if the menu's cache should be cleared.
    $diff = array_diff_assoc($menu_views, $original);
    if (!empty($diff)) {

      // Clear the affected menu cache so the view can render properly.
      menu_cache_clear($item['menu_name']);
      _menu_clear_page_cache();
    }
    drupal_set_message(t('Your configuration has been saved.'));
  }

  // Redirect to the menu list.
  $form_state['redirect'] = 'admin/structure/menu/manage/' . $item['menu_name'];
}