2

I have a custom form in Drupal that ties into Entity-generating code. In essence, there's a button which should return an ideal Entity state (determined in a method that returns true or false).

The code below fires the method in question, but even if it gets a return value from the method, the value of the button never changes.

Am I overlooking something as simple as returning a new #value in the AJAX callback?

function example_mark_block_form($form, $form_state) {
  $form = array();

  $is_marked = _example_is_user_marked();

  $form['mark-changes'] = array(
    '#type' => "button",
    '#id' => "mark-changes",
    '#default_value' => t($is_marked),
    '#prefix' => "<div class='mark-wrapper'>",
    '#suffix' => "</div>",
    '#ajax' => array(
      'callback' => 'example_mark_follow_ajax_response',
      'wrapper' => 'mark-wrapper',
      'method' => 'replace',
    ),
  );

  return $form;
}

/**
 * AJAX callback for the follow response
 */
function example_mark_follow_ajax_response($form, $form_state) {
  global $user;

  return $form['mark-changes'];
}

1 Answer 1

3

From the FAPI Reference:

#ajax['wrapper']

This property defines the HTML id attribute of an element on the page which will be replaced by the html returned by the #ajax['path'] or #ajax['callback'] function. Usually, a div element is used as the wrapper, as it provides the most flexibility for placement of elements before, after, or inside of its HTML tags. This property is required for using AJAX requests in on a form element. Note that the entire element with this ID is replaced, not just the contents of the element.

So this:

'#prefix' => "<div class='mark-wrapper'>",

Should be:

'#prefix' => "<div id='mark-wrapper'>",

Also buttons don't have a #default_value, so replace:

'#default_value' => t($is_marked),

with:

'#value' => t('@value', array('@value' => $is_marked)),

And you should be on track (the change to t() is just for sanitisation, see Translating Variables).

Your Answer

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

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