3

I'm using the Filtered HTML input format without a WYSIWYG editor.

When editing text, if I put some text onto a new line by hitting return, when I save its fine.

However if I add another line (so there is a space between the 2 lines of text) by hitting return twice, when I save this extra line disappears. How can I stop this from happening? Thanks

So I can do this:

1
2

But I can't do this:

1

2
11
  • Is it possible to look at the source code before and after? Somebody may already know the answer to this, but seeing the before and after source code would be great. Sep 27, 2012 at 13:49
  • The html once the comment is saved is every new line wraps its text in p tags. Before the comment is saved if i inspect the text element I can see my spacing in it but no mark up.
    – Evanss
    Sep 27, 2012 at 14:17
  • It sounds like it's removing blank paragraphs. D7 or D6? Sep 27, 2012 at 14:37
  • 7 ..............
    – Evanss
    Sep 27, 2012 at 14:43
  • Like you said, I think whats happening is Drupal is converting new lines into new p tags, but double lines arnt adding a br tag or empty p tag. Id say this is a bug or at least a limitation as you cant alter this with filters at all.
    – Evanss
    Sep 27, 2012 at 15:09

5 Answers 5

1

Consider using the Markdown Filter. It not only solves your problem, but gives a more intuitive input option.

If your users are used to using Markdown on StackOverflow, then they will love it ;) In fact, StackOverflow was one of the first sites to integrate the Markdown format.

0

Add the "P" tag (< p >) and "BR" tag (< br >) to the list of allowed tags on your filter HTML tags settings. You don't have to worry about security because since it is a filter HTML all the extra code like style inside the p tag will be remove.

Note: Make sure to check the option to Convert line breaks into HTML (i.e. < br > and < p >)

Note 2: if using a WYSIWYG editor like ckeditor make sure you have a set a condition like in note 1.

8
  • Ive already added those tags and p and br are in the html allowed tags list.
    – Evanss
    Sep 27, 2012 at 14:12
  • If I remove all filters except for 'Convert line breaks into HTML (i.e. <br> and <p>)' then I can still only start a new line, I cant put a double spacing in the text.
    – Evanss
    Sep 27, 2012 at 14:15
  • Just as a comment do you check the user permissions are set to allow it to use the filter.
    – Emil Orol
    Sep 27, 2012 at 21:23
  • Also one more question: are the new lines been replaced by P's and BR's, but there are not enough space between them? if that is the case you just need a line in your CSS like p {margin-bottom:1.5em;}
    – Emil Orol
    Sep 27, 2012 at 21:24
  • Its not a permissions issue, the same thing happens when I post comments as the admin.
    – Evanss
    Sep 28, 2012 at 8:49
0

Ive found a solution but its not perfect.

Ive turned on the 'Limit allowed HTML tags' filter for safety but turned all other filters off. This means the spaces appear in the code but do nothing as they arn't p or br tags.

Then I apply white-space: pre; to the element. Now all the spaces and new lines are preserved.

The issue with this is that a user could enter 100 new lines as there is no validation at all. Another problem is if this field is aggregated or used elsewhere on the site you would need to apply the css again to see the spaces.

Ill be very happy to accept a better solution.

0

Another solution is to use a wysiwyg editor. If you add a blank line then Ckeditor will create a p tag containing a html space.

I was however hoping to avoid using an wysiwyg as it needlessly increases the page size. I also dont know what its performance is link on mobile devices as its javascript based.

0

I don't know a perfect solution either. You can limit the number of consecutive newlines at any stage you like with regex - client-side presave, server-side presave, server-side pre-output, client-side output.

Here's an example for server-side pre-output modification in your theme's template.php file. The plain_text format can be any text filter of your choice and the null case covers plain text (when filter not set). I'm limiting the max newlines to 2 (using {3,} for 3 instances and above). My generalised regex covers \r newlines as well but this wouldn't be necessary in this use case.

function YOURSUBTHEME_preprocess_field(&$variables, $hook) {
  if ($variables['element']['#field_type'] == 'text_long' &&  // Makes sure this is a text_long field type.
    isset($variables['element']['#items'][0]) && 
    (!isset($variables['element']['#items'][0]['format']) ||
    $variables['element']['#items'][0]['format'] === 'plain_text')
  ) {
    foreach ($variables['items'] as $index => $value) {
      $markup = isset($variables['items'][$index]['#markup']) ? $variables['items'][$index]['#markup'] : '';
      $markup = preg_replace("/[\r\n]{3,}/", "\n\n", $markup);
      $variables['items'][$index]['#markup'] = nl2br($markup);
    }
  }
}

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.