2

we're using drupal and I have setup an ajax callback on a form item. The form item is a radio button with two options on/off. also attached to the radio button is some js to toggle a class. The class will toggle correctly as long as the callback is commented out

Here is a paste of my code:

// Making a call to web service and getting value to determine class

<?php
if (($user_alert['endDate'] == $today) || ($user_alert['endDate'] > $today) ) {

$activeFlagClass = $user_alert['activeFlag'];
$active_flag_class = get_active_flag_class($activeFlagClass);

$alerts[] = array(
  'activFlag' => $user_alert['activeFlag'],
  'activeFlagClass' => $active_flag_class,
  'adhMedicationAlertId' => $user_alert['adhMedicationAlertId'],
  );
}

// This is the theme template where I am printing the class and calling a form

<?php foreach($alerts as $key=>$value): ?>
    <div class="list-alerts-group">
    <!-- printing the php class here -->
      <div class="list-alerts-item <?php print $value['activeFlagClass']; ?>">
      <!-- There are list of variables here including calling a form -->
        <div class="alerts-item alert-active-flag-form-wrapper">
        <?php
        $active = $value['activeFlag'];
        $alertId = $value['adhMedicationAlertId'];
        $alertsActiveFlag = drupal_get_form('innovicares_alerts_active_flag_form', $active, $alertId);
        print drupal_render($alertsActiveFlag);
        ?>
        </div>
      </div>

    </div>
<?php endforeach ?>

// This the form with attached js for toggleClass and attached #ajax callback

<?php
function innovicares_alerts_active_flag_form($form, &$form_state, $active, $alertId) {
   // Init variables
  $active_flag = '';
  $alert_id = '';

  // Let's define the default value for $active
  if ($active == 'true') {
    $active_flag = 1;
  }

  if ($active == 'false') {
    $active_flag = 0;
  }

  // Toggles alert form based on $active
  $form['#attached']['js'] = array(
    drupal_get_path('module', 'innovicares_alerts') . '/js/alerts.js',
  );

  // krumo($active_flag);

  $form['alert']['active_flag'] = array(
    '#type' => 'radios',
    '#attributes' => array(
      'class' => array(
        'alert-active-flag-radio'
        ),
      ),
    // Get default from callback
    '#default_value' => $active_flag,
    '#options' => array(
      1 => t('ON'), 
      0 => t('OFF')
      ),
    '#ajax' => array(
      'callback' => 'set_active_flag',
      'event' => 'click',
      'progress' => array('type' => 'none'),
      ),
  );

  // Let's define the alert id
  $alert_id = $alertId;

  $form['alert']['alert_id'] = array(
    '#type' => 'hidden',
    '#default_value' => $alert_id,
  );  

  // Return the form
  return $form;
}

// This is the #ajax callback

function set_active_flag($form, &$form_state) {
  // Init variables
  $current_active_flag ='';
  $active_flag = '';

  $alertId = $form_state['values']['alert_id'];

  $current_active_flag = $form_state['values']['active_flag'];

  if ($current_active_flag == 1) {
    $active_flag = deactivate_active_flag($alertId);
    krumo($active_flag);
  }

  elseif ($current_active_flag == 0) {
    $active_flag = activate_active_flag($alertId);
    // krumo($active_flag['data']['activeFlag']);
    // This is coming back as false

    // $_SERVER['PHP_SELF'];

    return $active_flag;

  }
}

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.