RE: Structures that contain Structures

David Leeson, modified 7 Years ago. New Member Posts: 4 Join Date: 9/18/18 Recent Posts

Hello all!

 

I am new to Liferay so I have what may be a quite elementary question (or a limitation of the product!).

 

2 questions...

  1. I would like to create a structure (A) which includes another structure (B)
  2. I would like to be able to reference the structure (B) in an Application Display Template for Web Content of structure type (A)

 

For example...

 

  • - Structure (B) could be an Author with the fields "Name", "Company", "Photo"
  • - Structure (A) could be a News Article with fields like "Headline", "Byline", "Publish Date", "Main Body", and "*Author*"

 

  1. Clearly, several News Articles could have the same Author, so I would like users to be able to use and associated Template to create a News Article with "Headline", "Byline", "Publish Date", "Main Body", and selecting an Author from a list of Authors (that were created earlier via the Authors Template).
  2. Then, when I display a list of articles on a page, I would like my Application Display Template to be able to format the details of each News Article and the Author.

 

How do I do that?

 

(I am not looking for inheritance here - if it can be avoided! I don't want to have to create a "News Article With Author" that is a child of the "Author" structure - for one thing, I might need to add another structure to the News Article, which would require multiple inheritance!)

 

Thanks for your help!

thumbnail
Samuel Kong, modified 7 Years ago. Liferay Legend Posts: 1902 Join Date: 3/10/08 Recent Posts

2 questions...

  1. I would like to create a structure (A) which includes another structure (B)
  2. I would like to be able to reference the structure (B) in an Application Display Template for Web Content of structure type (A)

 

1. You should set Structure A as the parent of Structure B.

2. Your template for Structure B will be able to access the fields in Structure A.

 

(I am not looking for inheritance here - if it can be avoided!

 

I know you you said you want to avoid inheritance, but this this is exactly the purpose of this feature.

 

I don't want to have to create a "News Article With Author" that is a child of the "Author" structure - for one thing, I might need to add another structure to the News Article, which would require multiple inheritance!)

 

Not sure why this is problem. You can name your structure anything you want. Just call it "News Article" if you want.

Structures can't have multiple parents. However, you can extend the example above such that structure A is a parent of structure B and structure B is a parent of structure C. Structure C will include the fields from structure A and B.

David Leeson, modified 7 Years ago. New Member Posts: 4 Join Date: 9/18/18 Recent Posts

Samuel,

Thanks for the prompt response.

 

As you may have guessed form my original post, I was afraid that inheritance would be the only way to make this work.

 

Of course, it will work, but I was looking for a more elegant implementation - where you can just add a custom structure directly to another structure - either through point and click or through some custom code.

 

The problem with the solution of inheritance is that it will create a structure that requires the user to enter both the parent and child information every time a new child is required. What I am looking for is a solution where the parent (or child) information can be reused. For instance, in my example, I would like to be able to create an Author that I can apply to a number of different articles without having to reenter the Author details every time.

 

I guess you are saying that that is not an option.

 

David Leeson, modified 7 Years ago. New Member Posts: 4 Join Date: 9/18/18 Recent Posts

Perhaps the workaround answer is to have the user who is creating a new Article enter the ID of the Author instance.

 

That would be a bit cumbersome - certainly less friendly than allowing the user to select the Author from a list - but at least we would be able to reuse Author details rather then reentering them.

 

In that case, could you give me some advice/examples on how to retrieve the Author details using an id - from within an Application Display Template that I would use to display the Article?

 

Thanks!

thumbnail
Christoph Rabel, modified 7 Years ago. Liferay Legend Posts: 1555 Join Date: 9/24/09 Recent Posts

Not sure which Liferay version you are using, but in 7.0+ there is a field type "Webcontent". With that you can select another webcontent. So, you create your structure Author and your Structure News. In News you add a field of type Webcontent "Author". Of course, the person selecting the Author can select any article there, there are no restrictions, but people should be able to do that.

Rendering it is a bit complicated, you need to fetch the article using the journalarticle service and then call journalarticle.getContent(...)

If I recall correctly, this article is pretty helpful:

https://community.liferay.com/blogs/-/blogs/divide-and-conquer-rendering-structured-web-content-with-the-asset-publisher

 

I had a similar requirement once and we just used a textfield where the editor entered the screenname of the author. We fetched the user and displayed fields from there (name, picture, email). And we had a default user in case the user didn't exist.

 

David Leeson, modified 7 Years ago. New Member Posts: 4 Join Date: 9/18/18 Recent Posts

Thanks for the reply, Christoph.

 

(I am using Liferay 7.1)

 

Crating a structure with a web content object embedded is a piece of cake - I like that approach.

 

I am still struggling with addressing the web content object within the "parent" structure.

 

In fact, addressing any of the custom web content fields (i.e. other than Title and Summary) seems problematic: I expected to be able to address these as members of the component class - e.g. Assign shortSumm = Article.shortSummary, Assign artcleBody=Article.body.

 

Can anyone help with a simple example?

 

Thanks!

 

Masoud Salehisedeh, modified 7 Years ago. New Member Posts: 4 Join Date: 9/20/18 Recent Posts

I am using liferay_ui on each iteration of entries to have an article  web content display and within the web content template i tried to find a way to use liferay_ui to call an author web content. I could not find examples on how to do it since variables available in display template is not accessible in web content template. The i tried using serviceLocator to display author content. But i get restriction exception on serviceLocator. I tried removing restrictions from "Control Panel -> Configuration -> System Settings -> Template Engines -> FreeMarker Engine -> Restricted Variables", but i still get the exception. Anyone out there knows how to make any one of the above paths i have taken work? And on top of that, Is there any way to pass a parameter to liferay_ui or serviceLocator to control amount of information being displayed?

Is there any other possible path to take?

Masoud Salehisedeh, modified 7 Years ago. New Member Posts: 4 Join Date: 9/20/18 Recent Posts

I restarted the server and problem with serviceLocator  is resolved. Now I still need to know how i can pass a parameter to the ADT for author or article in order to have the template display data differently on different pages.

Thanks

Masoud Salehisedeh, modified 7 Years ago. New Member Posts: 4 Join Date: 9/20/18 Recent Posts

JUST in case if anybody is interested. The solution that i came up with to pass parameters to the web content journals is to pass them through request.setAttribute("myAttr","myValue") and get them back in the template through request.getAttribute("myAttr").  I am still looking to see if anybody has a better approach.

thumbnail
Minhchau Dang, modified 7 Years ago. Liferay Master Posts: 598 Join Date: 10/22/07 Recent Posts
Masoud Salehisedeh:

I tried removing restrictions from "Control Panel -> Configuration -> System Settings -> Template Engines -> FreeMarker Engine -> Restricted Variables", but i still get the exception. Anyone out there knows how to make any one of the above paths i have taken work?

Changes to those restrictions only take effect after a server restart.