Skip to content

Instantly share code, notes, and snippets.

@netlooker
Last active September 24, 2015 12:06
Show Gist options
  • Save netlooker/00f689e19151e150322c to your computer and use it in GitHub Desktop.
Save netlooker/00f689e19151e150322c to your computer and use it in GitHub Desktop.
Drupal 7 - Form API - Populating field value by ajax request via ajax_command
<?php
/**
* Implements hook_form_FORM_ID_alter().
*/
function hook_form_id_form_alter(&$form, &$form_state) {
$form['field_text_1']['#prefix'] = "<div id='text-1-value'>";
$form['field_text_1']['#sufix'] = "</div>";
$form['submit'] = array(
'#type' => 'button',
'#value' => t('Search'),
'#ajax' => array(
'callback' => 'update_field',
'wrapper' => 'text-1-value',
'method' => 'replace',
'effect' => 'fade',
),
);
return $form;
}
function update_field($form, &$form_state) {
$form['field_text_1']['und'][0]['value']['#value'] = 'BAM';
return array(
'#type' => 'ajax',
'#commands' => array(
ajax_command_replace("#text-1-value", render($form['field_text_1'])),
),
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment