0

I have created a simple content type "reservation" with an integer field called "field_km" with a default value of 50.
On the node creation form, I want to have a markup field showing the value of the "field_km" field. So, I have created a simple module with the following code:

/**
 * @file
 * Drupal Module : myresa
 * Adds custom code specific to this Drupal 7 site.
 */

function myresa_form_reservation_node_form_alter(&$form, &$form_state, $form_id) {
  $form[‘box’] = array(
    ‘#type’ => ‘markup’,
    ‘#prefix’ => ‘’,
    ‘#suffix’ => ‘’,
    ‘#markup’ => ‘Price not computed',
  );

  $form[‘mybtn’] = array(
    ‘#type’ => ‘button’,
    ‘#id’ => ‘mybtn’,
    ‘#ajax’ => array(
      ‘callback’ => ‘myresa_callback’,
      ‘wrapper’ => ‘box’,
      ‘name’ => ‘mybtn1’,
    ),
    ‘#value’ => t(‘Compute the price’),
  );
}

function myresa_callback($form, $form_state) {
  $element = $form[‘box’] ;
  $element[‘#markup’] = «The price for » . $form_element[‘field_km’][‘#default_value’] . » km is 35 euros» ;
  return $element ;
}

The form shows the 'box' markup, the button, and when clicked the button activates the callback and shows the wanted text, but not the value of 'field_km'. It is probably a simple syntax mistake, but it beats me.

3 Answers 3

2
function myresa_callback($form, $form_state) {
  $element = $form[‘box’] ;
  $element[‘#markup’] = «The price for » . $form_element

No wonder this doesn't work, where does $form_element come from? Also fields tend to be structured like $form[field name][$node->language][columname] (the latter being value in your case IMO)

0

I am not satisfied with your code, I am making changes according to my experience.

function myresa_form_reservation_node_form_alter(&$form, &$form_state, $form_id) {
 $form[‘box’] = array(
  ‘#prefix’ => ‘<div id="box-val">’,
  ‘#suffix’ => ‘</div>’,
  ‘#value’ => ‘Price not computed',
);

$form[‘mybtn’] = array(
  ‘#type’ => ‘button’,
  ‘#id’ => ‘mybtn’,
  ‘#ajax’ => array(
    ‘callback’ => ‘myresa_callback’,
    ‘wrapper’ => ‘box-val’,
    ‘name’ => ‘mybtn1’,
    ),
  ‘#value’ => t(‘Compute the price’),
  );
}

function myresa_callback($form, $form_state) {
  $element = $form[‘box’] ;
  $element[‘#value’] = "The price for" . $form_state['values']['field_km'] . " km is 35 euros" ;
  return $element ;
}
1
  • Ok guys, thanks to both your inputs I have found the way:
    – Supertramp
    Jun 9, 2011 at 17:16
0

As I was going to say, I have found the answer thanks to you both.

The correct adressing of the field value in my case is :

$form_state['values']['field_km']['und']['0']['value']

Meaning that calling $form['field_km']['und']['0']['value'] as suggested by CHX renders "Array" and $form_state['values']['field_km'] as suggested by SHOAIB renders also "Array" !!! The Array result put me on the track, as I have printed the whole form render using a preprint function. I noticed that it was the first value after the declaration of the field, and that then it cascaded down to its value through the language 'und', then instance '0', and finally 'value'.

Hope it helps somebody out there in outer space ;-)) Thanks a lot to you both anyway -

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.