Jump to content

Text field: preserve all whitespaces


rooofl
 Share

Recommended Posts

Yes, completely possible! Indeed, I'm not sure why you're losing the spaces.

Do you have a text formatter (on the Details tab) applied to the field? If so, this might be removing the spaces.

If the textarea uses a rich text editor (CKEditor, TinyMCE), it's possible that the editor (or an associated function) is removing the spaces, so it'd be worth checking the settings. I can't think of one that would do this off the top of my head, but I may well have forgotten something!

If it's neither of these, take a look at the HTML in your browser using the Inspect option. And if that doesn't clarify whether spaces are getting through to the page, you could check using something like:

echo str_replace(" ", "_", $page->myTextField);

If underscores appear, the issue is something to do with the rendering of the page.

You could try replacing the the underscores with &nbsp; in the above, though this shouldn't be necessary for the spaces to render in a <pre> element.

Edited by BillH
Incorrectly stated that strings of spaces would appear in the <p> element.
  • Like 1
Link to comment
Share on other sites

InputfieldText (and other inputfield types that extend InputfieldText such as InputfieldTextarea) has a "noTrim" setting that is false by default, which is what causes leading and trailing whitespace to be trimmed out of the field value.

This setting isn't included in the config options for text fields (not sure why, perhaps because it's rarely needed) but you can add a config option for it with a hook:

$wire->addHookAfter('InputfieldText::getConfigInputfields', function(HookEvent $event) {
	/** @var InputfieldText */
	$inputfield = $event->object;
	/** @var InputfieldWrapper $wrapper */
	$wrapper = $event->return;
	$field = $inputfield->hasField;
	// Only for inputfields that are associated with a Field object
	if(!$field) return;
	// Add checkbox field to config to control noTrim setting
	/** @var InputfieldCheckbox $f */
	$f = $event->wire()->modules->get('InputfieldCheckbox');
	$f->name = 'noTrim';
	$f->label = 'No trim';
	$f->label2 = 'Do not trim whitespace from the field value';
	$f->checked($field->noTrim);
	if(!$field->noTrim) $f->collapsed = Inputfield::collapsedYes;
	$wrapper->add($f);
});

This will add a "No trim" checkbox to text fields, and if you tick the checkbox the field value won't be trimmed.

image.png.b83d61619d81165fce1242b7412fb4d1.png

Result:

image.png.dec66dc3624772307f3a7c068fdcc829.png

  • Like 3
  • Thanks 1
Link to comment
Share on other sites

Hi @Robin S and thank you for your hook, it appears to be exactly what I need. However, after some tests, the checkbox appears, the field is logged as noTrim = 1 with wire('log'), but the textarea content leading white spaces are still removed. Is there something else I should check? Currently, I have zero formatter and the content type is text.

Field Object ( [id] => 160 [name] => ascii_drawing [label] => [flags] => [type] => FieldtypeTextarea Object ( [data] => Array ( [inputfieldClass] => InputfieldTextarea [contentType] => 0 [htmlOptions] => Array ( ) ) ) [data] => Array ( [inputfieldClass] => InputfieldTextarea [contentType] => 0 [collapsed] => 0 [minlength] => 0 [maxlength] => 0 [showCount] => 0 [rows] => 5 [noTrim] => 1 ) )

Edit: The field is use with the FormBuilder module, maybe the FormBuilder associated field also trims the content?

Link to comment
Share on other sites

17 minutes ago, rooofl said:

Edit: The field is use with the FormBuilder module, maybe the FormBuilder associated field also trims the content?

If the field value is being set from a FormBuilder form field (e.g. send form submission to PW pages) then you will need to set the noTrim setting for the FormBuilder field too. Again, there is no config field for this but I expect you will be able to hook into the form rendering and/or processing and set noTrim=1. Ryan should be able to help with this in the FormBuilder subforum if you're not sure how.

  • Like 1
Link to comment
Share on other sites

I spoke too soon - should've checked first!

I realise that I've been using CKEditor fields for this sort of thing, and they add &nbsp; entities to the start of the line. Might this be a workaround?

Meanwhile, that's a really useful hook from @Robin S, which I've already made use of (but not for Form Builder fields).

Link to comment
Share on other sites

  • 9 months later...
On 5/30/2023 at 2:07 AM, Robin S said:
$wire->addHookAfter('InputfieldText::getConfigInputfields', function(HookEvent $event) {
	/** @var InputfieldText */
	$inputfield = $event->object;
	/** @var InputfieldWrapper $wrapper */
	$wrapper = $event->return;
	$field = $inputfield->hasField;
	// Only for inputfields that are associated with a Field object
	if(!$field) return;
	// Add checkbox field to config to control noTrim setting
	/** @var InputfieldCheckbox $f */
	$f = $event->wire()->modules->get('InputfieldCheckbox');
	$f->name = 'noTrim';
	$f->label = 'No trim';
	$f->label2 = 'Do not trim whitespace from the field value';
	$f->checked($field->noTrim);
	if(!$field->noTrim) $f->collapsed = Inputfield::collapsedYes;
	$wrapper->add($f);
});

Just used this for a field holding private keys. Perfect! Thanks @Robin S 👍

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...