Jan,
thanks for your help.
With your reply and after a relaxing weekend, I had a fresh look at the Wiki
pages and APIs. I didn't want to dive into Xpath queries, but eventually
gave in :-).
Here's what I came up with - I'm recording this for anyone up to something
similar.
I've created a new model for the latest news teaser based on the model class
info.magnolia.module.templatingkit.paragraphs.NewsListModel, which is by
default used by it. You can configure the model class to be used by a
particular paragraph in the paragraphs template config.
Here's my code:
public class NewsListModel extends
info.magnolia.module.templatingkit.paragraphs.NewsListModel {
public NewsListModel(Content content, RenderableDefinition definition,
RenderingModel parent) {
super(content, definition, parent);
}
@Override
protected List search() throws RepositoryException {
List newsItems = new LinkedList();
List pages = STKUtil.getContentListByTemplateName(getSearchRoot(),
"newsList");
if (pages != null) {
Content content = (Content) pages.get(0);
String contentPath = content.getHandle();
// Search for all paragraphs on the page found using the
newsHeader paragraph template.
QueryManager mgr = MgnlContext.getQueryManager("website");
Query query =
mgr.createQuery("/"+contentPath+"//element(*,mgnl:contentNode)[MetaData/@mgn
l:template='newsHeader']", Query.XPATH);
QueryResult result = query.execute();
Collection nodes =
result.getContent(ItemType.CONTENTNODE.getSystemName());
if ((nodes != null) && (nodes.size() > 0)) {
System.out.println("Found "+nodes.size()+" news items on
news list page '"+contentPath+"'");
newsItems.addAll(nodes);
}
}
return newsItems;
}
@Override
protected void filter(List itemList) {
// Do nothing.
}
}
What does this do? It first uses STKUtil to find all pages using my
³newsList² page template. Pages of this type contain the news paragraphs I¹m
interested in. I then only look at the first page returned (this is all I
need) and use an Xpath query to find all content nodes using my ³newsHeader²
paragraph template. I also overwrite the default filter() method of
NewsListModel, since I don¹t want to filter the result.
Note that my ³newsHeader² paragraph builds on the default ³stkNewsHeader².
It does feature the typical article/news meta data info such as the author
and a date. I can thus easily feed the result to the paragraph¹s FTL
template to display the news.
This code could be extended/completed to e.g.:
* take all paragraphs of all ³newsList² pages found.
* It could use Xpath only to find all pages.
* And it could be changed to order all resulting paragraphs before returning
them.
Hope this helps someone some time
Andreas
> Von: Jan Haderka <[email protected]>
> Antworten an: Magnolia User-List <[email protected]>
> Datum: Mon, 21 Sep 2009 08:55:46 +0200
> An: Magnolia User-List <[email protected]>
> Betreff: Re: [magnolia-user] Retrieve pars created with a certain par tmpl
>
>
>
>> I then would like to find all paragraphs on that page, which were created
>> using the "newsHeader" paragraph template. I've tried several ways, but
>> nothing seems to work. I've also tried STKUtil.getContentListByTemplateName,
>> but I'm probably doing something wrong with the Content object I get back by
>> these methods?
>>
>> How do I go about this?
>>
>
> I believe those STKUtil methods are meant to search for the pages not
> for paragraphs. Since you already have a page, you can try to either get
> all its paragraphs and iterate through them yourself or you can try to
> get QueryManager and search for it using something
> like /path/to/the/page/element(*,
> mgnl:contentNode)[MetaData/@mgnl:template eq 'myParagraphTemplate']
> (wrote that xpath query from top of my head so you might have to tweak
> it a bit if it doesn't work straightaway) ... you might also want to
> search list archives, there were some posts about searching for pages or
> paragraphs by template names.
>
> HTH,
> Jan
>
>
> ----------------------------------------------------------------
> For list details see
> http://www.magnolia-cms.com/home/community/mailing-lists.html
> To unsubscribe, E-mail to: <[email protected]>
> ----------------------------------------------------------------
>
----------------------------------------------------------------
For list details see
http://www.magnolia-cms.com/home/community/mailing-lists.html
To unsubscribe, E-mail to: <[email protected]>
----------------------------------------------------------------