0

I want to set the value of a textfield based upon the selection of a field above. You can choose a template and the textfield would have it as a value.

This is the select field used to select the template with the ajax call

// Template Select field
$form['template'] = [
'#type' => 'select',
'#title' => $this->t('Template'),
'#options' => $event_templates_select,
'#required' => TRUE,
'#ajax' => [
    'callback' => '::changeOutputValue',
    'disable-refocus' => FALSE,
    element.
        'event' => 'change',
        'wrapper' => 'edit-output',
        'progress' =>[
            'type' => 'throbber',
            'message' => $this->t('Updating e-mail template'),
        ],
    ]
];

This is the textfield that I want to display the value. It's in a ckeditor.

// Textarea field
$form['output'] = [
'#type' => 'text_format',
'#format' => 'full_html',
'#required' => TRUE,
'#disabled' => FALSE,
'#value' => 'hallo',
'#prefix' => '<div id="edit-output" class="acceptform__output">',
'#suffix' => '</div>',
];

And finally, the ajax function

  public function changeOutputValue(array &$form, FormStateInterface $form_state) {

    $selected_option =  $form_state->getValue('template');
    if ($selected_option == 0){
        $output_text = '';
    } else {
        $template = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->load($selected_option);
        $tempate_content = $template->get('field_template')->getValue();
        $tempate_content = $tempate_content[0]['value'];
        $output_text = $tempate_content;
    }


    $form['output']['#value'] = $output_text;

    $response = new AjaxResponse();
    return $response;

  }   
}

Can someone explain what I'm doing wrong. I was trying to follow the drupal 8 form api docs but I can't get it to work with a ckeditor field.

The select field contains a number of e-mail templates. Upon selecting one, I want to display the template in the output field with a ckeditor so the user can still make changes if necessary.

I'm using Drupal 8 with a custom module.

Thanks in advance.

New contributor
SR Latched is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
  • This is the wrong approach. First try to rebuild the form without ajax. Build all form elements, also the ones which are added in a later rebuild, in the form build. When this is working add the ajax callback, which then only needs one statement, to return the changed part of the form in a wrapper. – 4k4 Mar 25 at 13:08
  • Hey, thanks for helping! The form works fine and I've edited the ajax function but it's still not updating the field value. I'm also not really sure what you mean with a wrapper. – SR Latched Mar 25 at 16:10
  • Returning the entire or part of $form in a div wrapper is the standard ajax pattern, see the examples in Examples for Developers. – 4k4 Mar 25 at 17:06
0

From the ajax callback, you only need to return the render array containing the data to be inserted into the div you have specified as the callback to your #ajax.

So in the ajax callback you can return this, rather than an AjaxResponse:

return $form['output'];
  • Thanks! This works but only in a textarea. I'm trying to show the output in a ` '#type' => 'text_format '` so that I can use the ckeditor. – SR Latched Mar 26 at 9:28
  • No, it will work for any render array elements, not just textareas. Whatever part of the form render array you return will be rendered and inserted into the element with the ID you've specified as the #ajax wrapper. It doesn't matter what the type of the render array element is. – Jaypan Mar 26 at 22:27

Your Answer

SR Latched is a new contributor. Be nice, and check out our Code of Conduct.

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.