1

I have configuration form for a custom module.

namespace Drupal\amu_http_status_code_display\Form;

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;

class AmuHttpStatusCodeDisplayConfigForm extends ConfigFormBase {

  public function getFormId() {
    return 'AmuHttpStatusCodeDisplayConfigForm';
  }


  public function buildForm(array $form, FormStateInterface $form_state) {

    $config = $this->config('amustatuscode.settings');  
    $form = parent::buildForm($form, $form_state);

    $form['404_message'] = array(
      '#type' => 'textarea',
      '#title' => t('404 error message'),
      '#description' => t('error 404 message.'),
      '#default_value' => '404 error',
    );

    $form['403_message'] = array(
      '#type' => 'textarea',
      '#title' => t('403 error message'),
      '#description' => t('error 403 message.'),
      '#default_value' => '403 error',
    );

    return $form;
  }

  public function submitForm(array &$form, FormStateInterface $form_state) {
    $config = $this->config('amustatuscode.settings');
    $config->set('404_message', $form_state->getValue('404_message'));
    $config->set('403_message', $form_state->getValue('403_message'));
    $config->save();

    return parent::submitForm($form, $form_state);
  }

  public function getEditableConfigNames() {
    return ['amustatuscode.settings'];
  }

}

How can I make the form field I add use a WYSIWYG editor?

2 Answers 2

4

You want to use the TextFormat form element: https://api.drupal.org/api/drupal/core%21modules%21filter%21src%21Element%21TextFormat.php/class/TextFormat/8.4.x

Note that this will give you a value and a format in a nested form structure, you need to store both (or lock to a specific format) and then render the content with that format to avoid security issues.

1
  • I'm not understanding how TextFormat is being used. Can you provide an example?
    – AlxVallejo
    Sep 28, 2020 at 14:54
1

Like so:

    $config = $this->config('amustatuscode.settings');
    $form = parent::buildForm($form, $form_state);

    $form['404_message'] = array(
      '#type' => 'text_format',
      '#title' => t('404 error message'),
      '#description' => t('error 404 message.'),
      '#default_value' => $config->get('404_message'),
      '#format' => $config->get('404_message_format'),
    );

    $form['403_message'] = array(
      '#type' => 'text_format',
      '#title' => t('403 error message'),
      '#description' => t('error 403 message.'),
      '#default_value' => $config->get('403_message'),
      '#format' => $config->get('403_message_format'),
    );

    return $form;

  }

  public function submitForm(array &$form, FormStateInterface $form_state) {
    $config = $this->config('amustatuscode.settings');
    $values = $form_state->getValues();
    $config->set('404_message', $values['404_message']['value']);
    $config->set('404_message_format', $values['404_message']['format']);
    $config->set('403_message', $values['403_message']['value']);
    $config->set('403_message_format', $values['403_message']['format']);
    $config->save();

    return parent::submitForm($form, $form_state);
  }

  public function getEditableConfigNames() {
    return ['amustatuscode.settings'];
  }

Please note this requires a change to your amustatuscode.settings schema definition to include the additional format string

2
  • Hi. I just followed your instructions to use text_format. Thank you. You then mentioned a required change to amustatuscode.settings. I don't know what that means. What I'm using inside ` public function blockForm($form, FormStateInterface $form_state) {...}` is $config = $this->getConfiguration();. I then have public function blockSubmit($form, FormStateInterface $form_state) {...} and public function build() {...}, and $config is not in either of them. Thus, cannot save what is in the text_format fields. it just says Array.
    – Pegues
    Sep 26, 2021 at 23:10
  • 1
    amustatuscode.settings is from the OP, not a requirement for text_format. You can store the data however you like. The important part to note is that the values will be in an associative array with keys value and format, rather than a scalar value like e.g. a text_field would be Sep 28, 2021 at 13:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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