Liferay 7 Extending EditableFragmentEntryProcessor

421 Views Asked by At

I want to extend functionality of EditableFragmentEntryProcessor in Liferay 7.4 (<lfr-editable> tags in fragments) by searching in text syntaxes like {user.name} and replacing it with value from response from my external API.

e.x.

I type something like

This is super fragment and you are {user.name}.

And result should be

This is super fragment and you are Steven.

I achieve that with creating my own FragmentEntryProcessor, but I did this by putting fragment configuration variable in my custom tag

<my-data-api> ${configuration.testVariable} </my-data-api>

I tried something like this before

<my-data-api> 
  <lfr-editable id="some-id" type="text">  
    some text to edit
  </lfr-editable>
</my-data-api>

And it doesn't work (and I know why).

So I want to get something like this. Appreciate any help or hints.

EDIT:

Here my custom FragmentEntryProcessor:

package com.example.fragmentEntryProcessorTest.portlet;

import com.example.test.api.api.TestPortletApi;
import com.liferay.fragment.exception.FragmentEntryContentException;
import com.liferay.fragment.model.FragmentEntryLink;
import com.liferay.fragment.processor.FragmentEntryProcessor;
import com.liferay.fragment.processor.FragmentEntryProcessorContext;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.util.Validator;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

import java.io.IOException;

/**
 * @author kabatk
 */

@Component(
        immediate = true, property = "fragment.entry.processor.priority:Integer=100",
        service = FragmentEntryProcessor.class
)

public class FragmentEntryProcessorApiDataCopy implements FragmentEntryProcessor {

    private static final String _TAG = "my-data-api";

    @Reference
    private TestPortletApi _api;

    @Override
    public String processFragmentEntryLinkHTML(
            FragmentEntryLink fragmentEntryLink, String html,
            FragmentEntryProcessorContext fragmentEntryProcessorContext)
            throws PortalException {

        Document document = _getDocument(html);
        Elements elements = document.getElementsByTag(_TAG);
        elements.forEach(
                element -> {
                    String text = element.text();
                    String attrValue = element.attr("dataType");
                    String classValues = element.attr("classes");
                    Element myElement = null;
                    String result;

                    try {
                        result = _api.changeContent(text);
                    } catch (IOException e) {
                        e.printStackTrace();
                        result = "";
                    }

                    if(attrValue.equals("img")){
                        myElement = document.createElement("img");
                        myElement.attr("class", classValues);
                        myElement.attr("src", result);

                    }else if(attrValue.equals("text")){
                        myElement = document.createElement("div");
                        myElement.attr("class", classValues);
                        myElement.html(result);
                    }

                    if(myElement != null)
                        element.replaceWith(myElement);
                    else
                        element.replaceWith(
                            document.createElement("div").text("Error")
                        );
                });

        Element bodyElement = document.body();

        return bodyElement.html();

    }

    @Override
    public void validateFragmentEntryHTML(String html, String configuration)
            throws PortalException {

        Document document = _getDocument(html);

        Elements elements = document.getElementsByTag(_TAG);

        for (Element element : elements) {
            if (Validator.isNull(element.attr("dataType"))) {
                throw new FragmentEntryContentException("Missing 'dataType' attribute!");
            }
        }
    }

    private Document _getDocument(String html) {
        Document document = Jsoup.parseBodyFragment(html);

        Document.OutputSettings outputSettings = new Document.OutputSettings();

        outputSettings.prettyPrint(false);

        document.outputSettings(outputSettings);

        return document;
    }

}


0

There are 0 best solutions below