Search code examples
ckeditordrupal-8

ckeditor in Drupal 8 custom module


I am currently developing a custom module for Drupal 8. While adding a backend form to get some data from users I tried to get the ckeditor configured to replace my textareas... and failed >.<

here is the form definition:

    $form['text'] = array(
        '#type' => 'textarea',
        '#title' => t('Text'),
        '#required' => TRUE,
        '#attributes' => array(
            'id' => 'text',
            'style' => 'max-width: 650px'
        ),
        '#default_value' => $data['text']
    );

where do I need to load the ckeditor to replace my textarea?

things I already tried:

$build['#attached'] = array(
        'js' => array(
          drupal_get_path('module', 'ckeditor') . '/js/ckeditor.js'
drupal_render($build);

and

drupal_load_library("ckeditor", "ckeditor");

but I can't load all the dependencies


Solution

  • Refer to the Drupal 8 form API which has a text_format form type. A check on your #default_value is also recommended.

    Navigate to Configuration > Content authoring > Text formats and editors giving you an overview of the current available text formats. The ones listed here can be used in the #format proprty of your form field. (Default fallback currently is basic_html)

    Try to change your code to:

    $form['text'] = array(
        '#type' => 'text_format',
        '#title' => t('Text'),
        '#required' => TRUE,
        '#default_value' => isset($data['text']) ? $data['text'] : '',
        '#format' => 'full_html',
    );