3

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();
  • As side note, $form['']['format'] should probably be $form['body']['format']. – kiamlaluno Jun 24 '12 at 12:16
  • CCKeditors are easily configurable through admin panel, no need of code modification – GoodSp33d Jun 24 '12 at 12:37
8

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'),
  );
  • 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 '17 at 15:45
4

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

text_format

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

  • 1
    This is the correct answer – Scott Joudry Mar 2 '15 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 '17 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 '17 at 6:03
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

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.

  • I did not find any options at admin/config/content/ckeditor. Could you please explain it in more detail? – Alexander Ishmuradov Oct 24 '16 at 13:09
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

0

In Drupal 8.6.x my solution was:

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

Your Answer

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.