I have a body field in a congtent type, and I have set the input filter in the content type to 'plain text'.
Now, when I edit the body, I can enter newlines.
These are not reflected in the view-mode of the node: Test is shown as a long sentence; all newlines are gone, and not converted to
.

Comments

yched’s picture

Status: Active » Closed (works as designed)

That's how 'plain text' works.
Newlines in text input are converted into <br> by the 'linebreak' filter, which therefore only applies within an input format.
If the text is a 'plain text', linebreaks are kept as "\n" chars, and are not displayed as linebreaks by the browser.
Line breaks are 'formatting', they require an 'input format'

johnv’s picture

OK, thanks for explaining.

izkreny’s picture

Priority: Minor » Normal
Status: Closed (works as designed) » Active

Hm, if this really works as designed, then I don't get it why when I choose Filtered text (user selects text format) in Text processing for Body field suddenly all Enabled filters for Plain text text format are working in already created content which is using Plain text?! ;)

Also I don't understand why there is possibility to enable filters for Plain text text format if by desing they are supposed to not work?

:confused:

P.S.
I'm using drupal-7.0

davidwhthomas’s picture

You can add HTML line breaks to your plain text field output in the field template

e.g field--field_contact_physical_address.tpl.php

Adding this line

<?php $item['#markup'] = nl2br($item['#markup']); // add linebreaks ?>

The template file then contains:

<?php
<div class="<?php print $classes; ?> clearfix"<?php print $attributes; ?>>
  <?php if (!$label_hidden) : ?>
    <div class="field-label"<?php print $title_attributes; ?>><?php print $label ?>:&nbsp;</div>
  <?php endif; ?>
  <div class="field-items"<?php print $content_attributes; ?>>
    <?php foreach ($items as $delta => $item) : ?>
      <?php $item['#markup'] = nl2br($item['#markup']); // add linebreaks ?>
      <div class="field-item <?php print $delta % 2 ? 'odd' : 'even'; ?>"<?php print $item_attributes[$delta]; ?>><?php print render($item); ?></div>
    <?php endforeach; ?>
  </div>
</div>
?>

There's a probably a better way of doing it in the preprocess function by checking the formatter and changing the value there, but I used the above method as a quick fix.

DT

davidwhthomas’s picture

Status: Active » Closed (works as designed)

Closing as per #1, #3 is a different issue.

star-szr’s picture

template.php approach to #4 in case anyone else ends up here looking for line breaks with their plain text fields.

/**
 * Add line breaks to field
 */
function THEMENAME_preprocess_field(&$variables, $hook) {
  if ($variables['element']['#field_name'] == 'FIELD_NAME') {
    $variables['items'][0]['#markup'] = nl2br($variables['items'][0]['#markup']);
  }
}
loparr’s picture

hi,
this code is not working for me. I put this code into template.php, changed themename but with no result.

star-szr’s picture

@loparr - if you mean the code I posted, you will need to change FIELD_NAME as well, perhaps I should have mentioned that.

Also be sure to clear your cache.

loparr’s picture

Ok, that must be it;) will try it . Thank you very much.

leopark’s picture

you can set below.

go to Configuration > Text formsts > Plain text > configure > and check Convert line breaks into HTML (i.e. <br> and <p>)
make sure it should be in the last in Filter processing order area
LEO.

jptaranto’s picture

Here's some updated code that should be fairly generic.

This will add line breaks for ANY long text field that is set as PLAIN TEXT (not filtered text -> plain text).

<?php
/**
* Add line breaks to field
*/
function THEMENAME_preprocess_field(&$vars) {
  if ($vars['element']['#field_type'] == 'text_long') {
    $field_name = $vars['element']['#field_name'];
    foreach ($vars['items'] as $key => &$item) {
      if ($vars['element']['#object']->{$field_name}[LANGUAGE_NONE][$key]['format'] == NULL) {
        $item['#markup'] = nl2br($item['#markup']);
      }
    }
  }
}
?>
bpadaria’s picture

Thanks for sharing. #11 works.

Seymour83’s picture

Issue summary: View changes

#11 Thank you!

Helice’s picture

#11 thank you very much!!