rooofl Posted May 29, 2023 Share Posted May 29, 2023 I have a text field and I need its value to preserve all white spaces, especially the one ahead all text content. Is that possible? What I get when I <pre> the output: 1 2 3 What I need 1 2 3 Link to comment Share on other sites More sharing options...
BillH Posted May 29, 2023 Share Posted May 29, 2023 (edited) 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 in the above, though this shouldn't be necessary for the spaces to render in a <pre> element. Edited May 29, 2023 by BillH Incorrectly stated that strings of spaces would appear in the <p> element. 1 Link to comment Share on other sites More sharing options...
Robin S Posted May 30, 2023 Share Posted May 30, 2023 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. Result: 3 1 Link to comment Share on other sites More sharing options...
rooofl Posted May 30, 2023 Author Share Posted May 30, 2023 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 More sharing options...
Robin S Posted May 30, 2023 Share Posted May 30, 2023 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. 1 Link to comment Share on other sites More sharing options...
rooofl Posted May 30, 2023 Author Share Posted May 30, 2023 Yes, I just did that, adding noTrim to the FormBuilder field as well. But the issue still exists. My logs: Link to comment Share on other sites More sharing options...
BillH Posted May 30, 2023 Share Posted May 30, 2023 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 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 More sharing options...
rooofl Posted May 30, 2023 Author Share Posted May 30, 2023 I tried using leading invisible characters or even visible ones, but trim() seams to transform 1 2 into 12 for the first line of the content. Link to comment Share on other sites More sharing options...
diogo Posted March 21 Share Posted March 21 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 ? 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now