Search code examples
phpcssmysqlmagento2nl2br

Restoring linebreaks in product descriptions migrated into Magento 2


I'm helping migrate an old Magento 1.6 site to Magento 2.3, and it looks like the product descriptions and short descriptions which were entered in plain text with minimal markup and linebreaks to form paragraphs now look like everything is smashed into a single paragraph, while the page source still renders the whitespace correctly and shows the line breaks. But without tags, the whitespace is ignored by the browser.

Moving forward in Magento 2, we'd like to use the WYSIWYG editor to better format the product descriptions, but we'd like the migrated products to at least be formatted properly.

This thread seemed to describe what I wanted to do, wrapping the description template in the nl2br() method: Linebreak in magento product description when NOT using wysiwyg

This looked like a good way to solve this issue without affecting the way our future product descriptions (using WYSIWYG) will display, at least I think? This solution was for Magento 1, so I had to find the Magento 2 equivalent. I found the description.phtml template in /vendor/magento/module-catalog/view/frontend/templates/product/view/description.phtml

the line now reads:

<?= /* @escapeNotVerified */ $this->helper('Magento\Catalog\Helper\Output')->productAttribute($block->getProduct(), $block->getProduct()->getDescription(), 'description') ?>

How can I apply the nl2br() here? Do I just wrap it around "getDescription()" or should it go elsewhere? And where should I place the new template it in my custom theme to have it overwrite the default? I've tried several variations and different locations in the file structure and nothing I've done seems to be taking effect.

I'm not a developer so I'm probably making basic mistakes here. I've even tried screwing up the code on purpose but it doesn't do anything, so I'm pretty sure the template isn't in the right place, or maybe I'm not clearing the right caches? I'm using cache:clear and cache:flush, and even deleting pub/static/frontend and var/view_preprocessed just in case, but nothing seems to be working.

Another approach I considered was using CSS to add a "whitespace:pre-line;" attribute to the .description class, but while it would fix the migrated data, when I tested it in the browser's developer mode I noticed that it doubles the white space in new products entered with the WYSIWYG editor, which is less than optimal moving forward. I'm also not very familiar with LESS, so I'm not sure where to best place this rule. would it be in _theme.less? _extend.less?

A last thought I had was to try to figure out a MySQL command to do a one-time replacement so \n becomes
tags in the description and shortdescription attributes, but I don't know enough MySQL to do something like that.

So which solution should I be pursuing and what syntaxes should I use? I feel like a bad pool player who knows what angles I can take but can't sink the shot!

Thank you for your help.


Solution

  • Well I couldn't figure out how to get nl2br() to work, but I found the product descriptions and short descriptions in the catalog_product_entity_text table, along with other attributes for the catalog products.

    I did some tests and found that products edited with the WYSIWYG editor had the html code in their entries, whereas the migrated text had just linebreaks. Manually inserting html tags had the desired effect, and after confirming with the team that they preferred br over p tags for generating their linebreaks, I was able to fix everything with the following queries:

    DESCRIPTIONS (attribute_id 61)

    UPDATE catalog_product_entity_text SET value = REPLACE( value, '\r\n', '<br />' ) WHERE attribute_id = "61"
    

    SHORT DESCRIPTIONS (attribute_id 62)

    UPDATE catalog_product_entity_text SET value = REPLACE( value, '\r\n', '<br />' ) WHERE attribute_id = "62"
    

    If this hadn't worked, I might've tried just '\n' in the replace value, but it went on the first go and everything is looking as expected. What a relief!

    If anyone knows how I could've modified the php template to achieve this (and where that file should go), I'd like to know for future reference and others' sake.

    Cheers! Self-five