Skip to content

How To Use the configuration provider

Sothea edited this page Oct 11, 2017 · 8 revisions

Until now, we've been extending the base component that comes with Danta base components. We want to use our own base component for section components, thus we can add additional configuration. We'll want to be able to inherit properties from the configuration node (xk.config), so we won't have to create such configurations every time we create a new component. We'll also have the a single source for a common configuration, so all components will behaving in a appropriate manner.

  1. Copy the /apps/danta/components/section/base node.
    /apps/danta/components/section/base

  2. Paste the component copied in the last step onto the /apps/dantademo/components/section folder.
    /apps/dantademo/components/section

  3. Modify the Resource Supertype from the text and the title components:

    Property Old Value New Value
    sling:resourceSuperType danta/components/section/base dantademo/components/section/base

    From now on, our base components for the title and text components is the one located in the dantademo project folder.

  4. Modify the AddDateContextProcessor class with the following code:

package danta.aemdemo.contextprocessors;

import com.google.common.collect.Sets;
import danta.api.configuration.Configuration;
import danta.api.configuration.Mode;
import danta.core.contextprocessors.AbstractCheckComponentCategoryContextProcessor;
import danta.api.ContentModel;
import danta.api.ExecutionContext;
import danta.api.exceptions.ProcessException;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;

import java.util.GregorianCalendar;
import java.util.Set;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.lang.String;

import org.apache.sling.api.resource.ValueMap;

import static danta.Constants.*;
import static danta.aem.Constants.*;

@Component
@Service
public class AddDateContextProcessor
        extends AbstractCheckComponentCategoryContextProcessor {

    @Override
    public Set<String> anyOf() {
        return Sets.newHashSet("date");
    }

    @Override
    public void process(final ExecutionContext executionContext, final ContentModel contentModel)
            throws ProcessException {
        try {
            final SlingHttpServletRequest request = (SlingHttpServletRequest) executionContext.get(SLING_HTTP_REQUEST);
            final ValueMap props = request.getResource().getValueMap();
            final Resource resource = request.getResource();

            Configuration configuration = configurationProvider.getFor(resource.getResourceType());
            final String dateFormatProperty = configuration.asString("dateFormat",Mode.INHERIT);

            DateFormat dateFormat = dateFormatProperty!=null?new SimpleDateFormat(dateFormatProperty):new SimpleDateFormat("yyyy-MM-dd");
            GregorianCalendar lastModified = (GregorianCalendar) props.get("jcr:lastModified");

            dateFormat.setCalendar(lastModified);
            contentModel.set("date.lastModified",dateFormat.format(lastModified.getTime()));

        } catch (Exception e) {
            throw new ProcessException(e);
        }
    }
}

This should inherit the configuration properties from the base component (notice the Mode.INHERIT parameter), so we can use it in all of our extended components. Using this method, we'll retrieve de dateFormat configuration property so we can use it as the format used for the date context, if it's not present, we'll use a default one (yyyy-MM-dd).

  1. Add a new property to our base component xk-config node:

    Property Type Value
    dateFormat String MM-dd-YY

/apps/dantademo/components/section/base/xk.config

  1. Reload /editor.html/content/danta-demo.html and see how the date format looks like.

Go back to Configuration Provider

Clone this wiki locally