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.