0

I am starting to work with webform in drupal 8 and I need to show values on the second page of the webform wizard, I have reviewed several solutions but I cannot do it. I tried using $ form_state-> setValue

function webform_certificadoemail_webform_element_alter(array &$element, FormStateInterface $form_state, array $context) {

  if ($form_state->get('current_page') == 'preview') {

    $value_entered = $form_state->getValue('nombre');
    drupal_set_message('Valor del nombre en el preview =:'.$value_entered); 

     $form_state->setValue('valor','This value is .......'); // This not work 
  }
}
6
  • Does your message show up? If not, you should use if ($form['progress']['#current_page'] == 'preview') { – No Sssweat Nov 19 '19 at 20:02
  • Thanks for replying, the message in drupal_set_message if it appears, but I can't update is the value field using the $ form_state-> setValue method ('value', 'This value is .......'); – Francisco Danubio Salas Rosett Nov 20 '19 at 13:48
  • Are you trying to set a default value? Does the valor field appear in the preview step? – No Sssweat Nov 20 '19 at 13:53
  • I explain my problem better, on the first page of the assistant I enter name, identity, etc. on the second page the data stored in postgresql should appear, I was reviewing examples and they use the method $ form_state-> setValue, where the name of the variable and the value in database are established. But when I go to the second page, those results are not displayed. Test with text field and compose token to display the result – Francisco Danubio Salas Rosett Nov 20 '19 at 14:01
  • So the value of "valor" is entered via textfield in step 1? – No Sssweat Nov 20 '19 at 14:08
0

When you are in the preview page, you're too late then.

You need to set this in your previous step in a validate function

function webform_certificadoemail_webform_element_alter(array &$element, FormStateInterface $form_state, array $context) {

   if ($form_state->get('current_page') == 'step_1') { // or whatever you called the first page
     $element['#element_validate'][] = 'webform_certificadoemail_set_valor';
  }

}

function webform_certificadoemail_set_valor($element, FormStateInterface $form_state) {
   $form_state->setValue('valor','This value is .......');
}
2
  • I was checking your code but it didn't work out, I don't know what I'm doing wrong. webform_certificadoemail_webform_element_alter(array &$element, FormStateInterface $form_state, array $context) { if ($form_state->get('current_page') == 'datos_de_solicitud') { // First page name $element['#element_validate'][] = 'webform_certificadoemail_set_valor'; } } function webform_certificadoemail_set_valor($element, FormStateInterface $form_state) { $form_state->setValue('texto','This value is .......'); // "texto", text field key, "This value is ......." ,content of the field } – Francisco Danubio Salas Rosett Nov 20 '19 at 14:58
  • Thank you very much, No Sssweat, check now again your code and now update the value, now what I have to do is place the code in another hook, because what I had is when an element of the webform was altered – Francisco Danubio Salas Rosett Nov 20 '19 at 15:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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