How to combine two fields with an PresentationArticleDecorator?
This is the code example.
/*
* $Header$
*
* Created on Apr 5, 2006
*
* Copyright (C) 2005 Escenic.
* All Rights Reserved. No use, copying or distribution of this work may be made
* except in accordance with a valid license agreement from Escenic. This notice
* must be ncluded on all copies, modifications and derivatives of this work.
*/
package com.escenic.example;
import neo.xredsys.presentation.PresentationArticle;
import neo.xredsys.presentation.PresentationArticleDecorator;
public class LocationContentDecorator extends PresentationArticleDecorator {
private String locationContent;
private String separator = "<p>";
public LocationContentDecorator() {
super();
}
public LocationContentDecorator(PresentationArticle pPresentationArticleToDecorate) {
super(pPresentationArticleToDecorate);
}
public String getFieldElement(String pFieldName) {
if ("locationContent".equalsIgnoreCase(pFieldName)) {
if (this.locationContent == null) {
String content = super.getFieldElement("content");
StringBuffer result = new StringBuffer();
result.append(content.substring(0, this.separator.length()));
result.append("<span class=\"location\">");
result.append(super.getFieldElement("location"));
result.append("</span>");
result.append(content.substring(this.separator.length(), content.length()));
this.locationContent = result.toString();
}
return this.locationContent;
}
return super.getFieldElement(pFieldName);
}
}
We override the {{getFieldElement(String)}} method. This is the one that is called when you do a {{<ARTICLE:field field="hubba" />}}. So we have now in effect created a new field with the name of {{locationContent}}. When you now do a call with {{<ARTICLE:field field="locationContent" />}} you call upon our newly created method.
For how to attach this decorator to a spesific article type, have a look at {{Page developers guide - Advanced topics}}
--[Seberget]
