3

I've got a problem with CKEditor in Drupal 8. I've searched related questions like CKeditor adds extra <p> tags, but almost all questions are related to Drupal 7.

So, my CKEditor automatically applies a <p>&nbsp</p> when I hit enter and don't type anything.

screenshot

How can I disable this? I don't want to remove the padding or anything of a <p> tag. I need something that checks if the paragraph is empty and then don't display it.

  • This is standard ckeditor behavior. Use shift+enter to inserr breaks instead of paragraphs. – user21641 Oct 11 '17 at 15:02
  • I know that, but I don't want to do that. I want shift-enter to be the default behaviour. Because when a user accidentally presses enter after writing their article, there must not be another <p> tag ... – Rubenxfd Oct 11 '17 at 15:03
  • A solution for Drupal 7: WYSIWYG module, br instead of p tags in Drupal 7. Although I am pretty sure you can use modify this for D8 as well. – ram4nd Oct 13 '17 at 7:20
1

have you tried toggling off the "Convert line breaks into HTML (i.e. <br> and <p>)" setting on the text input format?

enter image description here

  • Yesss I did never enabled them =/ – Rubenxfd Oct 13 '17 at 7:34
1

Note that you can add your own customized filter in a custom module : as an example, this could look like this to remove empty paragraphs :

<?php

namespace Drupal\your_module\Plugin\Filter;

use Drupal\filter\FilterProcessResult;
use Drupal\filter\Plugin\FilterBase;
use Drupal\filter\Annotation\Filter;
use Drupal\Core\Annotation\Translation;

/**
 * Class FilterEmptyParagraphs
 * @Filter(
 *   id = "filter_empty_paragraphs",
 *   title = @Translation("Filter empty paragraphs"),
 *   description = @Translation("Removes empty paragraphs."),
 *   type = Drupal\filter\Plugin\FilterInterface::TYPE_MARKUP_LANGUAGE,
 * )
 * @package Drupal\your_module\Plugin\Filter
 */
class FilterEmptyParagraphs extends FilterBase
{
    /**
     * Performs the filter processing.
     *
     * @param string $text
     *   The text string to be filtered.
     * @param string $langcode
     *   The language code of the text to be filtered.
     *
     * @return \Drupal\filter\FilterProcessResult
     *   The filtered text, wrapped in a FilterProcessResult object, and possibly
     *   with associated assets, cacheability metadata and placeholders.
     *
     * @see \Drupal\filter\FilterProcessResult
     */
    public function process($text, $langcode)
    {
        $regex = '@<p([^>]*)>\s*\n*\t*(&nbsp;)*\s*\n*\t*</p>@igm';
        $text = preg_replace($regex, '', $text);
        return new FilterProcessResult($text);
    }
}

(regex not tested)

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.