Assign a Display Page Templates to multiple Web content after migrating from a previous version of DXP 7.1
How To articles are not official guidelines or officially supported documentation. They are community-contributed content and may not always reflect the latest updates to Liferay DXP. We welcome your feedback to improve How To articles!
While we make every effort to ensure this Knowledge Base is accurate, it may not always reflect the most recent updates or official guidelines.We appreciate your understanding and encourage you to reach out with any feedback or concerns.
Legacy Article
You are viewing an article from our legacy "FastTrack"
publication program, made available for informational purposes. Articles
in this program were published without a requirement for independent
editing or verification and are provided"as is" without
guarantee.
Before using any information from this article, independently verify its
suitability for your situation and project.
Issue
- In versions prior to 7.1 there were no Display Page Templates to use with web contents.
- After migrating to 7.1+, we can create a default Display Page Template associated to an existing web content structure from Design > Page Templates > Display Page Templates.
- If we edit an existing web content with that structure, we see that it points to the default Display Page Template, but the relation will only be effective if we save the web content.
- Is there a way to avoid this manual work and massively change the Display Page Template of migrated web contents?
Resolution
- To avoid manually changing each web content, we can write a Groovy a script using this API method (source):
addAssetDisplayPageEntry(long userId, long groupId, long classNameId, long classPK, long layoutPageTemplateEntryId, int type, com.liferay.portal.kernel.service.ServiceContext serviceContext)
-
Important: The following pieces of Groovy code are intended as a starting guide and are not officially supported. They should be carefully reviewed and adapted for each use case. A database backup should be made before executing any code, as the changes are not revertible.
- The first step would be to get the
userId and classNameId
// We are using the current user running the script but it could be another admin user.
// Documentation: https://help.liferay.com/hc/en-us/articles/360017877412-Running-Scripts-From-the-Script-Console#tip-1-use-the-predefined-variables
def userId = Long.valueOf(userInfo.get('liferay.user.id'));
def classNameId = PortalUtil.getClassNameId(JournalArticle.class.getName());
- The next step would be getting the necessary
JournalArticleResources for the web contents to modify.
List<JournalArticleResource> journalArticleResources =
JournalArticleResourceLocalServiceUtil.getJournalArticleResources(-1, -1);
Note: If you have a lot of articles, in order to maintain performance please make sure you paginate your call to this service using a count method, for example:
JournalArticleLocalServiceUtil.getStructureArticlesCount(long groupId, String ddmStructureKey)
- Finally, for each
journalArticleResource:
- Get the associated
groupId and resourcePrimKey
def groupId = journalArticleResource.getGroupId();
def resourcePrimKey = journalArticleResource.getResourcePrimKey();
- Choose the
layoutPageTemplateId and type, which can be the same for all journalArticleResources or different for each one:
// We are assigning a default display page template relationships, hence TYPE_DEFAULT, so we also use 0L for layoutPageTemplateEntryId
// If other types are used, such as TYPE_SPECIFIC, query the table layoutpagetemplateentry for the value of layoutPageTemplateEntryId associated to a particular display page template.
def type = AssetDisplayPageConstants.TYPE_DEFAULT;
def layoutPageTemplateEntryId = 0L;
- Obtain the existing
assetDisplayPageEntry, that is the Display Page Template, and add assign a new one:
AssetDisplayPageEntry assetDisplayPageEntry = AssetDisplayPageEntryLocalServiceUtil.fetchAssetDisplayPageEntry(groupId, classNameId, resourcePrimKey);
if (assetDisplayPageEntry == null) {
AssetDisplayPageEntryLocalServiceUtil.addAssetDisplayPageEntry(userId, groupId, classNameId, resourcePrimKey, layoutPageTemplateEntryId, type, new ServiceContext());
}
-
Reminder: Please carefully review this code to adapt it to the particular use case and create a database backup before execution.
Did this article resolve your issue ?