0

I have added some extra options in the My Profile page which users can edit. But now I have to track down the change of this value...

It's a checkbox and whenever the user (un)checks this box, something must happen. So I thought this would be in the $form_state, but no luck there.

Next thing I did was to preform an EntityQuery on the user object and try to cross-reference it with the input, but again, no luck.

So how can I (programmatically) detect if a form-element goes from unchecked to checked or the other way around?

EDIT: What I try to do is an implementation with Campaign Monitor (a newsletter plugin). If a user has changed it's checkbox, his subscription must be updated (of leave it as it is when there is no change)

4
  • what changes you want to do on form element change? An overview would do. Commented Jan 30, 2013 at 8:59
  • An overview is necessary, But maybe Rules can help you a bit.
    – Vic
    Commented Jan 30, 2013 at 9:02
  • @subhojit777, does my edit make it a bit more clearer?
    – Michiel
    Commented Jan 30, 2013 at 9:03
  • @Vic, what do you mean by 'overview'?
    – Michiel
    Commented Jan 30, 2013 at 9:05

2 Answers 2

0

When you change something in form, that change can be found in $form_state. You need to add an AJAX callback and do your changes in that callback.

Sample code:

function MYMODULE_form_alter(&$form, $form_state, $form_id) {
  switch ($form_id) {
    case 'MY_FORM_ID':
      $form['field_select']['#ajax'] = array(
        'event' => 'change',
        'callback' => 'your_ajax_callback',
        'progress' => array('type' => 'none'),
      );
    break;
  }
}

/**
 * AJAX Callback.
 */
function your_ajax_callback(&$form, &$form_state) {
  // dpm($form_state);
  // Check $form_state here and update subscription.
}

Hope this helps!

10
  • Ok, thanks for the effort. And how can I check if a value has changed? Compare $form and $form_state? Is the 'field_select' in your $form-variable, the field it applies to?
    – Michiel
    Commented Jan 30, 2013 at 12:02
  • @Michiel you will get the changed value in $form_state. Inside $form_state['values']['field_select'] (I guess) you will get the changed value. I have did a dpm($form_state) inside the AJAX callback Commented Jan 30, 2013 at 12:30
  • The field is called $form['field_newsletter'], but any dpm or die('yes') doesn't show up, although I changed my value. What do I do wrong?
    – Michiel
    Commented Jan 30, 2013 at 12:40
  • Ok! I managed to do so, but Ajax isn't an option now I saw it working... The actions should happen invisible and not causing any loading of content... Is there an other option for adding a callback besides Ajax?
    – Michiel
    Commented Jan 30, 2013 at 12:48
  • @Michiel since the dpm is in ajax callback so you have to refresh the page to see dpm results. I guess you have not refreshed the page.. Commented Jan 30, 2013 at 12:49
0

I have had a similar issue and I haven't found any easy answer. My solution was to create a "consistency check" function triggered after editing content, which would compare the values provided in the form with how things are actually set-up. If the values were inconsistent with the set-up I would know that they were changed.

In other words, whenever you edit the profile set things up whether they were changed or not. In case of editing a profile it will not be too heavy for the web-page.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.