Hello,
i have also spent some time with this till I got to the philosophy... But is is so, like You write - because the method runtime creates the new portlet in the current page. So, there is actually created a new record in the Db table having the portlet preferences for this page. Because every portlet can have different prefereces on every page, if You create the new page , the theme.runtime method just creates the new representation of the portlet on your new page - so it is also filled by the default settings.
It is also pretty problematic to change the article in the page to some another - because in the case You have already called the method theme.rutime for the first time, the portlet preferences are not stored - You must delete the portlet from the page and reinitialize it again. I make it in the following way:
1
2 #set ($locGroupAndArticleIdForChecking = "$locArticleGroupId" + "_" + "$locArticleId" + "_")
3 $velocityPortletPreferences.setValue("portlet-setup-show-borders", "false")
4 $velocityPortletPreferences.setValue("group-id", "$locArticleGroupId")
5 $velocityPortletPreferences.setValue("article-id", "$locArticleId")
6 #set ($locPortletId = "56_INSTANCE_" + $globCounterOfThemeDynamicPortletId)
7 #set ($globCounterOfThemeDynamicPortletId = $globCounterOfThemeDynamicPortletId + 1)
8 #set($locRenderedPortletContent = $theme.runtime($locPortletId, "", $velocityPortletPreferences.toString()))
9 #set ($locCorrectArticleHasBeenReturned = $locRenderedPortletContent.contains($locGroupAndArticleIdForChecking))
10 #if ($locCorrectArticleHasBeenReturned)
11 ##correct - let it be
12 #else
13 ##incorrect - the article or group Id has been changed - so remove the old porltet preferences record and reinitialize it:
14 #set ($locPortletPreferenceService = $serviceLocator.findService("com.liferay.portal.service.PortletPreferencesLocalService"))
15 #set ($locPlidLong = $getterUtil.getLong($plid))
16 $locPortletPreferenceService.deletePortletPreferences(0, 3, $locPlidLong, $locPortletId)
17 ## ^^^^^^^^^^^^^^^^^^^^^^^^^^^ 0 ... PortletKeys.PREFS_OWNER_ID_DEFAULT, 3 ... PortletKeys.PREFS_OWNER_TYPE_LAYOUT
18 #set ($locRenderedPortletContent = $theme.runtime($locPortletId, "", $velocityPortletPreferences.toString()))
19 #end
20 $locRenderedPortletContent
21 $velocityPortletPreferences.reset()
- in $locArticleGroupId and $locArticleId are the Ids of the group to which belongs the article and id of article I want to put ionto the theme and $globCounterOfThemeDynamicPortletId is the counter of the portlet to get always a unique Id of the portlet for the page (this is maybe not necessary). I use the fact the Id of the group and article assigned to $locGroupAndArticleIdForChecking is contained in the rendered portlet content of the rendered journal article content - so I first check if it is correct - this is in the case the article has not been changed to the article with another article id from the last time the page has been rendered. In the case the value from $locGroupAndArticleIdForChecking is not contained in the portlet content then the content previously pointed to some other article (with other article id), therefore I delete it by calling deletePortletPreferences and call the runtime method again to recreate the portlet content with the changed preferences.
It is necessary to the check for $locGroupAndArticleIdForChecking there, because otherwise to be sure the correct article is rendered, You would have always to delete the portelt preferences and create it again - this would be then the case with every rendering of every page - this would be very ineffective, so I added such a check there...
I hope this can help - if my description can be understood, what I am not very sure ;-)
with regards, Artur