0

In a Drupal 8 project I've a controller which returns a form that contains a text_format field.

CKeditor is working perfectly in my administration theme (Stable), but it does not on the default theme.

I've tried to attach ckeditor core module libraries to the controller returned response with no results.

The role which I test with has permissions "Use simple HTML text format", "Use full HTML text format", also the role is checked on /admin/config/content/formats/manage/basic_html also on /admin/config/content/formats/manage/full_html.

my_module/src/Controller/MyController.php :

<?php

namespace Drupal\my_module\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\user\Entity\User;

/**
 * Provide user form.
 */
class MyController extends ControllerBase {

  /**
   * Return page content.
   */
  public function content() {
    // Load user edit form.
    $entity = User::load(\Drupal::currentUser()->id());
    $formObject = \Drupal::entityTypeManager()
      ->getFormObject('user', 'default')
      ->setEntity($entity);
    $form = \Drupal::formBuilder()->getForm($formObject);
    $render = \Drupal::service('renderer');
    $render_form = $render->renderPlain($form);
    return [
      '#theme' => 'customize_user_form',
      '#form' => $render_form,
      '#attached' => [
        // Attach ckeditor core module libraries.
        'library' => [
          'ckeditor/drupal.ckeditor',
          'ckeditor/drupal.ckeditor.stylescombo.admin',
          'ckeditor/drupal.ckeditor.admin',
          'ckeditor/drupal.ckeditor.plugins.drupalimagecaption',
        ],
      ],
    ];
  }

}

Results:

enter image description here

Any suggestions?

Many thanks.

1
  • 1
    I would guess there are missing libraries that aren't loaded in the page, but I could not tell you which ones.
    – apaderno
    May 17, 2020 at 21:43

1 Answer 1

1

A form usually contains all libraries necessary, but you shouldn't render the form at this point in the controller.

Put it unrendered in the template variable

return [
  '#theme' => 'customize_user_form',
  '#form' => $form,
];

and when you print the form in the template {{ form }} the libraries should be attached automatically to the page.

1
  • Exactly, thank you :)
    – MiharbKH
    May 22, 2020 at 13:08

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.