craftcms / cms

Build bespoke content experiences with Craft.
https://craftcms.com
Other
3.27k stars 635 forks source link

Reference Tags parsed & not restored on save #1643

Closed khalwat closed 6 years ago

khalwat commented 7 years ago

If you insert a Reference Tag (https://craftcms.com/docs/reference-tags) such as {globalset:737:loanName} into a Rich Text field, an attempt is made to preserve them: https://github.com/craftcms/cms/blob/develop/src/fields/RichText.php#L246

            // Preserve the ref tags with hashes {type:id:url} => {type:id:url}#type:id
            $value = preg_replace_callback('/(href=|src=)([\'"])(\{(\w+\:\d+\:'.HandleValidator::$handlePattern.')\})(#[^\'"#]+)?\2/',
                function($matches) {
                    return $matches[1].$matches[2].$matches[3].(!empty($matches[5]) ? $matches[5] : '').'#'.$matches[4].$matches[2];
                }, $value);

And then they are parsed via Elements->parseRefs -- the problem is that the preservation only appears to work for the Asset and Entry links that Craft is specifically looking for.

When serializeValue() is called to save the RTF data, it attempts to convert them back into reftags: https://github.com/craftcms/cms/blob/develop/src/fields/RichText.php#L326

But this fails to work for any reference tags that are not Asset or Entry links, such as the above example.

What happens is that the reftag is saved properly the first time (and is properly parsed as output as per https://github.com/nystudio107/craft3-richvariables ) but editing that Entry again will cause the parsed text to be in the RTF, not the reftag. So if the entry is saved a second time, the tagref disappears, and reftag is no longer parsed.

Expected result:

  1. The preservation/restoration magic should work for any type of tagref

(This is present in both Craft 2.x and Craft 3.x)

carlcs commented 7 years ago

You need to preserve the reference somehow.

In case of element URLs the reftag is preserved using a hash
http://example.com/news/my-entry#entry:79

For plain text one option would be to preserve it using data attribute on a span tag. <span data-reftag="globalset:737:loanName">My Loan Name</span>

I think it would be nice if Craft would allow Plugins to hook into RichTextFieldType’s onGetInputHtml() and prepValue() to parse and preserve reference tags or other types of shortcodes and prepare them for output.

brandonkelly commented 6 years ago

@MakeilaLundy @Radabaugh let's see if we can get this fixed for Craft 2.

steveooo9 commented 6 years ago

@brandonkelly Any update on this? This is somewhat critical for a project we work on...