7

I'm using the CKEditor module, and I would like to have the CKEditor in my custom textarea. I tried hard but it just doesn't work...

This some of the code I tried:

    $form['body'] = array (
        '#type' => 'textarea',
        '#title' => 'Item Snippet',
        '#default_value' => $body,
        '#cols' => 80,
        '#rows' => 7
    );
    $form['']['format'] = filter_form();
2
  • As side note, $form['']['format'] should probably be $form['body']['format'].
    – apaderno
    Jun 24, 2012 at 12:16
  • CCKeditors are easily configurable through admin panel, no need of code modification
    – GoodSp33d
    Jun 24, 2012 at 12:37

7 Answers 7

14

Use the text_format element type and CKEditor will catch your field:

  $form['body'] = array(
    '#type' => 'text_format',
    '#title' => t('Body'),
    '#format'=> 'full_html',
  );

If you have issues with showing CKEditor in popup use additional #pre_render:

  $form['body'] = array(
    '#type' => 'text_format',
    '#title' => t('Body'),
    '#format'=> 'full_html',
    '#pre_render' => array('ckeditor_pre_render_text_format'),
  );
1
  • It worked. How I can attach a field to my form? For example, I have created a user field under account settings. The field name is field_oembed_link I will show the uploaded link with this field in views. This way I will be able to create a list of content containing the uploaded links. Just for extra information, I want to allow users to upload a link, which will be converted to an oembed content automatically.
    – Umair
    May 25, 2017 at 15:45
5

u need to use "text_format" instead of "textarea"

text_format

Description: A text-format-enabled version of a textarea.

3
  • 1
    This is the correct answer Mar 2, 2015 at 20:28
  • @exlord It worked. How I can attach a field to my form? For example, I have created a user field under account settings. The field name is field_oembed_link I will show the uploaded link with this field in views. This way I will be able to create a list of content containing the uploaded links. Just for extra information, I want to allow users to upload a link, which will be converted to an oembed content automatically.
    – Umair
    May 25, 2017 at 15:45
  • @Umair Don't use comment to ask a new question. Create a new Question. And u gana need to provide a sample and more clear explanation about what you want ...
    – Exlord
    May 27, 2017 at 6:03
3

In Drupal 8.6.x my solution was:

$form['Description'] = [
  '#type' => 'text_format',
  '#title' => $this->t('Description'),
  '#format' => 'rich_text',
];
2

just in case somebody only wants a basic_html format one and does not want show format selection ('#allowed_formats' => ['basic_html'] is the point),

$form['description'] = [
  '#type' => 'text_format',
  '#title' => $this->t('message'),
  '#format' => "basic_html",
  '#allowed_formats' => ['basic_html'],
  '#required' => TRUE,
];
0

You can try this

 $form['body'] = array(
   '#type' => 'textarea',
   '#title' => 'Item Snippet',
   '#default_value' => $body,
   '#cols' => 80,
   '#rows' => 7,
   '#wysiwyg' => TRUE, // just add this and you'll get the CKeditor 
 );
 // …

I was facing the same problem; when I added '#wysiwyg' => TRUE, it worked.

0

Some of the methods discussed above do get the CKEditor in the textarea, but if it is a configuration form, the values don't get saved properly. If you are stuck with same problem you can use a second CKEditor from DNS as explained here: http://naikacademy.blogspot.com/2018/09/how-to-add-text-editor-to-custom-form.html

-1

There is a setting page for CKEdiors. You can configure there. You need to give the path to that form and select include from dropdown and it will apply Rich Text editors to all text areas in that path.

1
  • I did not find any options at admin/config/content/ckeditor. Could you please explain it in more detail? Oct 24, 2016 at 13:09

Your Answer

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

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