Advertisement
DrupalCustom

dropdownexaple

Feb 16th, 2012
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 19.36 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * @file
  5.  * AJAX Examples module file with basic examples.
  6.  */
  7.  
  8. /**
  9.  * @defgroup ajax_example Example: AJAX
  10.  * @ingroup examples
  11.  * @{
  12.  * These examples show basic AJAX concepts.
  13.  *
  14.  * General documentation is available at
  15.  * @link ajax AJAX Framework documentation @endlink and at the
  16.  * @link http://drupal.org/node/752056 AJAX Forms handbook page @endlink.
  17.  *
  18.  * The several examples here demonstrate basic AJAX usage.
  19.  */
  20.  
  21. // The Node Form Alter example needs to be in another file.
  22. module_load_include('inc', 'ajax_example', 'ajax_example_node_form_alter');
  23.  
  24. /**
  25.  * Implements hook_menu().
  26.  *
  27.  * Sets up calls to drupal_get_form() for all our example cases.
  28.  *
  29.  * @see menu_example.module for more details on hook_menu().
  30.  */
  31. function ajax_example_menu() {
  32.   $items = array();
  33.  
  34.   $items['examples/ajax_example'] = array(
  35.     'title' => 'AJAX Example',
  36.     'page callback' => 'ajax_example_intro',
  37.     'access callback' => TRUE,
  38.     'expanded' => TRUE,
  39.   );
  40.  
  41.   // Change the description of a form element.
  42.   $items['examples/ajax_example/simplest'] = array(
  43.     'title' => 'Simplest AJAX Example',
  44.     'page callback' => 'drupal_get_form',
  45.     'page arguments' => array('ajax_example_simplest'),
  46.     'access callback' => TRUE,
  47.     'weight' => 0,
  48.   );
  49.   // Generate a changing number of checkboxes.
  50.   $items['examples/ajax_example/autocheckboxes'] = array(
  51.     'title' => 'Generate checkboxes',
  52.     'page callback' => 'drupal_get_form',
  53.     'page arguments' => array('ajax_example_autocheckboxes'),
  54.     'access callback' => TRUE,
  55.     'weight' => 1,
  56.   );
  57.   // Generate different textfields based on form state.
  58.   $items['examples/ajax_example/autotextfields'] = array(
  59.     'title' => 'Generate textfields',
  60.     'page callback' => 'drupal_get_form',
  61.     'page arguments' => array('ajax_example_autotextfields'),
  62.     'access callback' => TRUE,
  63.     'weight' => 2,
  64.   );
  65.  
  66.   // Submit a form without a page reload.
  67.   $items['examples/ajax_example/submit_driven_ajax'] = array(
  68.     'title' => 'Submit-driven AJAX',
  69.     'page callback' => 'drupal_get_form',
  70.     'page arguments' => array('ajax_example_submit_driven_ajax'),
  71.     'access callback' => TRUE,
  72.     'weight' => 3,
  73.   );
  74.  
  75.   // Repopulate a dropdown based on form state.
  76.   $items['examples/ajax_example/dependent_dropdown'] = array(
  77.     'title' => 'Dependent dropdown',
  78.     'page callback' => 'drupal_get_form',
  79.     'page arguments' => array('ajax_example_dependent_dropdown'),
  80.     'access callback' => TRUE,
  81.     'weight' => 4,
  82.   );
  83.   // Repopulate a dropdown, but this time with graceful degredation.
  84.   // See ajax_example_graceful_degradation.inc.
  85.   $items['examples/ajax_example/dependent_dropdown_degrades'] = array(
  86.     'title' => 'Dependent dropdown (with graceful degradation)',
  87.     'page callback' => 'drupal_get_form',
  88.     'page arguments' => array('ajax_example_dependent_dropdown_degrades'),
  89.     'access callback' => TRUE,
  90.     'weight' => 5,
  91.     'file' => 'ajax_example_graceful_degradation.inc',
  92.   );
  93.   // The above example as it appears to users with no javascript.
  94.   $items['examples/ajax_example/dependent_dropdown_degrades_no_js'] = array(
  95.     'title' => 'Dependent dropdown with javascript off',
  96.     'page callback' => 'drupal_get_form',
  97.     'page arguments' => array('ajax_example_dependent_dropdown_degrades', TRUE),
  98.     'access callback' => TRUE,
  99.     'file' => 'ajax_example_graceful_degradation.inc',
  100.     'weight' => 5,
  101.   );
  102.  
  103.   // Populate a form section based on input in another element.
  104.   $items['examples/ajax_example/dynamic_sections'] = array(
  105.     'title' => 'Dynamic Sections (with graceful degradation)',
  106.     'page callback' => 'drupal_get_form',
  107.     'page arguments' => array('ajax_example_dynamic_sections'),
  108.     'access callback' => TRUE,
  109.     'weight' => 6,
  110.     'file' => 'ajax_example_graceful_degradation.inc',
  111.   );
  112.   // The  above example as it appears to users with no javascript.
  113.   $items['examples/ajax_example/dynamic_sections_no_js'] = array(
  114.     'title' => 'Dynamic Sections w/JS turned off',
  115.     'page callback' => 'drupal_get_form',
  116.     'page arguments' => array('ajax_example_dynamic_sections', TRUE),
  117.     'access callback' => TRUE,
  118.     'weight' => 6,
  119.     'file' => 'ajax_example_graceful_degradation.inc',
  120.   );
  121.  
  122.   // A classic multi-step wizard, but with no page reloads.
  123.   // See ajax_example_graceful_degradation.inc.
  124.   $items['examples/ajax_example/wizard'] = array(
  125.     'title' => 'Wizard (with graceful degradation)',
  126.     'page callback' => 'drupal_get_form',
  127.     'page arguments' => array('ajax_example_wizard'),
  128.     'access callback' => TRUE,
  129.     'file' => 'ajax_example_graceful_degradation.inc',
  130.     'weight' => 7,
  131.   );
  132.   // The above example as it appears to users with no javascript.
  133.   $items['examples/ajax_example/wizard_no_js'] = array(
  134.     'title' => 'Wizard w/JS turned off',
  135.     'page callback' => 'drupal_get_form',
  136.     'page arguments' => array('ajax_example_wizard', TRUE),
  137.     'access callback' => TRUE,
  138.     'file' => 'ajax_example_graceful_degradation.inc',
  139.     'weight' => 7,
  140.   );
  141.  
  142.   // Add-more button that creates additional form elements.
  143.   // See ajax_example_graceful_degradation.inc.
  144.   $items['examples/ajax_example/add_more'] = array(
  145.     'title' => 'Add-more button (with graceful degradation)',
  146.     'page callback' => 'drupal_get_form',
  147.     'page arguments' => array('ajax_example_add_more'),
  148.     'access callback' => TRUE,
  149.     'file' => 'ajax_example_graceful_degradation.inc',
  150.     'weight' => 8,
  151.   );
  152.   // The above example as it appears to users with no javascript.
  153.   $items['examples/ajax_example/add_more_no_js'] = array(
  154.     'title' => 'Add-more button w/JS turned off',
  155.     'page callback' => 'drupal_get_form',
  156.     'page arguments' => array('ajax_example_add_more', TRUE),
  157.     'access callback' => TRUE,
  158.     'file' => 'ajax_example_graceful_degradation.inc',
  159.     'weight' => 8,
  160.   );
  161.  
  162.   // Use the AJAX framework outside the context of a form using the use-ajax
  163.   // class. See ajax_example_misc.inc.
  164.   $items['examples/ajax_example/ajax_link'] = array(
  165.     'title' => 'Ajax Link ("use-ajax" class)',
  166.     'page callback' => 'ajax_example_render_link',
  167.     'access callback' => TRUE,
  168.     'file' => 'ajax_example_misc.inc',
  169.     'weight' => 9,
  170.   );
  171.   // Use the AJAX framework outside the context of a form using a renderable
  172.   // array of type link with the #ajax property. See ajax_example_misc.inc.
  173.   $items['examples/ajax_example/ajax_link_renderable'] = array(
  174.     'title' => 'Ajax Link (Renderable Array)',
  175.     'page callback' => 'ajax_example_render_link_ra',
  176.     'access callback' => TRUE,
  177.     'file' => 'ajax_example_misc.inc',
  178.     'weight' => 9,
  179.   );
  180.   // A menu callback is required when using ajax outside of the Form API.
  181.   $items['ajax_link_callback'] = array(
  182.     'page callback' => 'ajax_link_response',
  183.     'access callback' => 'user_access',
  184.     'access arguments' => array('access content'),
  185.     'type' => MENU_CALLBACK,
  186.     'file' => 'ajax_example_misc.inc',
  187.   );
  188.  
  189.   // Use AJAX framework commands outside of the #ajax form property.
  190.   // See ajax_example_advanced.inc.
  191.   $items['examples/ajax_example/advanced_commands'] = array(
  192.     'title' => 'AJAX framework commands',
  193.     'page callback' => 'drupal_get_form',
  194.     'page arguments' => array('ajax_example_advanced_commands'),
  195.     'access callback' => TRUE,
  196.     'file' => 'ajax_example_advanced.inc',
  197.     'weight' => 10,
  198.   );
  199.  
  200.   return $items;
  201. }
  202.  
  203. function ajax_example_intro() {
  204.   $markup = t('The AJAX example module provides many examples of AJAX including forms, links, and AJAX commands.');
  205.   return $markup;
  206. }
  207.  
  208. /**
  209.  * Simple form whose ajax-enabled 'changethis' member causes a text change
  210.  * in the description of the 'replace_textfield' member.
  211.  * See @link http://drupal.org/node/262422 Form API Tutorial @endlink
  212.  */
  213. function ajax_example_simplest($form, &$form_state) {
  214.   $form = array();
  215.   $form['changethis'] = array(
  216.     '#title' => t("Choose something and explain why"),
  217.     '#type' => 'select',
  218.     '#options' => array(
  219.       'one' => 'one',
  220.       'two' => 'two',
  221.       'three' => 'three',
  222.     ),
  223.     '#ajax' => array(
  224.       // #ajax has two required keys: callback and wrapper.
  225.       // 'callback' is a function that will be called when this element changes.
  226.       'callback' => 'ajax_example_simplest_callback',
  227.       // 'wrapper' is the HTML id of the page element that will be replaced.
  228.       'wrapper' => 'replace_textfield_div',
  229.       // There are also several optional keys - see ajax_example_autocheckboxes
  230.       // below for details on 'method', 'effect' and 'speed' and
  231.       // ajax_example_dependent_dropdown for 'event'.
  232.      ),
  233.   );
  234.  
  235.   // This entire form element will be replaced whenever 'changethis' is updated.
  236.   $form['replace_textfield'] = array(
  237.     '#type' => 'textfield',
  238.     '#title' => t("Why"),
  239.     // The prefix/suffix provide the div that we're replacing, named by
  240.     // #ajax['wrapper'] above.
  241.     '#prefix' => '<div id="replace_textfield_div">',
  242.     '#suffix' => '</div>',
  243.   );
  244.  
  245.   // An AJAX request calls the form builder function for every change.
  246.   // We can change how we build the form based on $form_state.
  247.   if (!empty($form_state['values']['changethis'])) {
  248.     $form['replace_textfield']['#description'] = t("Say why you chose '@value'", array('@value' => $form_state['values']['changethis']));
  249.   }
  250.   return $form;
  251. }
  252.  
  253. /**
  254.  * Callback for ajax_example_simplest.
  255.  *
  256.  * On an ajax submit, the form builder function is called again, then the $form
  257.  * and $form_state are passed to this callback function so it can select which
  258.  * portion of the form to send on to the client.
  259.  *
  260.  * @return renderable array (the textfield element)
  261.  */
  262. function ajax_example_simplest_callback($form, $form_state) {
  263.   // The form has already been submitted and updated. We can return the replaced
  264.   // item as it is.
  265.   return $form['replace_textfield'];
  266. }
  267.  
  268. /**
  269.  * AJAX-enabled select element causes replacement of a set of checkboxes
  270.  * based on the selection.
  271.  */
  272. function ajax_example_autocheckboxes($form, &$form_state) {
  273.   // Since the form builder is called after every AJAX request, we rebuild
  274.   // the form based on $form_state.
  275.   $num_checkboxes = !empty($form_state['values']['howmany_select']) ? $form_state['values']['howmany_select'] : 1;
  276.  
  277.   $form['howmany_select'] = array(
  278.     '#title' => t('How many checkboxes do you want?'),
  279.     '#type' => 'select',
  280.     '#options' => array(1 => 1, 2 => 2, 3 => 3, 4 => 4),
  281.     '#default_value' => $num_checkboxes,
  282.     '#ajax' => array(
  283.       'callback' => 'ajax_example_autocheckboxes_callback',
  284.       'wrapper' => 'checkboxes-div',
  285.       //'method' defaults to replaceWith, but valid values also include
  286.       // append, prepend, before and after.
  287.       // 'method' => 'replaceWith',
  288.       // 'effect' defaults to none. Other valid values are 'fade' and 'slide'.
  289.       // See ajax_example_autotextfields for an example of 'fade'.
  290.       'effect' => 'slide',
  291.       // 'speed' defaults to 'slow'. You can also use 'fast'
  292.       // or a number of milliseconds for the animation to last.
  293.       // 'speed' => 'slow',
  294.       // Don't show any throbber...
  295.       'progress' => array('type' => 'none'),
  296.     ),
  297.   );
  298.  
  299.  
  300.   $form['checkboxes_fieldset'] = array(
  301.     '#title' => t("Generated Checkboxes"),
  302.     // The prefix/suffix provide the div that we're replacing, named by
  303.     // #ajax['wrapper'] above.
  304.     '#prefix' => '<div id="checkboxes-div">',
  305.     '#suffix' => '</div>',
  306.     '#type' => 'fieldset',
  307.     '#description' => t('This is where we get automatically generated checkboxes'),
  308.   );
  309.  
  310.   for ($i = 1; $i <= $num_checkboxes; $i++) {
  311.     $form['checkboxes_fieldset']["checkbox$i"] = array(
  312.       '#type' => 'checkbox',
  313.       '#title' => "Checkbox $i",
  314.     );
  315.   }
  316.  
  317.   $form['submit'] = array(
  318.     '#type' => 'submit',
  319.     '#value' => t('Submit'),
  320.   );
  321.  
  322.   return $form;
  323. }
  324.  
  325. /**
  326.  * Callback element needs only select the portion of the form to be updated.
  327.  * Since #ajax['callback'] return can be HTML or a renderable array (or an
  328.  * array of commands), we can just return a piece of the form.
  329.  * See @link ajax_example_advanced.inc AJAX Advanced Commands for more details
  330.  * on AJAX framework commands.
  331.  *
  332.  * @return renderable array (the checkboxes fieldset)
  333.  */
  334. function ajax_example_autocheckboxes_callback($form, $form_state) {
  335.   return $form['checkboxes_fieldset'];
  336. }
  337.  
  338.  
  339. /**
  340.  * Show/hide textfields based on AJAX-enabled checkbox clicks.
  341.  */
  342. function ajax_example_autotextfields($form, &$form_state) {
  343.  
  344.   $form['ask_first_name'] = array(
  345.     '#type' => 'checkbox',
  346.     '#title' => t('Ask me my first name'),
  347.     '#ajax' => array(
  348.       'callback' => 'ajax_example_autotextfields_callback',
  349.       'wrapper' => 'textfields',
  350.       'effect' => 'fade',
  351.     )
  352.   );
  353.   $form['ask_last_name'] = array(
  354.    '#type' => 'checkbox',
  355.    '#title' => t('Ask me my last name'),
  356.     '#ajax' => array(
  357.       'callback' => 'ajax_example_autotextfields_callback',
  358.       'wrapper' => 'textfields',
  359.       'effect' => 'fade',
  360.     ),
  361.   );
  362.  
  363.   $form['textfields'] = array(
  364.     '#title' => t("Generated text fields for first and last name"),
  365.     '#prefix' => '<div id="textfields">',
  366.     '#suffix' => '</div>',
  367.     '#type' => 'fieldset',
  368.     '#description' => t('This is where we put automatically generated textfields'),
  369.   );
  370.  
  371.   // Since checkboxes return TRUE or FALSE, we have to check that
  372.   // $form_state has been filled as well as what it contains.
  373.   if (!empty($form_state['values']['ask_first_name']) && $form_state['values']['ask_first_name']) {
  374.     $form['textfields']['first_name'] = array(
  375.       '#type' => 'textfield',
  376.       '#title' => t('First Name'),
  377.     );
  378.   }
  379.   if (!empty($form_state['values']['ask_last_name']) && $form_state['values']['ask_last_name']) {
  380.     $form['textfields']['last_name'] = array(
  381.       '#type' => 'textfield',
  382.       '#title' => t('Last Name'),
  383.     );
  384.   }
  385.  
  386.   $form['submit'] = array(
  387.     '#type' => 'submit',
  388.     '#value' => t('Click Me'),
  389.   );
  390.  
  391.   return $form;
  392. }
  393.  
  394. /**
  395.  * Selects the piece of the form we want to use as replacement text and returns
  396.  * it as a form (renderable array).
  397.  *
  398.  * @return renderable array (the textfields element)
  399.  */
  400. function ajax_example_autotextfields_callback($form, $form_state) {
  401.   return $form['textfields'];
  402. }
  403.  
  404.  
  405. /**
  406.  * A very basic form which with an AJAX-enabled submit.
  407.  *
  408.  * On submit, the markup in the #markup element is updated.
  409.  */
  410. function ajax_example_submit_driven_ajax($form, &$form_state) {
  411.   $form['box'] = array(
  412.     '#type' => 'markup',
  413.     '#prefix' => '<div id="box">',
  414.     '#suffix' => '</div>',
  415.     '#markup' => '<h1>Initial markup for box</h1>',
  416.   );
  417.  
  418.   $form['submit'] = array(
  419.     '#type' => 'submit',
  420.     '#ajax' => array(
  421.       'callback' => 'ajax_example_submit_driven_callback',
  422.       'wrapper' => 'box',
  423.       'name' => 'submit1',
  424.     ),
  425.     '#value' => t('Submit'),
  426.   );
  427.  
  428.   return $form;
  429. }
  430.  
  431. /**
  432.  * Select the 'box' element, change the markup in it, and return it as a
  433.  * renderable array.
  434.  *
  435.  * @return renderable array (the box element)
  436.  */
  437. function ajax_example_submit_driven_callback($form, $form_state) {
  438.   // In most cases, it is recomended that you put this logic in form generation
  439.   // rather than the callback. Submit driven forms are an exception, because
  440.   // you may not want to return the form at all.
  441.   $element = $form['box'];
  442.   $element['#markup'] = "Clicked submit ({$form_state['values']['op']}): " . date('c');
  443.   return $element;
  444. }
  445.  
  446.  
  447. /**
  448.  * A form with a dropdown whose options are dependent on a
  449.  * choice made in a previous dropdown.
  450.  *
  451.  * On changing the first dropdown, the options in the second
  452.  * are updated.
  453.  */
  454. function ajax_example_dependent_dropdown($form, &$form_state) {
  455.   // Get the list of options to populate the first dropdown.
  456.   $options_first = _ajax_example_get_first_dropdown_options();
  457.   // If we have a value for the first dropdown from $form_state['values'] we use
  458.   // this both as the default value for the first dropdown and also as a
  459.   // parameter to pass to the function that retrieves the options for the
  460.   // second dropdown.
  461.   $selected = isset($form_state['values']['dropdown_first']) ? $form_state['values']['dropdown_first'] : key($options_first);
  462.  
  463.   $form['dropdown_first'] = array(
  464.     '#type' => 'select',
  465.     '#title' => 'Instrument Type',
  466.     '#options' => $options_first,
  467.     '#default_value' => $selected,
  468.     // Bind an ajax callback to the change event (which is the default for the
  469.     // select form type) of the first dropdown. It will replace the second
  470.     // dropdown when rebuilt
  471.     '#ajax' => array(
  472.       // When 'event' occurs, Drupal will perform an ajax request in the
  473.       // background. Usually the default value is sufficient (eg. change for
  474.       // select elements), but valid values include any jQuery event,
  475.       // most notably 'mousedown', 'blur', and 'submit'.
  476.       // 'event' => 'change',
  477.       'callback' => 'ajax_example_dependent_dropdown_callback',
  478.       'wrapper' => 'dropdown-second-replace',
  479.     ),
  480.   );
  481.  
  482.   $form['dropdown_second'] = array(
  483.     '#type' => 'select',
  484.     '#title' => $options_first[$selected] . ' ' . t('Instruments'),
  485.     // The entire enclosing div created here gets replaced when dropdown_first
  486.     // is changed.
  487.     '#prefix' => '<div id="dropdown-second-replace">',
  488.     '#suffix' => '</div>',
  489.     // when the form is rebuilt during ajax processing, the $selected variable
  490.     // will now have the new value and so the options will change
  491.     '#options' => _ajax_example_get_second_dropdown_options($selected),
  492.     '#default_value' => isset($form_state['values']['dropdown_second']) ? $form_state['values']['dropdown_second'] : '',
  493.   );
  494.  
  495.  
  496.   $form['submit'] = array(
  497.     '#type' => 'submit',
  498.     '#value' => t('Submit'),
  499.   );
  500.   return $form;
  501. }
  502.  
  503. /**
  504.  * Selects just the second dropdown to be returned for re-rendering
  505.  *
  506.  * Since the controlling logic for populating the form is in the form builder
  507.  * function, all we do here is select the element and return it to be updated.
  508.  *
  509.  * @return renderable array (the second dropdown)
  510.  */
  511. function ajax_example_dependent_dropdown_callback($form, $form_state) {
  512.   return $form['dropdown_second'];
  513. }
  514.  
  515. /**
  516.  * Helper function to populate the first dropdown. This would normally be
  517.  * pulling data from the database.
  518.  *
  519.  * @return array of options
  520.  */
  521. function _ajax_example_get_first_dropdown_options() {
  522.   // drupal_map_assoc() just makes an array('String' => 'String'...).
  523.   return drupal_map_assoc(array(t('String'), t('Woodwind'), t('Brass'), t('Percussion')));
  524. }
  525.  
  526. /**
  527.  * Helper function to populate the second dropdown. This would normally be
  528.  * pulling data from the database.
  529.  *
  530.  * @param $key
  531.  *   This will determine which set of options is returned.
  532.  *
  533.  * @return array of options
  534.  */
  535. function _ajax_example_get_second_dropdown_options($key = '') {
  536.   $options = array(
  537.     t('String') => drupal_map_assoc(array(t('Violin'), t('Viola'), t('Cello'), t('Double Bass'))),
  538.     t('Woodwind') => drupal_map_assoc(array(t('Flute'), t('Clarinet'), t('Oboe'), t('Bassoon'))),
  539.     t('Brass') => drupal_map_assoc(array(t('Trumpet'), t('Trombone'), t('French Horn'), t('Euphonium'))),
  540.     t('Percussion') => drupal_map_assoc(array(t('Bass Drum'), t('Timpani'), t('Snare Drum'), t('Tambourine'))),
  541.   );
  542.   if (isset($options[$key])) {
  543.     return $options[$key];
  544.   }
  545.   else {
  546.     return array();
  547.   }
  548. }
  549. /**
  550.  * @} End of "defgroup ajax_example".
  551.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement