How to pass variables to custom fields of Widget?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

How to pass variables to custom fields of Widget?

acheung

Hi, I have defined a content type called "NewsItem Widget" with several custom fields. Then I have created 2 widget content of this content type, with different values for the custom fields. (see screenshot file called WidgetContentDefinition.png). I need to pass variables from the velocity code to one of the custom fields (i.e. the "Number of Items To Pull") field. I tried putting in ${numberToPull} (where numberToPull is a variable in the velocity code), I have also tried $render.eval, but none worked.


Any idea how I can pass a variable from velocity field to the custom field in the widget content?


Thanks.


 -Andrew

--
http://dotcms.com - Open Source Java Content Management
---
You received this message because you are subscribed to the Google Groups "dotCMS User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/dotcms/ad084635-3664-4997-9c2c-39db84225b0d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

WidgetContentDefinition.png (49K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: How to pass variables to custom fields of Widget?

John Michael Thomas
From your description I don't fully understand what you're trying to do, but here's a couple things that hopefully will help.

1. In the `Widget Code` field of the Widget Content Type, you can reference the values of any of the other fields of the Widget Content Type using the ${fieldVarName} notation.  For an example, look at the Widget content type in the starter/demo site (the actual VTL code is in the /application/vtl/widgets/you-tube/you-tube.vtl file, but look at the `Widget Code` field in the YouTube content type to see where that's used in the content type).

2. But when you create an instance of the Widget content type you're creating a content item, not a content type.  And from your screen shot (which is showing the _content_ editing screen, not the _Content Type_ editing screen) it looks like you're trying to access the value of a field in that content item from another field in that content type, which doesn't work.  The exception is if you create a custom field (in the Widget Content Type, not in the content item) and use js/dojo code to get the value of another field; but that code is still all in the widget Content Type, not in the widget content items.

So in short, any code that accesses other fields has to exist in the widget Content Type, not in the widget content items.

On Tuesday, October 24, 2017 at 7:56:42 AM UTC-7, Andrew Cheung wrote:

Hi, I have defined a content type called "NewsItem Widget" with several custom fields. Then I have created 2 widget content of this content type, with different values for the custom fields. (see screenshot file called WidgetContentDefinition.png). I need to pass variables from the velocity code to one of the custom fields (i.e. the "Number of Items To Pull") field. I tried putting in ${numberToPull} (where numberToPull is a variable in the velocity code), I have also tried $render.eval, but none worked.


Any idea how I can pass a variable from velocity field to the custom field in the widget content?


Thanks.


 -Andrew

--
http://dotcms.com - Open Source Java Content Management
---
You received this message because you are subscribed to the Google Groups "dotCMS User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/dotcms/ea274872-5653-458e-b39b-5c1ae570ff5d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How to pass variables to custom fields of Widget?

acheung
Hi, John. Here's what I'm trying to do. 

1. I have created a NewsItem Widget content type. This contains a few custom fields (one of which is the "numberOfItemsToPull" field). In the widget code, I have use the custom fields using ${numberOfItemsToPull}. 
2. Then in Content, I have created 2 pieces of content of the content type "NewItem Widget" content type defined above. For each piece of content, I have entered in the values for the custom fields. e.g. for the field called "searchField", I entered in the word "categories". However, for the custom field "number Of Items to pull", I don't know at this point what the value is. This is because it is determined by the velocity code.

What I'm trying to do is : A page where it calls #dotParse(myNewsPage.vtl). The myNewsPage.vtl file will call $import.read("http://mydomain.com/api/widget/id/024b65e1-327c-4069-912b-7bd84a0ba57e) and $import.read(http://mydomain.com/api/widget/id/aba6f0a3-8174-40a2-b48d-60ec777001c3) to display the 2 pieces of content. In this myNewsPage.vtl file it contains the logic regarding what the value should be used in the "number of Items to pull" field.

In summary :

dotCMS page -> myNewsPage.vtl -> calling the 2 widget instances but needs to be able to pass the "number of Items To Pull" from a variable in the myNewsPage.vtl file


Hope this clarifies what I'd like to do. Any help is appreciated.

 -Andrew


On Tuesday, October 24, 2017 at 11:23:56 AM UTC-4, John Michael Thomas wrote:
From your description I don't fully understand what you're trying to do, but here's a couple things that hopefully will help.

1. In the `Widget Code` field of the Widget Content Type, you can reference the values of any of the other fields of the Widget Content Type using the ${fieldVarName} notation.  For an example, look at the Widget content type in the starter/demo site (the actual VTL code is in the /application/vtl/widgets/you-tube/you-tube.vtl file, but look at the `Widget Code` field in the YouTube content type to see where that's used in the content type).

2. But when you create an instance of the Widget content type you're creating a content item, not a content type.  And from your screen shot (which is showing the _content_ editing screen, not the _Content Type_ editing screen) it looks like you're trying to access the value of a field in that content item from another field in that content type, which doesn't work.  The exception is if you create a custom field (in the Widget Content Type, not in the content item) and use js/dojo code to get the value of another field; but that code is still all in the widget Content Type, not in the widget content items.

So in short, any code that accesses other fields has to exist in the widget Content Type, not in the widget content items.

On Tuesday, October 24, 2017 at 7:56:42 AM UTC-7, Andrew Cheung wrote:

Hi, I have defined a content type called "NewsItem Widget" with several custom fields. Then I have created 2 widget content of this content type, with different values for the custom fields. (see screenshot file called WidgetContentDefinition.png). I need to pass variables from the velocity code to one of the custom fields (i.e. the "Number of Items To Pull") field. I tried putting in ${numberToPull} (where numberToPull is a variable in the velocity code), I have also tried $render.eval, but none worked.


Any idea how I can pass a variable from velocity field to the custom field in the widget content?


Thanks.


 -Andrew

--
http://dotcms.com - Open Source Java Content Management
---
You received this message because you are subscribed to the Google Groups "dotCMS User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/dotcms/036a53f2-a27a-4796-8a20-a745d68f4fb6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.