CommentFileSizeAuthor
#3 drupal-context-condition-ajax.jpg11.46 KBwesleymusgrove

Comments

recidive’s picture

Status: Active » Closed (fixed)

Just got around this by returning a simple array with the context as return for condition_form() and did everything else in a hook_form_alter().

dobe’s picture

I did the same thing for the context reactions. A demonstration of this can be found in the Chardin module.

wesleymusgrove’s picture

Issue summary: View changes
Status: Closed (fixed) » Active
StatusFileSize
new11.46 KB

I spent some time struggling with this very issue. Other than what was provided in the Chardin module, I could barely find any other documentation for needing to refresh portions of a context condition form via Ajax whatsoever. I referenced the Chardin module, plugins from the Context contributed module, and the simple Ajax-enabled forms example here: https://www.drupal.org/node/752056.

I couldn't get this ajax functionality to work using the typical condition_values(), condition_form(), or options_form() functions, which is why I went forward with adapting the hook_form_FORM_ID_alter() as seen in the Chardin module.

I had to put the howmany_select and the checkboxes_fieldset inside the $form['conditions']['plugins']['example_condition']['values'] array, which mimicked the way values get stored in the form if you do it the traditional way using condition_values(), condition_form() functions.

NOTE: I don't know why, but the condition_form_submit($values) function was still required in order to correctly submit and save values set in example_form_ctools_export_ui_edit_item_form_alter. Otherwise $form_state['item']->conditions['example_condition']['values'] did not contain the correct values.

<?php
function example_form_ctools_export_ui_edit_item_form_alter(&$form, &$form_state, $form_id) {      
  //Make sure we are on the correct ctools form
  if($form_id == 'ctools_export_ui_edit_item_form') {

    $defaults = !empty($form_state['item']->conditions['example_condition']['values']) ? $form_state['item']->conditions['example_condition']['values'] : array();

    $form['conditions']['plugins']['example_condition']['values'] = array(
      '#tree' => TRUE,
    );

    $form['conditions']['plugins']['example_condition']['values']['howmany_select'] = array(
      '#title' => t('How many checkboxes do you want?'),
      '#type' => 'select',
      '#options' => array(1 => 1, 2 => 2, 3 => 3, 4 => 4),
      '#default_value' => $defaults['howmany_select'],
      '#ajax' => array(
        'callback' => '_ajax_example_autocheckboxes_callback',
        'wrapper' => 'checkboxes-div',
        'method' => 'replace',
        'effect' => 'fade',
      ),
    );

    $form['conditions']['plugins']['example_condition']['values']['checkboxes_fieldset'] = array(
      '#title' => t("Generated Checkboxes"),
      // The prefix/suffix provide the div that we're replacing, named by #ajax['wrapper'] above.
      '#prefix' => '<div id="checkboxes-div">',
      '#suffix' => '</div>',
      '#type' => 'fieldset',
      '#description' => t('This is where we get automatically generated checkboxes'),
    );

    $num_checkboxes = !empty($defaults['howmany_select']) ? $defaults['howmany_select'] : 1;
    
    for ($i = 1; $i <= $num_checkboxes; $i++) {
      $form['conditions']['plugins']['example_condition']['values']['checkboxes_fieldset']["checkbox$i"] = array(
        '#type' => 'checkbox',
        '#title' => "Checkbox $i",
        '#default_value' => $defaults['checkboxes_fieldset']["checkbox$i"],
      );
    }
    
    return $form;
  }
}

function _ajax_example_autocheckboxes_callback($form, $form_state) {
  return $form['conditions']['plugins']['example_condition']['values']['checkboxes_fieldset'];
}

function condition_form_submit($values) {
  return $values;
}
?>
paulocs’s picture

Thanks for the explanation @wesleymusgrove.

I'm closing this issue.

paulocs’s picture

Status: Active » Fixed
paulocs’s picture

Status: Fixed » Closed (fixed)