joomla / joomla-cms

Home of the Joomla! Content Management System
https://www.joomla.org
GNU General Public License v2.0
4.73k stars 3.64k forks source link

Custom Fields - Text - Issue #42566

Open lushdomain opened 9 months ago

lushdomain commented 9 months ago

Steps to reproduce the issue

None, it just happens

Expected result

See Below

Actual result

See Below

System information (as much as possible)

J5.0.1 Chrome Browser

Additional comments

When I add a text type custom field in an article an extra space is added directly after the content of the field. I've double checked my entries for additional spaces, there are none.

Article text in editor operates in {field 6}, {field 7}. We cover........

Rendered article text in browser operates in Basingstoke , Hampshire . We cover.....

You can see the extra space being rendered before the , and the .

Code from Console

<p>operates in <span class="field-value ">Basingstoke</span>
, <span class="field-value ">Hampshire</span>
. We cover......</p>

This is exactly how the code is produced in the page, a bit "janky" I'd say, but may have no bearing on the issue.

This issue only seems to happen if the field is immediately followed by punctuation, if it's followed by another word there's no extra space. Although in other places in the article the field is followed by a dash ( - ) with no extra space. But, if followed by a ( ? ) then an extra space is inserted between the field and the ( ? )

Merry Christmas everyone.

lushdomain commented 8 months ago

Just wondering if anyone on the team has seen this yet?

brianteeman commented 8 months ago

confirmed

Saswatsusmoy commented 7 months ago

Here's a potential solution using the FieldsHelper::render() function that might help resolve this problem:

  1. Override the Render Method: You can override the FieldsHelper::render() method in your template or plugin to control how the fields are outputted.

    // Load the FieldsHelper
    JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php');
    
    // Get the custom fields for a specific item
    $jcFields = FieldsHelper::getFields('com_content.article', $item, true);
    
    // Loop through the fields and render them without extra spaces
    foreach($jcFields as $field) {
        // Render the field and trim any whitespace
        $fieldOutput = trim(FieldsHelper::render($field->context, 'field.render', array('field' => $field)));
        echo $fieldOutput;
    }

    In this snippet, trim() is used to remove any leading or trailing whitespace from the rendered field output.

Hope this helps!

brianteeman commented 7 months ago

chatGPT is not a solution

Saswatsusmoy commented 7 months ago

chatGPT is not a solution

Actually I tried solving using the same logic and code only, but for explanation I used ChatGPT

brianteeman commented 7 months ago

... and did you actually try the code? Did it really solve the reported problem?

Saswatsusmoy commented 7 months ago

... and did you actually try the code? Did it really solve the reported problem?

Yeah, the code mentioned above isn't the actual code, there's a different code but for like validation if my approach is correct I provided the sample code. I will be raising a PR with the actual code which is solving this problem