Liferay 7: Display Page for Journal Article

 Setup a Display Page for a Web Content (Journal Article) in Liferay 7


Manual and Programmatic Approach



Overview
You may need to define the JournalArticle display page to configure the layout, where the article should be displayed (for example, when clicking on the “Read More” link in Asset Publisher). Of course, you may do this manually in article configuration. But, if you create articles programmatically, you’ll need to set up the display page programmatically also. This article will cover both approaches.


Manual setup

To set up the DisplayPage manually - go to the Edit Article page (Site Menu -> Content & Data -> Web Content, and click on the article you need). 


On the sidebar to the right find the “Display Page Template” section, choose “Specific Display Page Template”, and click the “Select” button. In the popup, find the page you need (either on Public Pages or Private Pages tab):


Note: only those pages are available for selection, which have an Asset Publisher portlet on it, marked as “Default Asset Publisher on Page” in portlet configuration.


Save configuration, and the selected page should be displayed:


Programmatic approach


Find the display page and it’s layoutUuid:


Layout displayPage = layoutLocalService.getFriendlyURLLayout(group.getGroupId(), true, DISPLAY_PAGE_RIENDLY_URL);
String layoutUuid = displayPage != null ? displayPage.getUuid() : "";


Use layoutUuid as a parameter for article creation:


JournalArticle article = journalArticleLocalService.addArticle(
       creatorUserId,
       groupId,
       journalFolderId,
       CLASS_NAME_ID,
       CLASS_PK,
       ARTICLE_ID,
       AUTO_ARTICLE_ID,
       VERSION,
       titleMap,
       new HashMap<>(),
       content,
       structureName,
       templateName,
       layoutUuid,
       displayDateMonth,
       displayDateDay,
       displayDateYear,
       displayDateHours,
       displayDateMinutes,
       EXPIRATION_DATE_MONTH,
       EXPIRATION_DATE_DAY,
       EXPIRATION_DATE_YEAR,
       EXPIRATION_DATE_HOUR,
       EXPIRATION_DATE_MINUTE,
       NEVER_EXPIRE,
       REVIEW_DATE_MONTH,
       REVIEW_DATE_DAY,
       REVIEW_DATE_YEAR,
       REVIEW_DATE_HOUR,
       REVIEW_DATE_MINUTE,
       NEVER_REVIEW,
       INDEXABLE,
       SMALL_IMAGE,
       SMALL_IMAGE_URL,
       SMALL_IMAGE_FILE,
       IMAGES,
       ARTICLE_URL,
       serviceContext
);


Configure the display page:


private void setupDisplayPage(JournalArticle article, Layout displayPage) {
   try {
       long resourcePrimKey = article.getResourcePrimKey();
       long classNameId = classNameLocalService.getClassNameId(JournalArticle.class);
       long userId = article.getUserId();
       long groupId = article.getGroupId();
       ServiceContext serviceContext = new ServiceContext();
       serviceContext.setUserId(userId);
       serviceContext.setScopeGroupId(groupId);
       serviceContext.setAddGroupPermissions(true);
       serviceContext.setAddGuestPermissions(true);
       serviceContext.setDeriveDefaultPermissions(true);
       AssetDisplayPageEntry assetDisplayPageEntry = assetDisplayPageEntryLocalService.fetchAssetDisplayPageEntry(groupId, classNameId, resourcePrimKey);
       if (assetDisplayPageEntry == null) {
           assetDisplayPageEntry = assetDisplayPageEntryLocalService.addAssetDisplayPageEntry(
                   userId,
                   groupId,
                   classNameId,
                   resourcePrimKey,
                   GetterUtil.DEFAULT_LONG,
                   AssetDisplayPageConstants.TYPE_SPECIFIC,
                   serviceContext
           );
       }
       long plid = displayPage.getPlid();
       assetDisplayPageEntry.setType(AssetDisplayPageConstants.TYPE_SPECIFIC);
       assetDisplayPageEntry.setPlid(plid);
       assetDisplayPageEntryLocalService.updateAssetDisplayPageEntry(assetDisplayPageEntry);
   } catch (Exception e) {
       _log.error(String.format("Failed to setup a display page for article (articleId=%s), cause: '%s'",
               article.getArticleId(), e.getMessage()));
   }
}

Enjoy 😏

Comments

Popular posts from this blog

Liferay Search Container Example

Liferay DXP - max upload file size

Liferay Keycloak integration