home | career | drupal | java | mac | mysql | perl | php | scala | uml | unix

Drupal example source code file (menu.admin.js)

This example Drupal source code file (menu.admin.js) is included in the DevDaily.com "Drupal Source Code Warehouse" project. The intent of this project is to help you "Learn Drupal by Example".

PHP - Drupal tags/keywords

ajax, context, edit-menu, fieldset, function, jquery, option, php, selected, type, url, values, var

The menu.admin.js Drupal example source code

// $Id: menu.admin.js,v 1.1 2009/10/13 01:25:58 dries Exp $

(function ($) {

  Drupal.behaviors.menuChangeParentItems = {
    attach: function (context, settings) {
      $('fieldset#edit-menu input').each(function () {
        $(this).change(function () {
          // Update list of available parent menu items.
          Drupal.menu_update_parent_list();
        });
      });
    }
  }

  /**
   * Function to set the options of the menu parent item dropdown.
   */
  Drupal.menu_update_parent_list = function () {
    var values = [];

    $('input:checked', $('fieldset#edit-menu')).each(function () {
      // Get the names of all checked menus.
      values.push(Drupal.checkPlain($.trim($(this).val())));
    });

    var url = Drupal.settings.basePath + 'admin/structure/menu/parents';
    $.ajax({
      url: location.protocol + '//' + location.host + url,
      type: 'POST',
      data: {'menus[]' : values},
      dataType: 'json',
      success: function (options) {
        // Save key of last selected element.
        var selected = $('fieldset#edit-menu #edit-menu-parent :selected').val();
        // Remove all exisiting options from dropdown.
        $('fieldset#edit-menu #edit-menu-parent').children().remove();
        // Add new options to dropdown.
        jQuery.each(options, function(index, value) {
          $('fieldset#edit-menu #edit-menu-parent').append(
            $('<option ' + (index == selected ? ' selected="selected"' : '') + '></option>').val(index).text(value)
          );
        });
      }
    });
  }

})(jQuery);

Other Drupal examples (source code examples)

Here is a short list of links related to this Drupal menu.admin.js source code file:

new blog posts

"Drupal" is a registered trademark of Dries Buytaert.

my drupal tutorials and examples  

Copyright 1998-2016 Alvin Alexander, alvinalexander.com
All Rights Reserved.

Beginning in 2016, a portion of the proceeds from pages under the '/drupal-code-examples/' URI will be donated to charity.